国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

WebBrowser控件捕捉DHTML事件

2024-08-26 00:15:35
字體:
供稿:網(wǎng)友

作者:tuenhai.com msn: king#tuenhai.com

版權(quán)聲明:可以任意轉(zhuǎn)載,轉(zhuǎn)載時(shí)請(qǐng)務(wù)必以超鏈接形式標(biāo)明文章原始出處和作者信息及本聲明
http://www.tuenhai.com/

   開發(fā)工具:microsoft visual studio .net 2003
   操作系統(tǒng):windows xp

  原文:http://www.devx.com/vb2themax/tip/18798

  和其他控件一樣,我們可以用webbrowser控件來構(gòu)筑windows form應(yīng)用程序。從工具箱中選擇windows 窗體控件組,單擊“microsoft web 瀏覽器”,visual studio .net 在后臺(tái)使用aximp.exe工具創(chuàng)建activex 控件,控件名字為“axwebbrowser”。在vb.net中,不能直接使用com組件,com都是unmanaged code,在vb.net中使用這些組件,必須完成從unmanaged code到managed code的轉(zhuǎn)換。
   一般地,你可以像使用原來的webbrowser控件一樣,如call 方法,指定屬性,捕捉事件等。
   有些事情并不是那么簡單的。我們要捕捉頁面事件,如當(dāng)用戶點(diǎn)擊頁面元素(如背景)時(shí),引發(fā)頁面元素的onclick事件。發(fā)果沒有捕捉到事件,就要提升dhtml的等級(jí),直到document對(duì)象的最高層次。這樣,我們就能捕捉到任何事件了。在vb6中,我們可以簡單地用withevents關(guān)鍵詞指定webbrowser.document到mshtml.htmldocument。
   在vb.net中,這個(gè)簡單方法不再有效。因?yàn)閍ctivex控件創(chuàng)建了兩個(gè)接口,兩個(gè)接口中使用了同樣的方法名,導(dǎo)致出現(xiàn)運(yùn)行時(shí)錯(cuò)誤。所以,你必須明確指定document對(duì)象使用的接口,并創(chuàng)建事件處理句柄(呵呵,tuenhai翻譯得還不錯(cuò)吧)。

  以下是示例代碼:

' important: this code assumes that you've added a reference to the
' microsoft html object library type library

private sub form1_load(byval sender as system.object, _
byval e as system.eventargs) handles mybase.load
axwebbrowser1.navigate("http://localhost/default.asp")
end sub

private sub axwebbrowser1_navigatecomplete2(byval sender as object, _
byval e as axshdocvw.dwebbrowserevents2_navigatecomplete2event) handles _
axwebbrowser1.navigatecomplete2
' must wait for this event to grab a valid refernece to the document
' property
dim doc as mshtml.htmldocument = directcast(axwebbrowser1.document, _
mshtml.htmldocument)

' cast to the interface that defines the event you're interested in
dim docevents as mshtml.htmldocumentevents2_event = directcast(doc, _
mshtml.htmldocumentevents2_event)
' define a handler to the onclick event
addhandler docevents.onclick, addressof onclickproc
end sub

' notice that the signature of this event is different from usual, as it
' is expected to return a boolean - if false the default effect associated
' with the event (for example, jumping to another page if the click is on
' an hyperlink) is canceled.

private function onclickproc(byval obj as mshtml.ihtmleventobj) as boolean
' an object on the page has been clicked - you can learn more about
' type and position of this object by querying the obj's properties
' ...
end function

譯者注:
   這是tuenhai的第一篇譯稿。
   個(gè)人心得,近幾日在國外有關(guān)程序設(shè)計(jì)網(wǎng)站轉(zhuǎn)悠,得益良多。又想到書法學(xué)習(xí)的“取法乎上”。共享軟件的出路在于走向國際。軟件設(shè)計(jì)的學(xué)習(xí)又何嘗不是這樣呢?國際的學(xué)習(xí)資源相比國內(nèi)的學(xué)習(xí)資源如何?
   english決不是障礙。tuenhai不相信自己的english會(huì)比您好。初中基礎(chǔ),加上金山詞霸即指即譯,足矣。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 庆城县| 汉寿县| 开平市| 沂源县| 成都市| 乐都县| 乌鲁木齐县| 通道| 乐平市| 类乌齐县| 铅山县| 泸溪县| 玉林市| 巴南区| 康乐县| 巫溪县| 康平县| 太白县| 兰西县| 碌曲县| 奉贤区| 于都县| 华亭县| 栾川县| 锦屏县| 阿克陶县| 东海县| 黄平县| 光泽县| 福安市| 东丰县| 崇礼县| 天镇县| 紫阳县| 都匀市| 金塔县| 玛纳斯县| 峨山| 浮梁县| 怀柔区| 天祝|