[转]WebBrowser中html元素如何触发winform事件

来源:互联网 发布:古柯淘宝 编辑:程序博客网 时间:2024/05/29 08:09

======================================================
注:本文源代码点此下载
======================================================

评论

1343809

#1楼[楼主]

2008-07-02 18:31

freeliver54

在.net中利用webbrowser控件实现winform与webform交互

在.net中的webbrowser 控件可以让你装载windows form 应用程序中的web 网页和其它采用浏览器的文件。可以使用webbrowser 控件将现有的web框架控制项加入至 windows form 客户端应用程序。

还是直接看代码吧。

webbrowser 控制项 提供的属性、方法和事件,可用来实现 internet explorer 的控制项

webbrowser1.navigate("www.itwis.com");//将指定位置处的文件载入至 webbrowser

webbrowser1.goback();//上一页

webbrowser1.goforward();//下一页

webbrowser1.refresh();//刷新

webbrowser1.gohome();//主页

这里提供了webbrowser常用的方法,

上面的代码是将 我们园子的主页载入到webbrowser控件中。如果我们想要在应用程式中产生自己的网页内容,可以设定documenttext属性。也可以通过document属性来处理目前的网页内容。如下代码是使用 documenttext 属性,显示网页内容。并用document属性来处理所显示的网页。

1private void btndocumenttext_click(object sender, eventargs e)

2{

3string szhtml = @"

4

5

6 documenttext

7

8

9

10please enter your name:

11

12send input to method of form class

13

14

15";

17webbrowser1.documenttext = szhtml;18

19}

20

21private void webbrowser1_navigating(object sender, webbrowsernavigatingeventargs e)

22{

23system.windows.forms.htmldocument document =this.webbrowser1.document;

24

25if (document != null && document.all["name"] != null && string.isnullorempty(document.all["name"].getattribute("value")))

26{

27e.cancel = true;

28system.windows.forms.messagebox.show("you must enter your name before you can navigate to " +e.url.tostring());

29}

30

31}

既然我们可以通过documenttext生成自己的网页,那么能不能象使用ie那样操作这个网页呢?,答案是肯定的,完全可以像操作web程序那样操作webbrowser 控制项。比如我们可以加入脚本,css。当然,如果你熟悉 html 物件对象模型 (dom),也可以透过 document 属性来处理目前的web网页内容。下面的例子加入了javascript脚本来控制网页。如果要在winfrom程序中写大量的javascriot代码,而且这些代码最终要转换成string型载入到webbrowser 那将是很痛苦的事情,不过没有关系,我们可以创建一个js文件,放入资源中,用的时候只需从资源中载入即可。这里我创建一个名为 clientscript.js 的文件。

1

2function clickevent(name)

3{

4alert("hello: " +name);

5}

6

7function keydown()

8{

9if (event.keycode==116)

10{

11event.keycode=0;

12event.returnvalue=false;

13}

14

15return false;

16}

string szclientscript = managedwebbrowser.properties.resources.resourcemanager.getstring("clientscript");

string szwebbrowsertext = "" +

"" +

16""+

17

18"please enter your name:

"+

19"

"+

20"click here"+

21"";

22

23

24webbrowser1.documenttext = szwebbrowsertext;

25}

26public void clickevent(string username)

27{

28// simply echo out the name that the user typed in the input box of the html page

29if (system.threading.thread.currentthread.currentuiculture.textinfo.isrighttoleft == true)

30messagebox.show("hello " + username, "managed web browser sample", messageboxbuttons.ok, messageboxicon.information, messageboxdefaultbutton.button1, messageboxoptions.rightalign | messageboxoptions.rtlreading);

31else

32messagebox.show("hello " + username, "managed web browser sample", messageboxbuttons.ok, messageboxicon.information, messageboxdefaultbutton.button1);

33

34}

这里的objectforscripting 属性设置为 this。注意:在from1 类的开头加入了这么一句[comvisible(true)], 它在system.runtime.interopservices下,预设值为 true,指出 managed 型别对于 com 为可见的。

[comvisible(true)]

public partial class form1 : system.windows.forms.form

结束语:

本文简单的介绍了 webbrowser 的用法,实现了winform程序下与web页面的交互使用。

本文来自: it知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/winform/20080629/1884_2.html

回复 引用 查看

#2楼[楼主]

2008-10-17 13:37

freeliver54

//webbrowser取得页面文本框的值

string strusername= this.webbrowser1.document.all["txt_username"].getattribute("value");

回复 引用 查看

#3楼[楼主]

2008-10-17 15:23

freeliver54

webbrowser 物件有一個屬性 iswebbrowsercontextmenuenabled 設定 false 就可以禁止右鍵

回复 引用 查看


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/