欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#的WebBrowser的操作與注意事項介紹

 更新時間:2013年03月17日 16:10:32   作者:  
C#的WebBrowser的操作與注意事項介紹,需要的朋友可以參考一下

1.在Winform里使用WebBrowser,要對Form1.cs添加一些東西:
    1.1 在“public partial class Form1 : Form”上方,添加:

復(fù)制代碼 代碼如下:

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]

   1.2 在Form1的Shown事件中,添加:

復(fù)制代碼 代碼如下:

this.UI_webBrowser.ObjectForScripting = this;

2.由于WebBrowser是放在Winform界面中,由界面線程(主線程)管理,執(zhí)行渲染也是主線程,因此,不能把業(yè)務(wù)邏輯放在主線程中,應(yīng)該另開一個線程,執(zhí)行業(yè)務(wù)邏輯。并通過Invoke來與WebBrowser交互。

  例子:

復(fù)制代碼 代碼如下:

private void Form1_Shown(object sender, EventArgs e)
 {
     this._thread_mainLogic = new Thread(this.ThreadFunction_MainLogic);
     this._thread_mainLogic.Start();
 }

 private void ThreadFunction_MainLogic()
 {
     Debugger.Log(0, "", "\r\n開始執(zhí)行業(yè)務(wù)邏輯\r\n");
     this.Invoke( new Action( () => { this.webBrowser.Navigate("http://www.baidu.com");} ) );//通過Invoke來與webBrowser交互
     .....
 }

3.瀏覽指定URL。注意,此方法為異步方法,需要手動同步。

復(fù)制代碼 代碼如下:

//以下方法不是線程安全方法
 private AutoResetEvent _threadControlEvent_Tool_webBrowser_Navigate = null;

 private void Tool_webBrowser_Navigate(string arg_URL)
 {
     this._threadControlEvent_Tool_webBrowser_Navigate = new AutoResetEvent(false);
     this.Invoke(new Action(() =>
     {
         this.webBrowser.DocumentCompleted += webBrowser_DocumentCompleted_Tool_webBrowser_Navigate;
         this.webBrowser.Navigate(arg_URL);
     }));
     this._threadControlEvent_Tool_webBrowser_Navigate.WaitOne();
     this._threadControlEvent_Tool_webBrowser_Navigate.Close();
     this._threadControlEvent_Tool_webBrowser_Navigate.Dispose();
 }

 void webBrowser_DocumentCompleted_Tool_webBrowser_Navigate(object sender, WebBrowserDocumentCompletedEventArgs e)
 {
     this.webBrowser.DocumentCompleted -= webBrowser_DocumentCompleted_Tool_webBrowser_Navigate;
     this._threadControlEvent_Tool_webBrowser_Navigate.Set();
 }

4.根據(jù)ID獲取按鈕,并點擊它:(也可作用于網(wǎng)頁中的URL鏈接)

復(fù)制代碼 代碼如下:

//假設(shè)網(wǎng)頁里的按鈕,ID為"btn"
HtmlElement element_btn = null;
this.Invoke(new Action(() => { element_btn = this.UI_webBrowser.Document.All["btn"]; }));//獲取
element_btn.InvokeMember("Click");//點擊,此方法為同步方法,可安全使用

5.根據(jù)ID獲取輸入框,并輸入內(nèi)容

復(fù)制代碼 代碼如下:

//假設(shè)網(wǎng)頁里的輸入框,ID為"input"
HtmlElement input = null;
this.Invoke( new Action( () => { input = this.UI_webBrowser.Document.All["input"]; } ) );//獲取
input.InnerText = "123";//輸入"123"。此方法不為同步方法,需要使用下文的Wait_SafeMode方法。
Tool_Wait_SafeMode();//實現(xiàn)在下文

6.根據(jù)ID獲取form,并提交(submit)

復(fù)制代碼 代碼如下:

//假設(shè)網(wǎng)頁里的form,ID為"form2"
HtmlElement form2 = null;
this.Invoke( new Action( () => { form2 = this.UI_webBrowser.Document.Forms["form2"]; } ) );//獲取
form_submit.InvokeMember("submit");//提交form2里的內(nèi)容。此方法為同步方法,可安全使用。

7.根據(jù)ID獲取CheckBox,并設(shè)置為已選中(Checked)

復(fù)制代碼 代碼如下:

//假設(shè)網(wǎng)頁里的CheckBox,ID為"checkbox5"
HtmlElement checkBox5 = null;
this.Invoke( new Action( () => { checkBox5 = this.UI_webBrowser.Document.All["checkbox5"]; } ) );//獲取
checkBox5.SetAttribute("Checked", "true");//設(shè)置為已選中。此方法為同步方法,可安全使用。


8.根據(jù)元素的已知屬性,來查找該元素

復(fù)制代碼 代碼如下:

//假設(shè)網(wǎng)頁里,有且僅有這樣的一個元素:它有一個名為"value"的屬性,屬性值為"12345"
 bool isFind = false;
 HtmlElementCollection htmlElementCollection = null;
 this.Invoke( new Action( () => { htmlElementCollection = this.webBrowser.Document.All; } ) );//獲取集合
 HtmlElement resultElement = null;

 foreach (HtmlElement currentElement in htmlElementCollection)//在集合中遍歷所有元素來尋找
 {
     if (currentElement.GetAttribute("value") == "12345")
     {
         isFind = true;
         resultElement = currentElement;
         break;
     }
 }

 if( ! isFind )
 {
     對沒有找到的情況進行處理;
 }


9.對網(wǎng)頁中的ComboBox進行設(shè)置。注意,以下代碼有問題,請勿使用。由于SetAttribute是一個沒有回應(yīng)的API,因此建議使用js來進行設(shè)置。下文中,讓W(xué)ebBrowser執(zhí)行js代碼,可以做到有回調(diào)。

復(fù)制代碼 代碼如下:

//假設(shè)網(wǎng)頁中存在一個ComboBox,ID為"comboBox123",下拉菜單有兩項:
 //第一項的ID為1,value為"蘋果"
 //第二項的ID為2,value為"西瓜"
 HtmlElement element_comboBox = null;
 this.Invoke( new Action( () => { element_comboBox = this.webBrowser.Document.All["comboBox123"]; } ) );//獲取
 Tool_Wait_SafeMode();
 this.Invoke( new Action( () => { element_comboBox.SetAttribute("value", "2"); } ) );//設(shè)置為"西瓜",即value = 2
 Tool_Wait_SafeMode();

10.Tool_Wait_SafeMode

復(fù)制代碼 代碼如下:

private void Tool_Wait_SafeMode()
 {
     bool isError = false;
     bool isBusy = false;
     do
     {
         this.Invoke(new Action(() =>
         {
             try
             {
                 isBusy = this.webBrowser.IsBusy;
             }
             catch (System.Exception ex)
             {
                 isError = true;
             }
         }));
         if (isError)
         {
             Thread.Sleep(errorWaitTime);//建議為2秒以上。這個時間要根據(jù)機器性能來設(shè)置,必須設(shè)置長一些。
         }
         else
         {
             if (isBusy)
             {
                 Thread.Sleep(arg_waitTime);//建議為0.1秒以上。這個時間要根據(jù)機器性能來設(shè)置,可以設(shè)置短一些。
             }
         }
     }
     while (isError | isBusy);
 }

11.在網(wǎng)頁中執(zhí)行js代碼

    由于讓W(xué)ebBrowser執(zhí)行js,是一個異步過程,并且還需要回調(diào),因此這個功能有些復(fù)雜。對此進行了封裝,把它封裝為了一個同步過程,來方便使用:

復(fù)制代碼 代碼如下:

#region private void Tool_webBrowser_ExecUserJSScript(string arg_jsCodes)
         private AutoResetEvent _threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init = null;
         private AutoResetEvent _threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec = null;
         private object _returnObj_Tool_webBrowser_ExecUserJSScript = null;

         /// <summary>
         /// 用WebBrowser執(zhí)行JS自定義語句。
         /// 1:定義一個js方法,方法名盡量特殊些,以免與html里已存在的js方法重名。這個方法的結(jié)尾,一定要使用window.external.NotifyCSharpComplete( msg );才能實現(xiàn)js執(zhí)行結(jié)束后,通知CSharp。把這個方法傳遞給參數(shù)arg_jsFunctionDefineCodes。
         /// 2:把這個方法的方法名,傳遞給參數(shù)arg_jsFunctionName。
         /// 3: 把這個方法,需要傳遞的參數(shù),傳遞給arg_functionArgs。如果不需要傳入?yún)?shù),該字段可以不需要賦值,或賦值為null,或賦值為new object[]{}。
         /// 4: 如果js在回調(diào)C#時,不需要返回參數(shù),請在js方法里使用window.external.NotifyCSharpComplete( null );如果有返回參數(shù),則可以修改為window.external.NotifyCSharpComplete( 參數(shù)變量 );
         /// 例子:js方法:function jsFunctionTest( arg1, arg2 ) { var arg3 = arg1 + arg2; window.external.NotifyCSharpComplete( "運算結(jié)果:" + arg3 ); }
         /// 則 arg_jsFunctionDefineCodes = "function jsFunctionTest( arg1, arg2 ) { var arg3 = arg1 + arg2; window.external.NotifyCSharpComplete( \"運算結(jié)果:\" + arg3 ); }";
         ///    arg_jsFunctionName = jsFunctionTest
         ///    如果需要傳遞的參數(shù)為123、456,則arg_functionArgs = new object[] { 123, 456 }
         /// 返回值,通過object進行返回。如果object是一個其他類型,則請自行轉(zhuǎn)換。比如:stirng result = (string)Tool_webBrowser_ExecUserJSScript(...);
         /// </summary>
         /// <param name="arg_jsFunctionDefineCodes">js方法,注意,總長度不能超過1991(總長不能超過2048,程序中會對字符串添加一些內(nèi)容。)</param>
         /// <param name="arg_jsFunctionName">js方法的方法名</param>
         /// <param name="arg_functionArgs">js方法的參數(shù)列表。如果不需要傳入?yún)?shù),該字段可以不需要賦值,或賦值為null,或賦值為new object[]{}</param>
         /// <returns>返回執(zhí)行結(jié)果。注意,默認(rèn)為返回參數(shù)。如果沒有返回,請修改js方法,把NotifyCSharpComplete( msg )改為NotifyCSharpComplete( null )</returns>
         private object Tool_webBrowser_ExecUserJSScript(string arg_jsFunctionDefineCodes, string arg_jsFunctionName, object[] arg_functionArgs = null)
         {
             this._returnObj_Tool_webBrowser_ExecUserJSScript = null;
             if (arg_jsFunctionDefineCodes.Length > 1991)
             {
                 throw new Exception("錯誤:js方法定義的長度超過了1991。");
             }
             //1.寫入js方法。
             arg_jsFunctionDefineCodes = "javascript:" + arg_jsFunctionDefineCodes + ";window.external.NotifyCSharpCompleteInit();";
             if (arg_jsFunctionDefineCodes.Length >= 2048)
             {
                 throw new Exception("錯誤:js方法定義的總長度超過了2048(原始方法 + 添加的內(nèi)容)。");
             }
             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init = new AutoResetEvent(false);
             this.Invoke(new Action(() =>
             {
                 this.webBrowser.Navigate(arg_jsFunctionDefineCodes);
             }));
             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init.WaitOne();
             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init.Close();
             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init.Dispose();
             //2.執(zhí)行js方法
             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec = new AutoResetEvent(false);
             this.Invoke(new Action(() =>
             {
                 this.webBrowser.Document.InvokeScript(arg_jsFunctionName, arg_functionArgs);
             }));
             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec.WaitOne();
             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec.Close();
             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec.Dispose();
             //3.返回參數(shù)
             return this._returnObj_Tool_webBrowser_ExecUserJSScript;
         }

         public void NotifyCSharpCompleteInit()
         {
             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Init.Set();
         }

         public void NotifyCSharpComplete(object arg_obj)
         {
             this._returnObj_Tool_webBrowser_ExecUserJSScript = arg_obj;
             this._threadControlEvent_Tool_webBrowser_ExecUserJSScript_Exec.Set();
         }
         #endregion

用法例子1:

復(fù)制代碼 代碼如下:

string jsCmdTest = "function testFunction( msg ) { setTimeout(\"window.external.NotifyCSharpComplete(\\\"返回內(nèi)容\\\");\", 5000);};";
object returnObj = this.Tool_webBrowser_ExecUserJSScript(jsCmdTest, "testFunction", new object[] {"傳入?yún)?shù)"});
string returnStr = returnObj as string;

用法例子2:

復(fù)制代碼 代碼如下:

string jsCmdTest = "function testFunction( ) { var a = 122; var b = 244; var c = a + b; window.external.NotifyCSharpComplete(c);};";
object returnObj = this.Tool_webBrowser_ExecUserJSScript(jsCmdTest, "testFunction", null);
int returnInt = (int)returnObj;

用法例子3:

復(fù)制代碼 代碼如下:

string jsCmdTest = "function testFunction( ) { window.external.NotifyCSharpComplete(null);};";
object returnObj = this.Tool_webBrowser_ExecUserJSScript(jsCmdTest, "testFunction", null);
string result = "js執(zhí)行完畢";

總結(jié):使用WebBrowser的兩個大問題:

1.WebBrowser是調(diào)用機器上的IE,因此版本、渲染的程序也就取決與IE的版本與渲染器的程序。

2.WebBrowser的執(zhí)行js等很多操作都是異步且無事件回應(yīng)的,只能自己去估算一個執(zhí)行時間,來等待。并且等待時間一定要大于js實際執(zhí)行時間,否則后續(xù)代碼會出問題。

3.目前,執(zhí)行js的方式,只能通過瀏覽器的地址欄。地址欄是有長度限制的。

相關(guān)文章

最新評論