お世話になっております。
###前提・実現したいこと
.NET Framework 4.5.2 で、WebBrowser コントロールを利用した Windows フォームを作成しています。
ひとまず、任意のWEBページを読みこみ、特定の要素の属性を変更させる練習?をしています。
うまく動作させたいのですが、ご存知の方、お手ほどきをお願いできますでしょうか?
WebBrowser コントロールがIE11 相当で動くよう、レジストリを編集してあります(動作確認済み)
フォーム上の WebBrowser コントロールを例示した HTML ファイルがある URL にナビゲートさせ、(独自コンテキストメニュー項目の選択(menuItem1_Click イベントハンドラ)を介して)HTML コンテンツの1番目にある div 要素のテキスト・前景色・背景色を変更させる目論見です。
HTML コンテンツの先頭にある div 要素を HTMLElement 型のオブジェクトとして得て、innerText プロパティへの文字列代入と、SetAttribute メソッドでの属性設定を行っています。
###発生している問題・エラーメッセージ
テキストは ABC からSAMPLE に変わるのですが、色が変わりません。
メッセージボックスに、属性の内容を表示させてみており、SetAttribute 呼び出し後に値はセットされていることは確認できます("::" -> "yellow: blue: green")
###該当のソースコード
(練習用のため、エラーハンドルなど無く、まったくもって簡素です)
html
1<html> 2<body> 3<div>ABC</div> 4<div>DEF</div> 5</html>
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10 11namespace Sample 12{ 13 public partial class SampleForm : Form 14 { 15 // (略) 16 private void menuItem1_Click(object sender, EventArgs e) 17 { 18 var target = webBrowserMain.Document.GetElementsByTagName("div")[0]; 19 20 var beforeAttrs = target.GetAttribute("background-color") + ": " + target.GetAttribute("backgrounColor") + ": " + target.GetAttribute("color"); 21 System.Windows.Forms.MessageBox.Show(beforeAttrs); 22 23 target.InnerText = "SAMPLE"; 24 target.SetAttribute("background-color", "yellow"); 25 target.SetAttribute("backgrounColor", "blue"); 26 target.SetAttribute("color", "green"); 27 28 var afterAttrs = target.GetAttribute("background-color") + ": " + target.GetAttribute("backgrounColor") + ": " + target.GetAttribute("color"); 29 System.Windows.Forms.MessageBox.Show(afterAttrs); 30 } 31 } 32} 33 34![イメージ説明](9d9469adfd3fbae4816c61bbda045e05.png) 35
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/15 05:01
2016/08/15 05:04