WPF使用Webbrowser操作网页的主要代码
生活随笔
收集整理的這篇文章主要介紹了
WPF使用Webbrowser操作网页的主要代码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1,引用mshtml.dllusing mshtml;
2,獲取元素屬性值IHTMLDocument2 doc2=(IHTMLDocument2)webbrowser1.Document;
IHTMLElement img=(IHTMLElement)doc2.all.item("regimg",0);
string imgUrl=(string)img.getAttribute("src");
3,取表單控件IHTMLElement loginName=(IHTMLElement)doc2.all.item("loginname",0);
IHTMLElement loginPW=(IHTMLElement)doc2.all.item("password",0);
IHTMLElement loingYZM=(IHTMLElement)doc2.all.item("regcode",0);
IHTMLElement loginBT=(IHTMLElement)doc2.all.item("formsubmit",0);
4,填寫表單控件loginName.setAttribute("value",tbLoginName.Text);
loginPW.setAttribute("value",tbLoginPassWord.Password);
loginYZM.setAttribute("value",tbYZ.Text);
5,點(diǎn)擊按鈕loginBT.click();
6,執(zhí)行js腳本方法1:IHTMLwindow win=(IHTMLWindow2)doc2.parentWindows;
win.execScript("alert('hello!')","javascript");
方法2:webbrowser1.InvokeScript("eval","alert('hello!')");
7,屏蔽alert、confirm等,通過重定義實(shí)現(xiàn)復(fù)制代碼
private voie webbrowser1_navigated(object sender,WebBroserNavigatedEventArgs e)
{IHTMLWindow2 win=(IHTMLWindow2)webbrowser1.Document.Window.DomWindow;string s=@"window.alert=null; window.οnerrοr=null;window.confirm=null; windows.open=null; window.showModalDialg=null;";win.execScript(s,"javascript");
}
復(fù)制代碼
8,接收js消息復(fù)制代碼
[ComVisible(true)] //這句要加到類定義前,可與COM通信private void webbrowser1_Navigated(objec sender,WebBrowserNavigatedEventargs e)
{IHTMLWindow2 win=(IHTMLWindow2)webbrowser1.Document.Window.DomWindow;//假設(shè)把a(bǔ)lert消息傳出來處理string s=@"function alert(str){window.external.procMessage(str);}";win.execScript(s,"javascript");webbrowser1.ObjectForScripting=this; //指定腳本消息送到當(dāng)前實(shí)例處理
}//處理腳本消息的方法
public void procMessage(string s)
{MessageBox.Show("腳本消息:"+s);
}
#IHTMLDocument2 屬性說明
{IHTMLDocument2 方法:}3 write //寫入4 writeln //寫入并換行5 open //打開一個流,以收集 document.write 或 document.writeln 的輸出6 close //關(guān)閉并輸出用 document.open 方法打開的流7 clear //清空文檔中所有元素, 可能暫時不支持8 queryCommandSupported //是否支持指定命令9 queryCommandEnabled //指定命令是否有效10 queryCommandState //是否設(shè)置了指定命令11 queryCommandIndeterm //指定命令是否處于不確定狀態(tài)12 queryCommandText //命令13 queryCommandValue //命令值14 execCommand //執(zhí)行命令15 execCommandShowHelp //命令幫助信息16 createElement //建立元素17 elementFromPoint //指定坐標(biāo)下的元素18 createStyleSheet //創(chuàng)建一個樣式表19 20 {IHTMLDocument2 屬性:}21 all //所有 HTML 元素的集合22 images //所有 image 對象的集合23 applets //所有 applet 對象的集合24 links //所有 link 對象的集合25 forms //所有 form 對象的集合26 anchors //所有 anchor 對象的集合27 scripts //所有 script 對象的集合28 frames //所有 frame 對象的集合, 這應(yīng)該和 window.frames 是一樣的29 embeds //所有 embed 對象的集合30 plugins //所有 plugin 對象的集合31 styleSheets //樣式表集合32 title //網(wǎng)頁標(biāo)題33 body //body 對象34 activeElement //當(dāng)前具有輸入焦點(diǎn)的元素35 designMode //設(shè)計(jì)模式, 可選值: Inherit On Off36 selection //當(dāng)前選擇37 readyState //頁面讀取狀態(tài): uninitialized、loading、loaded、interactive、completed38 linkColor //鏈接的顏色39 alinkColor //活動鏈接的顏色40 vlinkColor //已訪問過的鏈接的顏色41 bgColor //背景顏色42 fgColor //文本顏色43 referrer //返回載入當(dāng)前頁面的頁面的 URL44 location //同 window.location45 lastModified //返回文檔最后修改的日期和時間46 url //返回當(dāng)前文檔的 URL47 domain //返回域名48 cookie //返回文檔相關(guān)的 cookie49 expando //可設(shè)置與獲取的布爾值, 是否允許擴(kuò)展50 charset //獲取文檔的字符集名稱51 defaultCharset //獲取瀏覽器默認(rèn)的字符集名稱52 mimeType //文檔類型53 fileSize //文檔大小, 單位字節(jié), 用字符串表示54 fileCreatedDate //文檔建立時間 ???55 fileModifiedDate //文檔修改時間 ???56 fileUpdatedDate //文檔更新時間 ???57 security //文檔安全58 protocol //傳輸協(xié)議59 nameProp //文件標(biāo)題, 好像同 Title60 parentWindow //父窗口61 Script //62 63 {IHTMLDocument2 事件:}64 onhelp //用 F1 獲取幫助時65 onclick //單擊時66 ondblclick //雙擊時67 onmousedown //鼠標(biāo)點(diǎn)下時68 onmouseup //鼠標(biāo)抬起時69 onmousemove //鼠標(biāo)移動時70 onmouseover //鼠標(biāo)進(jìn)入時71 onmouseout //鼠標(biāo)離開時72 onkeypress //按鍵時73 onkeyup //鍵按住時74 onkeydown //鍵抬起時75 onreadystatechange //狀態(tài)改變時76 onrowexit //當(dāng)前數(shù)據(jù)源的數(shù)據(jù)將要發(fā)生變化時77 onrowenter //當(dāng)前數(shù)據(jù)源的數(shù)據(jù)發(fā)生變化并且有新的數(shù)據(jù)時78 ondragstart //開始拖動時79 onselectstart //開始選擇時80 onbeforeupdate //更新前81 onafterupdate //更新后82 onerrorupdate //更新錯誤時83 84 85 {IHTMLDocument3 方法:}86 recalc //重新獲取文檔中的全部動態(tài)屬性87 releaseCapture //釋放文檔中對象的鼠標(biāo)捕捉88 89 {IHTMLDocument3 屬性:}90 createTextNode //建立文本91 attachEvent //綁定事件92 detachEvent //取消事件綁定93 createDocumentFragment//創(chuàng)建新文檔94 getElementsByName //返回帶有指定 name 的對象集合95 getElementById //返回指定 id 的第一個對象的引用96 getElementsByTagName //返回指定標(biāo)簽的對象集合97 documentElement //獲取文檔根部節(jié)點(diǎn), 也就是 HTML 節(jié)點(diǎn)98 uniqueID //獲取對象的唯一標(biāo)識符99 dir // 100 parentDocument // 101 enableDownload 102 baseUrl //基礎(chǔ)地址 103 104 {IHTMLDocument3 事件:} 105 onrowsdelete //當(dāng)前數(shù)據(jù)記錄被刪除時 106 onrowsinserted //當(dāng)前數(shù)據(jù)源將要插入新數(shù)據(jù)時 107 oncellchange //當(dāng)數(shù)據(jù)來源發(fā)生變化時 108 ondatasetchanged //數(shù)據(jù)源發(fā)生變化時 109 ondataavailable //當(dāng)數(shù)據(jù)接收完成時 110 ondatasetcomplete //當(dāng)數(shù)據(jù)源的全部有效數(shù)據(jù)讀取完畢時 111 onpropertychange //改變屬性時 112 oncontextmenu //當(dāng)按下鼠標(biāo)右鍵出現(xiàn)菜單或通過鍵盤觸發(fā)頁面菜單時 113 onstop //停止或離開時 114 115 116 {IHTMLDocument4 方法:} 117 focus //獲得輸入焦點(diǎn) 118 hasFocus //是否擁有輸入焦點(diǎn) 119 createDocumentFromUrl //下載指定網(wǎng)頁并返回其 IHTMLDocument2 接口 120 CreateEventObject //生成當(dāng)使用 fireEvent 方法時用于傳遞事件相關(guān)信息的 event 對象 ??? 121 FireEvent 122 123 {IHTMLDocument4 屬性:} 124 createRenderStyle //建立 IHTMLRenderStyle 接口 125 namespaces // 126 media // 127 URLUnencoded //獲取去除字符編碼的 URL 128 129 {IHTMLDocument4 事件:} 130 onselectionchange //改變選擇時 131 oncontrolselect //好像是框選時 132 133 134 {IHTMLDocument5 方法:} 135 createAttribute //建立指定 name 的屬性 136 createComment //建立注釋 137 138 {IHTMLDocument5 屬性:} 139 doctype //文檔類型 140 implementation_ //獲取 IHTMLDOMImplementation 接口 141 compatMode //兼容模式, 返回 BackCompat、CSS1Compat 等 142 143 {IHTMLDocument5 事件:} 144 onmousewheel //旋轉(zhuǎn)鼠標(biāo)滾動輪時 145 onfocusin //獲取輸入焦點(diǎn)時 146 onfocusout //失去輸入焦點(diǎn)時 147 onactivate //激活時 148 ondeactivate //當(dāng)前對象變?yōu)楦肝臋n的其他對象時 149 onbeforeactivate //onactivate 前 150 onbeforedeactivate //ondeactivate 前 151 152 Ihtmldocument2接口的使用 153 MSHTML是微軟公司的一個COM組件,該組件封裝了HTML語言中的所有元素及其屬性,通過其提供的標(biāo)準(zhǔn)接口,可以訪問指定網(wǎng)頁的所有元素. 154 MSHTML對象模型是由一些對象和集合組成的.處于根部的是HTML,描述了打開頁面的1個窗口,包括一系列集合和對象。如Frames集合,History,Location,Navigator,Document,Vi—sum,Event對象等.其中描述呈現(xiàn)在客戶窗口實(shí)際網(wǎng)頁的是Document對象。由一系列的屬性、方法、對象和集合組成.其中All集合中包含網(wǎng)頁中所有標(biāo)記(Tag)元素,其主要的方法和屬性有: 155 (1)Length(長度):即標(biāo)記出現(xiàn)的個數(shù),可以把標(biāo)記的集合理解為從0開始的一維數(shù)組,其次序按照標(biāo)記在網(wǎng)頁位置排列; 156 (2)Tags(標(biāo)記):用于過濾出給定標(biāo)記的集合,如Doc.Al1.Tags(P)得到所有分段標(biāo)記P; 157 (3)Item(項(xiàng)目):用于選擇集合中的某1個元素,如object.item(0)得到集合的第1個元素,而object.item(i)得到第i+1個元素. 158 此外,IHTMLElement也是個常用的集合對象,代表網(wǎng)頁中指定標(biāo)記的集合,通過這個集合對象,可以得到網(wǎng)頁上特定標(biāo)記的內(nèi)容.IHTMLElement有4個主要屬性: 159 (1)InnerText:開始標(biāo)記和結(jié)束標(biāo)記之間的文本; 160 (2)InnerHTML:開始標(biāo)記和結(jié)束標(biāo)記之間的文本和HTML; 161 (3)OuterText:對象的文本; 162 (4)OuterHTML:對象的文本和HTML. 163 注意:使用前加入單元mshtml 164 演示表單提交 165 procedure TForm1.Button1Click(Sender: TObject); 166 var 167 Doc:IHTMLDocument2; 168 input:OleVariant; 169 userinputelement,pwdinputelement:ihtmlinputelement; 170 begin 171 doc:=webbrowser1.document as ihtmldocument2; 172 userinputelement:=(doc.all.item('user'(也就是網(wǎng)頁中用戶名控件的名字),0) as ihtmlinputelement); 173 userinputelement.value:=edit1.text;(也就是你要向網(wǎng)頁輸入的東西) 174 pwdinputelement:=(doc.all.item('password',0) as ihtmlinputelement); 175 pwdinputelement.value:=edit2.text; 176 input:=doc.all.item('submit',0); 177 input.click; 178 end; 179 當(dāng)提交數(shù)據(jù)按鈕沒有NAME屬性時,采用如下方法: 180 procedure TForm1.Button1Click(Sender: TObject); 181 var 182 Doc:IHTMLDocument2; 183 form:ithmlformelement; 184 userinputelement,pwdinputelement:ihtmlinputelement; 185 begin 186 doc:=webbrowser1.document as ihtmldocument2; 187 userinputelement:=(doc.all.item('user'(也就是網(wǎng)頁中用戶名控件的名字),0) as ihtmlinputelement); 188 userinputelement.value:=edit1.text;(也就是你要向網(wǎng)頁輸入的東西) 189 pwdinputelement:=(doc.all.item('password',0) as ihtmlinputelement); 190 pwdinputelement:=edit2.text; 191 form:=(doc.all.item('login_form',0) as ihtmlformelement): 192 form.submit; 193 end; 194 登錄"按鈕一般都是網(wǎng)頁中默認(rèn)的回車按鈕,所以可以用上面代碼來代替前面的點(diǎn)擊按鈕 195 讀取某網(wǎng)頁內(nèi)容 196 在創(chuàng)建窗體的時候打開一網(wǎng)頁 197 198 webbrowser1.navigate('http://www.baidu.com') 199 procedure TForm1.Button1Click(Sender: TObject); 200 begin 201 memo1.lines.add(ihtmldocument2(webbrowser1.document).body.outerhtml); 202 end; 203 204 這樣一來memo1中顯示www.baidu.com的html 205 利用webbrowser1控件瀏覽某txt中文件的內(nèi)容,并以html形式來顯示出來 206 在webbrowser1的控件中的ondocumentcomplete事件加入如下代碼 207 procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; 208 const pDisp: IDispatch; var URL: OleVariant); 209 var 210 webdoc:htmldocument ; 211 webbody:htmlbody; 212 begin 213 webdoc:=webbrowser1.document as htmldocument; 214 webbody:=webdoc.body as htmlbody; 215 webbody.insertAdjacentHTML('beforeend','<form method="POST" action="">'); 216 webbody.insertAdjacentHTML('beforeend','Password: '); 217 webbody.insertAdjacentHTML('beforeend','<input type="password" >'); 218 webbody.insertAdjacentHTML('beforeend','<input type="submit" value="LOGIN" >'); 219 webbody.insertAdjacentHTML('beforeend',' '); 220 webbody.insertAdjacentHTML('beforeend','</form>'); 221 end; 222 223 224 讀取的時候,webbrowser控件得要完整的打開某一網(wǎng)頁,
#IHTMLDocument2 屬性說明
{IHTMLDocument2 方法:}3 write //寫入4 writeln //寫入并換行5 open //打開一個流,以收集 document.write 或 document.writeln 的輸出6 close //關(guān)閉并輸出用 document.open 方法打開的流7 clear //清空文檔中所有元素, 可能暫時不支持8 queryCommandSupported //是否支持指定命令9 queryCommandEnabled //指定命令是否有效10 queryCommandState //是否設(shè)置了指定命令11 queryCommandIndeterm //指定命令是否處于不確定狀態(tài)12 queryCommandText //命令13 queryCommandValue //命令值14 execCommand //執(zhí)行命令15 execCommandShowHelp //命令幫助信息16 createElement //建立元素17 elementFromPoint //指定坐標(biāo)下的元素18 createStyleSheet //創(chuàng)建一個樣式表19 20 {IHTMLDocument2 屬性:}21 all //所有 HTML 元素的集合22 images //所有 image 對象的集合23 applets //所有 applet 對象的集合24 links //所有 link 對象的集合25 forms //所有 form 對象的集合26 anchors //所有 anchor 對象的集合27 scripts //所有 script 對象的集合28 frames //所有 frame 對象的集合, 這應(yīng)該和 window.frames 是一樣的29 embeds //所有 embed 對象的集合30 plugins //所有 plugin 對象的集合31 styleSheets //樣式表集合32 title //網(wǎng)頁標(biāo)題33 body //body 對象34 activeElement //當(dāng)前具有輸入焦點(diǎn)的元素35 designMode //設(shè)計(jì)模式, 可選值: Inherit On Off36 selection //當(dāng)前選擇37 readyState //頁面讀取狀態(tài): uninitialized、loading、loaded、interactive、completed38 linkColor //鏈接的顏色39 alinkColor //活動鏈接的顏色40 vlinkColor //已訪問過的鏈接的顏色41 bgColor //背景顏色42 fgColor //文本顏色43 referrer //返回載入當(dāng)前頁面的頁面的 URL44 location //同 window.location45 lastModified //返回文檔最后修改的日期和時間46 url //返回當(dāng)前文檔的 URL47 domain //返回域名48 cookie //返回文檔相關(guān)的 cookie49 expando //可設(shè)置與獲取的布爾值, 是否允許擴(kuò)展50 charset //獲取文檔的字符集名稱51 defaultCharset //獲取瀏覽器默認(rèn)的字符集名稱52 mimeType //文檔類型53 fileSize //文檔大小, 單位字節(jié), 用字符串表示54 fileCreatedDate //文檔建立時間 ???55 fileModifiedDate //文檔修改時間 ???56 fileUpdatedDate //文檔更新時間 ???57 security //文檔安全58 protocol //傳輸協(xié)議59 nameProp //文件標(biāo)題, 好像同 Title60 parentWindow //父窗口61 Script //62 63 {IHTMLDocument2 事件:}64 onhelp //用 F1 獲取幫助時65 onclick //單擊時66 ondblclick //雙擊時67 onmousedown //鼠標(biāo)點(diǎn)下時68 onmouseup //鼠標(biāo)抬起時69 onmousemove //鼠標(biāo)移動時70 onmouseover //鼠標(biāo)進(jìn)入時71 onmouseout //鼠標(biāo)離開時72 onkeypress //按鍵時73 onkeyup //鍵按住時74 onkeydown //鍵抬起時75 onreadystatechange //狀態(tài)改變時76 onrowexit //當(dāng)前數(shù)據(jù)源的數(shù)據(jù)將要發(fā)生變化時77 onrowenter //當(dāng)前數(shù)據(jù)源的數(shù)據(jù)發(fā)生變化并且有新的數(shù)據(jù)時78 ondragstart //開始拖動時79 onselectstart //開始選擇時80 onbeforeupdate //更新前81 onafterupdate //更新后82 onerrorupdate //更新錯誤時83 84 85 {IHTMLDocument3 方法:}86 recalc //重新獲取文檔中的全部動態(tài)屬性87 releaseCapture //釋放文檔中對象的鼠標(biāo)捕捉88 89 {IHTMLDocument3 屬性:}90 createTextNode //建立文本91 attachEvent //綁定事件92 detachEvent //取消事件綁定93 createDocumentFragment//創(chuàng)建新文檔94 getElementsByName //返回帶有指定 name 的對象集合95 getElementById //返回指定 id 的第一個對象的引用96 getElementsByTagName //返回指定標(biāo)簽的對象集合97 documentElement //獲取文檔根部節(jié)點(diǎn), 也就是 HTML 節(jié)點(diǎn)98 uniqueID //獲取對象的唯一標(biāo)識符99 dir // 100 parentDocument // 101 enableDownload 102 baseUrl //基礎(chǔ)地址 103 104 {IHTMLDocument3 事件:} 105 onrowsdelete //當(dāng)前數(shù)據(jù)記錄被刪除時 106 onrowsinserted //當(dāng)前數(shù)據(jù)源將要插入新數(shù)據(jù)時 107 oncellchange //當(dāng)數(shù)據(jù)來源發(fā)生變化時 108 ondatasetchanged //數(shù)據(jù)源發(fā)生變化時 109 ondataavailable //當(dāng)數(shù)據(jù)接收完成時 110 ondatasetcomplete //當(dāng)數(shù)據(jù)源的全部有效數(shù)據(jù)讀取完畢時 111 onpropertychange //改變屬性時 112 oncontextmenu //當(dāng)按下鼠標(biāo)右鍵出現(xiàn)菜單或通過鍵盤觸發(fā)頁面菜單時 113 onstop //停止或離開時 114 115 116 {IHTMLDocument4 方法:} 117 focus //獲得輸入焦點(diǎn) 118 hasFocus //是否擁有輸入焦點(diǎn) 119 createDocumentFromUrl //下載指定網(wǎng)頁并返回其 IHTMLDocument2 接口 120 CreateEventObject //生成當(dāng)使用 fireEvent 方法時用于傳遞事件相關(guān)信息的 event 對象 ??? 121 FireEvent 122 123 {IHTMLDocument4 屬性:} 124 createRenderStyle //建立 IHTMLRenderStyle 接口 125 namespaces // 126 media // 127 URLUnencoded //獲取去除字符編碼的 URL 128 129 {IHTMLDocument4 事件:} 130 onselectionchange //改變選擇時 131 oncontrolselect //好像是框選時 132 133 134 {IHTMLDocument5 方法:} 135 createAttribute //建立指定 name 的屬性 136 createComment //建立注釋 137 138 {IHTMLDocument5 屬性:} 139 doctype //文檔類型 140 implementation_ //獲取 IHTMLDOMImplementation 接口 141 compatMode //兼容模式, 返回 BackCompat、CSS1Compat 等 142 143 {IHTMLDocument5 事件:} 144 onmousewheel //旋轉(zhuǎn)鼠標(biāo)滾動輪時 145 onfocusin //獲取輸入焦點(diǎn)時 146 onfocusout //失去輸入焦點(diǎn)時 147 onactivate //激活時 148 ondeactivate //當(dāng)前對象變?yōu)楦肝臋n的其他對象時 149 onbeforeactivate //onactivate 前 150 onbeforedeactivate //ondeactivate 前 151 152 Ihtmldocument2接口的使用 153 MSHTML是微軟公司的一個COM組件,該組件封裝了HTML語言中的所有元素及其屬性,通過其提供的標(biāo)準(zhǔn)接口,可以訪問指定網(wǎng)頁的所有元素. 154 MSHTML對象模型是由一些對象和集合組成的.處于根部的是HTML,描述了打開頁面的1個窗口,包括一系列集合和對象。如Frames集合,History,Location,Navigator,Document,Vi—sum,Event對象等.其中描述呈現(xiàn)在客戶窗口實(shí)際網(wǎng)頁的是Document對象。由一系列的屬性、方法、對象和集合組成.其中All集合中包含網(wǎng)頁中所有標(biāo)記(Tag)元素,其主要的方法和屬性有: 155 (1)Length(長度):即標(biāo)記出現(xiàn)的個數(shù),可以把標(biāo)記的集合理解為從0開始的一維數(shù)組,其次序按照標(biāo)記在網(wǎng)頁位置排列; 156 (2)Tags(標(biāo)記):用于過濾出給定標(biāo)記的集合,如Doc.Al1.Tags(P)得到所有分段標(biāo)記P; 157 (3)Item(項(xiàng)目):用于選擇集合中的某1個元素,如object.item(0)得到集合的第1個元素,而object.item(i)得到第i+1個元素. 158 此外,IHTMLElement也是個常用的集合對象,代表網(wǎng)頁中指定標(biāo)記的集合,通過這個集合對象,可以得到網(wǎng)頁上特定標(biāo)記的內(nèi)容.IHTMLElement有4個主要屬性: 159 (1)InnerText:開始標(biāo)記和結(jié)束標(biāo)記之間的文本; 160 (2)InnerHTML:開始標(biāo)記和結(jié)束標(biāo)記之間的文本和HTML; 161 (3)OuterText:對象的文本; 162 (4)OuterHTML:對象的文本和HTML. 163 注意:使用前加入單元mshtml 164 演示表單提交 165 procedure TForm1.Button1Click(Sender: TObject); 166 var 167 Doc:IHTMLDocument2; 168 input:OleVariant; 169 userinputelement,pwdinputelement:ihtmlinputelement; 170 begin 171 doc:=webbrowser1.document as ihtmldocument2; 172 userinputelement:=(doc.all.item('user'(也就是網(wǎng)頁中用戶名控件的名字),0) as ihtmlinputelement); 173 userinputelement.value:=edit1.text;(也就是你要向網(wǎng)頁輸入的東西) 174 pwdinputelement:=(doc.all.item('password',0) as ihtmlinputelement); 175 pwdinputelement.value:=edit2.text; 176 input:=doc.all.item('submit',0); 177 input.click; 178 end; 179 當(dāng)提交數(shù)據(jù)按鈕沒有NAME屬性時,采用如下方法: 180 procedure TForm1.Button1Click(Sender: TObject); 181 var 182 Doc:IHTMLDocument2; 183 form:ithmlformelement; 184 userinputelement,pwdinputelement:ihtmlinputelement; 185 begin 186 doc:=webbrowser1.document as ihtmldocument2; 187 userinputelement:=(doc.all.item('user'(也就是網(wǎng)頁中用戶名控件的名字),0) as ihtmlinputelement); 188 userinputelement.value:=edit1.text;(也就是你要向網(wǎng)頁輸入的東西) 189 pwdinputelement:=(doc.all.item('password',0) as ihtmlinputelement); 190 pwdinputelement:=edit2.text; 191 form:=(doc.all.item('login_form',0) as ihtmlformelement): 192 form.submit; 193 end; 194 登錄"按鈕一般都是網(wǎng)頁中默認(rèn)的回車按鈕,所以可以用上面代碼來代替前面的點(diǎn)擊按鈕 195 讀取某網(wǎng)頁內(nèi)容 196 在創(chuàng)建窗體的時候打開一網(wǎng)頁 197 198 webbrowser1.navigate('http://www.baidu.com') 199 procedure TForm1.Button1Click(Sender: TObject); 200 begin 201 memo1.lines.add(ihtmldocument2(webbrowser1.document).body.outerhtml); 202 end; 203 204 這樣一來memo1中顯示www.baidu.com的html 205 利用webbrowser1控件瀏覽某txt中文件的內(nèi)容,并以html形式來顯示出來 206 在webbrowser1的控件中的ondocumentcomplete事件加入如下代碼 207 procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; 208 const pDisp: IDispatch; var URL: OleVariant); 209 var 210 webdoc:htmldocument ; 211 webbody:htmlbody; 212 begin 213 webdoc:=webbrowser1.document as htmldocument; 214 webbody:=webdoc.body as htmlbody; 215 webbody.insertAdjacentHTML('beforeend','<form method="POST" action="">'); 216 webbody.insertAdjacentHTML('beforeend','Password: '); 217 webbody.insertAdjacentHTML('beforeend','<input type="password" >'); 218 webbody.insertAdjacentHTML('beforeend','<input type="submit" value="LOGIN" >'); 219 webbody.insertAdjacentHTML('beforeend',' '); 220 webbody.insertAdjacentHTML('beforeend','</form>'); 221 end; 222 223 224 讀取的時候,webbrowser控件得要完整的打開某一網(wǎng)頁,
?
1 document.execCommand()方法處理Html數(shù)據(jù)時常用語法格式 如下:2 復(fù)制內(nèi)容到剪貼板 3 代碼 :4 document.execCommand(sCommand[,交互方式, 動態(tài)參數(shù) ]) 5 其中:sCommand為指令參數(shù)(如下例中的"2D-Position"),交互方式參數(shù)如果是true的話將顯示對話框,如果為false的話,則不顯示對話框(下例中的"false"即表示不顯示對話框),動態(tài)參數(shù)一般為一可用值或?qū)傩?值(如下例中的"true")。?6 7 document.execCommand("2D-Position","false","true");?8 9 10 調(diào)用execCommand()可以實(shí)現(xiàn)瀏覽器菜單 的很多功能. 如保存文件 ,打開新文件,撤消、重做操作...等等. 有了這個方法,就可以很容易的實(shí)現(xiàn)網(wǎng)頁中的文本編輯器.?11 12 如果靈活運(yùn)用,可以很好的輔助我們完成各種項(xiàng)目.?13 14 使用的例子如下:?15 16 17 1、〖全選〗命令的實(shí)現(xiàn)?18 [格式]:document.execCommand("selectAll")19 [說明]將選種網(wǎng)頁中的全部內(nèi)容!20 [舉例]在<body></body>之間加入:21 <a href="#" οnclick=document.execCommand("selectAll")>全選</a>?22 23 2、〖打開〗命令的實(shí)現(xiàn)24 [格式]:document.execCommand("open")25 [說明]這跟VB等編程設(shè)計(jì)中的web browser控件中的命令有些相似,大家也可依此琢磨琢磨。26 [舉例]在<body></body>之間加入:27 <a href="#" οnclick=document.execCommand("open")>打開</a>28 29 3、〖另存為〗命令的實(shí)現(xiàn)?30 [格式]:document.execCommand("saveAs")31 [說明]將該網(wǎng)頁保存到本地盤的其它目錄!32 [舉例]在<body></body>之間加入:33 <a href="#" οnclick=document.execCommand("saveAs")>另存為</a>34 35 4、〖打印〗命令的實(shí)現(xiàn)?36 [格式]:document.execCommand("print")37 [說明]當(dāng)然,你必須裝了打印機(jī)!38 [舉例]在<body></body>之間加入:39 <a href="#" οnclick=document.execCommand("print")>打印</a>40 41 Js代碼 ?下面列出的是指令參數(shù)及意義42 43 //相當(dāng)于單擊文件中的打開按鈕?44 document.execCommand("Open");?45 ??46 //將當(dāng)前頁面 另存為?47 document.execCommand("SaveAs");?48 ??49 //剪貼選中的文字到剪貼板;?50 document.execCommand("Cut","false",null);?51 ??52 //刪除選中的文字;?53 document.execCommand("Delete","false",null);?54 ??55 //改變選中區(qū)域的字體;?56 document.execCommand("FontName","false",sFontName);?57 ??58 //改變選中區(qū)域的字體大小;?59 document.execCommand("FontSize","false",sSize|iSize);?60 ??61 //設(shè)置前景顏色;?62 document.execCommand("ForeColor","false",sColor);?63 ??64 //使絕對定位的對象可直接拖動;?65 document.execCommand("2D-Position","false","true");?66 ??67 //使對象定位變成絕對定位;?68 document.execCommand("AbsolutePosition","false","true");?69 ??70 //設(shè)置背景顏色;?71 document.execCommand("BackColor","false",sColor);?72 ??73 //使選中區(qū)域的文字加粗;?74 document.execCommand("Bold","false",null);?75 ??76 //復(fù)制選中的文字到剪貼板;?77 document.execCommand("Copy","false",null);?78 ??79 //設(shè)置指定錨點(diǎn)為書簽;?80 document.execCommand("CreateBookmark","false",sAnchorName);?81 ??82 //將選中文 本變成超連接,若第二個參數(shù)為true,會出現(xiàn)參數(shù)設(shè)置對話框;?83 document.execCommand("CreateLink","false",sLinkURL);?84 ??85 //設(shè)置當(dāng)前塊的標(biāo)簽名;?86 document.execCommand("FormatBlock","false",sTagName);??87 88 //相當(dāng)于單擊文件中的打開按鈕89 document.execCommand("Open");90 91 //將當(dāng)前頁面另存為92 document.execCommand("SaveAs");93 94 //剪貼選中的文字到剪貼板;95 document.execCommand("Cut","false",null);96 97 //刪除選中的文字;98 document.execCommand("Delete","false",null);?99 100 //改變選中區(qū)域的字體; 101 document.execCommand("FontName","false",sFontName);? 102 103 //改變選中區(qū)域的字體大小; 104 document.execCommand("FontSize","false",sSize|iSize);? 105 106 //設(shè)置前景顏色; 107 document.execCommand("ForeColor","false",sColor); 108 109 //使絕對定位的對象可直接拖動;? 110 document.execCommand("2D-Position","false","true"); 111 112 //使對象定位變成絕對定位;? 113 document.execCommand("AbsolutePosition","false","true"); 114 115 //設(shè)置背景顏色; 116 document.execCommand("BackColor","false",sColor); 117 118 //使選中區(qū)域的文字加粗; 119 document.execCommand("Bold","false",null); 120 121 //復(fù)制選中的文字到剪貼板; 122 document.execCommand("Copy","false",null); 123 124 //設(shè)置指定錨點(diǎn)為書簽; 125 document.execCommand("CreateBookmark","false",sAnchorName); 126 127 //將選中文本變成超連接,若第二個參數(shù)為true,會出現(xiàn)參數(shù)設(shè)置對話框; 128 document.execCommand("CreateLink","false",sLinkURL); 129 130 //設(shè)置當(dāng)前塊的標(biāo)簽名; 131 document.execCommand("FormatBlock","false",sTagName); 132 document對象execCommand通常在IE中在線處理Html數(shù)據(jù)時非常有用,它可以讓你輕而易舉實(shí)現(xiàn)文字的加粗、加顏色、加字體等一系列的命令。 133 ??? D-Position 允許通過拖曳移動絕對定位的對象。? 134 ??? AbsolutePosition 設(shè)定元素的 position 屬性為“absolute”(絕對)。? 135 ??? BackColor 設(shè)置或獲取當(dāng)前選中區(qū)的背景顏色。? 136 ??? BlockDirLTR 目前尚未支持。? 137 ??? BlockDirRTL 目前尚未支持。? 138 ??? Bold 切換當(dāng)前選中區(qū)的粗體顯示與否。? 139 ??? BrowseMode 目前尚未支持。? 140 ??? Copy 將當(dāng)前選中區(qū)復(fù)制到剪貼板。? 141 ??? CreateBookmark 創(chuàng)建一個書簽錨或獲取當(dāng)前選中區(qū)或插入點(diǎn)的書簽錨的名稱。? 142 ??? CreateLink 在當(dāng)前選中區(qū)上插入超級鏈接,或顯示一個對話框允許用戶指定要為當(dāng)前選中區(qū)插入的超級鏈接的 URL。? 143 ??? Cut 將當(dāng)前選中區(qū)復(fù)制到剪貼板并刪除之。? 144 ??? Delete 刪除當(dāng)前選中區(qū)。? 145 ??? DirLTR 目前尚未支持。? 146 ??? DirRTL 目前尚未支持。? 147 ??? EditMode 目前尚未支持。? 148 ??? FontName 設(shè)置或獲取當(dāng)前選中區(qū)的字體。? 149 ??? FontSize 設(shè)置或獲取當(dāng)前選中區(qū)的字體大小。? 150 ??? ForeColor 設(shè)置或獲取當(dāng)前選中區(qū)的前景(文本)顏色。? 151 ??? FormatBlock 設(shè)置當(dāng)前塊格式化標(biāo)簽。? 152 ??? Indent 增加選中文本的縮進(jìn)。? 153 ??? InlineDirLTR 目前尚未支持。? 154 ??? InlineDirRTL 目前尚未支持。? 155 ??? InsertButton 用按鈕控件覆蓋當(dāng)前選中區(qū)。? 156 ??? InsertFieldset 用方框覆蓋當(dāng)前選中區(qū)。? 157 ??? InsertHorizontalRule 用水平線覆蓋當(dāng)前選中區(qū)。? 158 ??? InsertIFrame 用內(nèi)嵌框架覆蓋當(dāng)前選中區(qū)。? 159 ??? InsertImage 用圖像覆蓋當(dāng)前選中區(qū)。? 160 ??? InsertInputButton 用按鈕控件覆蓋當(dāng)前選中區(qū)。? 161 ??? InsertInputCheckbox 用復(fù)選框控件覆蓋當(dāng)前選中區(qū)。? 162 ??? InsertInputFileUpload 用文件上載控件覆蓋當(dāng)前選中區(qū)。? 163 ??? InsertInputHidden 插入隱藏控件覆蓋當(dāng)前選中區(qū)。? 164 ??? InsertInputImage 用圖像控件覆蓋當(dāng)前選中區(qū)。? 165 ??? InsertInputPassword 用密碼控件覆蓋當(dāng)前選中區(qū)。? 166 ??? InsertInputRadio 用單選鈕控件覆蓋當(dāng)前選中區(qū)。? 167 ??? InsertInputReset 用重置控件覆蓋當(dāng)前選中區(qū)。? 168 ??? InsertInputSubmit 用提交控件覆蓋當(dāng)前選中區(qū)。? 169 ??? InsertInputText 用文本控件覆蓋當(dāng)前選中區(qū)。? 170 ??? InsertMarquee 用空字幕覆蓋當(dāng)前選中區(qū)。? 171 ?? InsertOrderedList 切換當(dāng)前選中區(qū)是編號列表還是常規(guī)格式化塊。? 172 ??? InsertParagraph 用換行覆蓋當(dāng)前選中區(qū)。? 173 ??? InsertSelectDropdown 用下拉框控件覆蓋當(dāng)前選中區(qū)。? 174 ??? InsertSelectListbox 用列表框控件覆蓋當(dāng)前選中區(qū)。? 175 ??? InsertTextArea 用多行文本輸入控件覆蓋當(dāng)前選中區(qū)。? 176 ??? InsertUnorderedList 切換當(dāng)前選中區(qū)是項(xiàng)目符號列表還是常規(guī)格式化塊。? 177 ??? Italic 切換當(dāng)前選中區(qū)斜體顯示與否。? 178 ??? JustifyCenter 將當(dāng)前選中區(qū)在所在格式化塊置中。? 179 ??? JustifyFull 目前尚未支持。? 180 ??? JustifyLeft 將當(dāng)前選中區(qū)所在格式化塊左對齊。? 181 ??? JustifyNone 目前尚未支持。? 182 ??? JustifyRight 將當(dāng)前選中區(qū)所在格式化塊右對齊。? 183 ??? LiveResize 迫使 MSHTML 編輯器在縮放或移動過程中持續(xù)更新元素外觀,而不是只在移動或縮放完成后更新。? 184 ??? MultipleSelection 允許當(dāng)用戶按住 Shift 或 Ctrl 鍵時一次選中多于一個站點(diǎn)可選元素。? 185 ??? Open 目前尚未支持。? 186 ??? Outdent 減少選中區(qū)所在格式化塊的縮進(jìn)。? 187 ??? OverWrite 切換文本狀態(tài)的插入和覆蓋。? 188 ??? Paste 用剪貼板內(nèi)容覆蓋當(dāng)前選中區(qū)。? 189 ??? PlayImage 目前尚未支持。? 190 ??? Print 打開打印對話框以便用戶可以打印當(dāng)前頁。? 191 ??? Redo 目前尚未支持。? 192 ??? Refresh 刷新當(dāng)前文檔。? 193 ??? RemoveFormat 從當(dāng)前選中區(qū)中刪除格式化標(biāo)簽。? 194 ??? RemoveParaFormat 目前尚未支持。? 195 ??? SaveAs 將當(dāng)前 Web 頁面保存為文件。? 196 ??? SelectAll 選中整個文檔。? 197 ??? SizeToControl 目前尚未支持。? 198 ??? SizeToControlHeight 目前尚未支持。? 199 ??? SizeToControlWidth 目前尚未支持。? 200 ??? Stop 目前尚未支持。? 201 ??? StopImage 目前尚未支持。? 202 ??? StrikeThrough 目前尚未支持。? 203 ??? Subscript 目前尚未支持。? 204 ??? Superscript 目前尚未支持。? 205 ??? UnBookmark 從當(dāng)前選中區(qū)中刪除全部書簽。? 206 ??? Underline 切換當(dāng)前選中區(qū)的下劃線顯示與否。? 207 ??? Undo 目前尚未支持。? 208 ??? Unlink 從當(dāng)前選中區(qū)中刪除全部超級鏈接。? 209 ??? Unselect 清除當(dāng)前選中區(qū)的選中狀態(tài)。 210 ? 211 ? 212 關(guān)于document.execCommand:??? 213 ????????? 要執(zhí)行編輯命令,可調(diào)用?? document.execCommand,并傳遞對應(yīng)于命令?? ID?? 的字符串。另外還有可選的第二個參數(shù) ,該參數(shù)指定如果可以應(yīng)用的話是否顯示此命令的用戶界面。傳遞整數(shù)?? 1?? 將顯示用戶界面,整數(shù)?? 0?? 將跳過它。這個參數(shù)通常不用于編輯命令。因?yàn)槟J(rèn)值為?? 0,所以假如您沒有使用第三個參數(shù)(在這種情況下,還必須為第二個參數(shù)傳遞值),一般可以不管它。第三個參數(shù)也是可選的, 在可應(yīng)用的情況下,使用它來將任何所需參數(shù)傳遞給該命令。 ?轉(zhuǎn)自:https://www.cnblogs.com/sekon/p/6517463.html
https://www.cnblogs.com/endv/p/5983110.html
轉(zhuǎn)載于:https://www.cnblogs.com/jasonzeng/p/9336269.html
總結(jié)
以上是生活随笔為你收集整理的WPF使用Webbrowser操作网页的主要代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装软件包的三种方法 ,rpm包介绍 ,
- 下一篇: 一个自我介绍