實用方法1:獲取狀態欄信息html
void webBrowser1_StatusTextChanged(object sender, EventArgs e)
{
label1.Text = webBrowser1.StatusText;
}
//在Navigated事件處理函數中改變地址欄地址是最恰當的:
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
textBox1.Text = webBrowser1.Url.ToString();
}
//建議使用執行單擊事件的方式來設置單選框,而不是修改屬性:
webBrowser1.Document.GetElementById("RBT_A").InvokeMember("click");
//比較常見的聯動型多級下拉列表就是省/市縣選擇了,這種狀況下直接設置選擇項的屬性不會觸發聯動,須要在最後執行觸發事件函數才能正常工做:
foreach (HtmlElement f in s.GetElementsByTagName("option"))
{
if (f.InnerText == "北京")
{
f.SetAttribute("selected", "selected");
}
else
{
f.SetAttribute("selected", "");
}
}
s.RaiseEvent("onchange");
<html>
<body>
<input type="button" id="btnClose" value="關閉" />
</body>
</html>
HtmlDocument htmlDoc = webBrowser.Document;
HtmlElement btnElement = htmlDoc.All[ " btnClose "];
if (btnElement != null)
{
btnElement.click += new HtmlElementEventHandler(HtmlBtnClose_Click);
}
//很簡單吧?那麼稍稍高級一點的——咱們都知道一個HTML元素可能有不少各類各樣的事件,而HtmlElement這個類只給出最經常使用、共通的幾個。那麼,如何響應其餘事件呢?這也很簡單,只須要調用HtmlElement的AttachEventHandler就能夠了:btnElement.AttachEventHandler(" new EventHandler(HtmlBtnClose_Click));
//這一句等價於上面的btnElement.click += new HtmlElementEventHandler(HtmlBtnClose_Click);
對於其餘事件,把"onclick"換成該事件的名字就能夠了。例如:web
formElement.AttachEventHandler("onsubmit", new EventHandler(HtmlForm_Submit));
HtmlElement btnSubmit = webBrowser.Document.All["submitbutton"];
HtmlElement tbUserid = webBrowser.Document.All["username"];
HtmlElement tbPasswd = webBrowser.Document.All["password"];
if (tbUserid == null || tbPasswd == null || btnSubmit == null)
return;
tbUserid.SetAttribute("value", "smalldust");
tbPasswd.SetAttribute("value", "12345678");
btnSubmit.InvokeMember("click");
關於表單的提交,的確還有另外一種方法就是獲取form元素而不是button,並用form元素的submit方法:ide
HtmlElement formLogin = webBrowser.Document.Forms["loginForm"];
//……
formLogin.InvokeMember("submit");
本文之因此沒有推薦這種方法,是由於如今的網頁,不少都在submit按鈕上添加onclick事件,以對提交的內容作最基本的驗證。若是直接使用form的submit方法,這些驗證代碼就得不到執行,有可能會引發錯誤。函數
首先是調用Web頁面的腳本中已經定義好的函數。假設HTML中有以下Javascript:this
function DoAdd(a, b) {
return a + b;
}
那麼,咱們要在WinForm調用它,只需以下代碼便可:spa
object oSum = webBrowser.Document.InvokeScript("DoAdd", new object[] { 1, 2 });
int sum = Convert.ToInt32(oSum);
其次,若是咱們想執行一段Web頁面中本來沒有的腳本,該怎麼作呢?此次.Net的類沒有提供,看來還要依靠COM了。IHTMLWindow2能夠將任意的字符串做爲腳本代碼來執行。debug
string scriptline01 = @"function ShowPageInfo() {";
string scriptline02 = @" var numLinks = document.links.length; ";
string scriptline03 = @" var numForms = document.forms.length; ";
string scriptline04 = @" var numImages = document.p_w_picpaths.length; ";
string scriptline05 = @" var numScripts = document.scripts.length; ";
string scriptline06 = @" alert('網頁的統計結果:\r\n連接數:' + numLinks + ";
string scriptline07 = @" '\r\n表單數:' + numForms + ";
string scriptline08 = @" '\r\n圖像數:' + numImages + ";
string scriptline09 = @" '\r\n腳本數:' + numScripts);}";
string scriptline10 = @"ShowPageInfo();";
string strScript = scriptline01 + scriptline02 + scriptline03 + scriptline04 + scriptline05 +
scriptline06 + scriptline07 + scriptline08 + scriptline09 + scriptline10;
IHTMLWindow2 win = (IHTMLWindow2)webBrowser.Document.Window.DomWindow;
win.execScript(strScript, "Javascript");
最後:在腳本中調用WinForm裏的代碼,這個可能嗎? 呵呵,固然是可能的。
下面的代碼示例演示如何使用 ObjectForScripting 屬性。在該示例中,ObjectForScripting 屬性被設置爲當前窗體。code
- using System;
- using System.Windows.Forms;
- using System.Security.Permissions;
- [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
- [System.Runtime.InteropServices.ComVisibleAttribute(true)]
- public class Form1 : Form
- {
- private WebBrowser webBrowser1 = new WebBrowser();
- private Button button1 = new Button();
- [STAThread]
- public static void Main()
- {
- Application.EnableVisualStyles();
- Application.Run(new Form1());
- }
- public Form1()
- {
- button1.Text = "call script code from client code";
- button1.Dock = DockStyle.Top;
- button1.Click += new EventHandler(button1_Click);
- webBrowser1.Dock = DockStyle.Fill;
- Controls.Add(webBrowser1);
- Controls.Add(button1);
- Load += new EventHandler(Form1_Load);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- webBrowser1.AllowWebBrowserDrop = false;
- webBrowser1.IsWebBrowserContextMenuEnabled = false;
- webBrowser1.WebBrowserShortcutsEnabled = false;
- webBrowser1.ObjectForScripting = this;
- // Uncomment the following line when you are finished debugging.
- //webBrowser1.ScriptErrorsSuppressed = true;
- webBrowser1.DocumentText =
- "<html><head><script>" +
- "function test(message) { alert(message); }" +
- "</script></head><body><button " +
- "onclick=\"window.external.Test('called from script code')\">" +
- "call client code from script code</button>" +
- "</body></html>";
- }
- public void Test(String message)
- {
- MessageBox.Show(message, "client code");
- }
- private void button1_Click(object sender, EventArgs e)
- {
- webBrowser1.Document.InvokeScript("test",
- new String[] { "called from client code" });
- }
- }
http://www.cnblogs.com/08shiyan/archive/2011/04/22/2023861.htmlorm