网页源码 webBrowser控制页面元素 username="张三";//webBrowser获取网页变量 //按下鼠标键触发的函数 function mdown(){ if (event.button==1){ document.getElementById("div1").innerText="mousedo wn左键"; }else{ document.getElementById("div1").innerText="mousedo wn右键"; } } //放开鼠标键触发的函数 function mup(){ if (event.button==1){ document.getElementById("div1").innerText="mouseup左键"; }else{ document.getElementById("div1").innerText="mouseup右键"; } } //无参函数test function test(){ alert("abc"); } //有参函数test2 function test2(a1,a2){ alert(a1); alert(a2); } 左右键测试区域 1.导航链接 webBrowser1.Navigate("c:\\index.html"); 2.屏蔽网页脚本错误 webBrowser1.ScriptErrorsSuppressed=true; 3.获取网页有name属性或id属性元素的属性值 webBrowser1.Document.GetElementById("btn1").GetAtt ribute("value");//GetAttribute返回值为string 4.执行网页中按钮元素的单击 webBrowser1.Document.GetElementById("btn1").Invoke Member("click"); 4.调用网页JavaScript无参函数 webBrowser1.Document.InvokeScript("test1"); 5.调用网页JavaScript有参函数 object[] args = new object[2]; args[0] = (object)"约定"; args[1] = (object)"蓝天"; webBrowser1.Document.InvokeScript("test2", args); 6.引用mshtml.dll 1)在解决方案资源管理器中右键单击引用,选择"添加引用",在.net标签页中找到Microsoft.mshtml,选择第三个,确定
2)在解决方案资源管理器中右键单击引用,选择"添加引用",在COM标签页中找到Microsoft HTML Object Library,确定
两种添加的mshtml.dll的版本不一样,.net中添加的是7.0,COM中添加的是4.0
7.引用shdocvw
1.在解决方案资源管理器中右键单击引用,选择"添加引用",在COM标签页中找到Microsoft Internet Controls,确定,右侧的路径中写的是iframe.dll
2.在解决方案资源管理器中右键单击引用,选择"添加引用",在浏览标签页中在C:\Windows\System32下找到shdocvw.dll,确定
8.阻止在新窗口打开链接,只在当前webBrowser中打开 private void webBrowser1_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl) { Cancel = true; webBrowser1.Navigate(bstrUrl); } 在使用webBrowser之前执行以下两行代码: webBrowser1.Navigate("about:blank"); (this.webBrowser1.ActiveXInstance as SHDocVw.WebBrowser).NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler( webBrowser1_NewWindow3);
需要引用mshtml,SHDocVw 9.触发网页元素的鼠标左键onmousedown和onmouseup事件 object refObj = null; IHTMLEventObj2 EventObj2 = (webBrowser1.Document.DomDocument as IHTMLDocument4).CreateEventObject(ref refObj) as IHTMLEventObj2; EventObj2.button = 1; object eventRef = EventObj2; ((webBrowser1.Document.DomDocument as HTMLDocument).getElementById("div1") as IHTMLElement3).FireEvent("onmousedown", ref eventRef); ((webBrowser1.Document.DomDocument as HTMLDocument).getElementById("div1") as IHTMLElement3).FireEvent("onmouseup", ref eventRef);
2)在解决方案资源管理器中右键单击引用,选择"添加引用",在COM标签页中找到Microsoft HTML Object Library,确定
两种添加的mshtml.dll的版本不一样,.net中添加的是7.0,COM中添加的是4.0
7.引用shdocvw
1.在解决方案资源管理器中右键单击引用,选择"添加引用",在COM标签页中找到Microsoft Internet Controls,确定,右侧的路径中写的是iframe.dll
2.在解决方案资源管理器中右键单击引用,选择"添加引用",在浏览标签页中在C:\Windows\System32下找到shdocvw.dll,确定
8.阻止在新窗口打开链接,只在当前webBrowser中打开 private void webBrowser1_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl) { Cancel = true; webBrowser1.Navigate(bstrUrl); } 在使用webBrowser之前执行以下两行代码: webBrowser1.Navigate("about:blank"); (this.webBrowser1.ActiveXInstance as SHDocVw.WebBrowser).NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler( webBrowser1_NewWindow3);
需要引用mshtml,SHDocVw 9.触发网页元素的鼠标左键onmousedown和onmouseup事件 object refObj = null; IHTMLEventObj2 EventObj2 = (webBrowser1.Document.DomDocument as IHTMLDocument4).CreateEventObject(ref refObj) as IHTMLEventObj2; EventObj2.button = 1; object eventRef = EventObj2; ((webBrowser1.Document.DomDocument as HTMLDocument).getElementById("div1") as IHTMLElement3).FireEvent("onmousedown", ref eventRef); ((webBrowser1.Document.DomDocument as HTMLDocument).getElementById("div1") as IHTMLElement3).FireEvent("onmouseup", ref eventRef);