作者: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ǔ),加上金山詞霸即指即譯,足矣。
新聞熱點(diǎn)
疑難解答