AxWebBrowser的WindowClosing事件无法触发!请教高手解决办法
本人用AxWebBrowser控件实现了一个浏览器,发现无法触发WindowClosing事件。
不知道为什么。请教高手。
如果这种方法不行的话,还有没有什么好的解决办法。
谢谢
问题点数:100、回复次数:31Top
1 楼misvcom(零下一度)回复于 2006-03-05 16:03:26 得分 100
http://spaces.msn.com/yeweis/Blog/cns!1pzbzDqGFL-ryfS2jj6VsOSg!134.entryTop
2 楼misvcom(零下一度)回复于 2006-03-05 16:05:35 得分 0
axWebBrowser.WindowClosing Event
最近在研究WebBrowser控件,发现一篇文章谈到windowclosing事件的触发问题:
http://www.kbcafe.com/iBLOGthere4iM/?guid=20040501150250
摘录如下:
If u implement axWebBrowser, you'll find the WindowClosing event doesn't fire. This is a workaround, which I confirm works.
1. Right below the System.Windows.Forms.Form class add another class whichderives from SHDocVw.DWebBrowserEvents2. For example:\
public class IEEvents: SHDocVw.DWebBrowserEvents2
{}
2. Save the file and go to class view (View | Class View menu option). Go to IEEvents class in the tree view and expand it. Keep expanding its children till you see 'DWebBrowserEvents'. Right click and select 'Add | Implement interfaces' menu option.
3. A method for WindowClosing event should be generated by above step. Apply the 'DispId' attribute to the method as shown below:
[DispId(0x00000107)]
public void WindowClosing(bool IsChildWindow, ref bool Cancel)
{
//message box to the event handler works
MessageBox.Show("Closing Event", "IE", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
4. Add the following lines of code to the end of the Forms 'InitializeComponent' method.
UCOMIConnectionPointContainer pConPtCon =
(UCOMIConnectionPointContainer)this.axWebBrowser1.GetOcx();
UCOMIConnectionPoint pConPt;
Guid guid = typeof(SHDocVw.DWebBrowserEvents2).GUID;
pConPtCon.FindConnectionPoint(ref guid, out pConPt);
IEEvents e = new IEEvents();
//make sure you declare private int dwCookie in the form class but outside this method
pConPt.Advise(e, out dwCookie);
5. Add the following lines of code to the beginning of the Forms Close handler method.
UCOMIConnectionPointContainer pConPtCon =
(UCOMIConnectionPointContainer)this.axWebBrowser1.GetOcx();
UCOMIConnectionPoint pConPt;
Guid guid = typeof(SHDocVw.DWebBrowserEvents2).GUID;
pConPtCon.FindConnectionPoint(ref guid, out pConPt);
pConPt.Unadvise(dwCookie);
试了一下,IEEvents class必须是public的,否则其它事件都会触发,唯独WindowClosing不会。让人想象是否M$犯的是不是类似的错误,:).(Note that IEvents class should be public, otherwise all the events will be fired exactly except WindowClosing event, this make me thought of the possibility of what M$ has done, ;))Top
3 楼xuleicsu()回复于 2006-03-05 16:12:26 得分 0
看不太懂啊,高手,能具体的教教我怎么做吗?Top
4 楼xuleicsu()回复于 2006-03-05 17:35:00 得分 0
我的开发环境是VS2003+.net1.1Top
5 楼misvcom(零下一度)回复于 2006-03-05 19:01:57 得分 0
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=7023178a-97e3-4d95-9dcb-d14a2ee357ebTop
6 楼xuleicsu()回复于 2006-03-05 20:28:49 得分 0
那该怎么搞呢?高手指点啊,我必须得触发这个事件Top
7 楼misvcom(零下一度)回复于 2006-03-05 21:29:14 得分 0
在定义使用AxWebBrowser控件的窗体的类里添加一个内部类:
public class IEEvents: SHDocVw.DWebBrowserEvents2
{}
保存后,点击视图的类视图选项。展开树节点,直到找到IEEvents下面的DWebBrowserEvents2,按右键,点击添加-》实现接口Top
8 楼misvcom(零下一度)回复于 2006-03-05 21:35:13 得分 0
找到public void WindowClosing(bool IsChildWindow, ref bool Cancel)这个方法
加上[DispId(0x00000107)]这个attribute
然后将下面这段代码添加到窗体的InitializeComponent方法末尾,记得先定义int dwCookie
UCOMIConnectionPointContainer pConPtCon =
(UCOMIConnectionPointContainer)this.axWebBrowser1.GetOcx();
UCOMIConnectionPoint pConPt;
Guid guid = typeof(SHDocVw.DWebBrowserEvents2).GUID;
pConPtCon.FindConnectionPoint(ref guid, out pConPt);
IEEvents e = new IEEvents();
//make sure you declare private int dwCookie in the form class but outside this method
pConPt.Advise(e, out dwCookie);
最后override窗体的Closed方法,在开始处加上下面的代码:
UCOMIConnectionPointContainer pConPtCon =
(UCOMIConnectionPointContainer)this.axWebBrowser1.GetOcx();
UCOMIConnectionPoint pConPt;
Guid guid = typeof(SHDocVw.DWebBrowserEvents2).GUID;
pConPtCon.FindConnectionPoint(ref guid, out pConPt);
pConPt.Unadvise(dwCookie);
这样就可以了Top
9 楼misvcom(零下一度)回复于 2006-03-05 21:38:09 得分 0
可以到http://www.fly99.com/index/teach/javascript_tx/cook23.htm这个网页上面做测试
还有记得加上using System.Runtime.InteropServices,否则用不了DispIdTop
10 楼xuleicsu()回复于 2006-03-05 23:03:52 得分 0
不好意思,上面的原文为:“5. Add the following lines of code to the beginning of the Forms Close handler method.”
但是,你翻译成“最后override窗体的Closed方法,在开始处加上下面的代码:”好像不妥。
窗体没有Closed方法,只有Close方法,但是不可以重载的。
上面英文所说的“Close handler method”到底是指哪一个方法啊?谢谢指点(是不是Dispose()方法啊)Top
11 楼misvcom(零下一度)回复于 2006-03-05 23:43:28 得分 0
事实上只是为了终止先前通过 Advise 建立的通知连接。
基本上只要在你不再需要的时候清,就可以了。
这里我是在关闭窗体时清的,所以就直接重写了OnClosed:
protected override void OnClosed(EventArgs e)Top
12 楼misvcom(零下一度)回复于 2006-03-05 23:46:35 得分 0
protected override void OnClosed(EventArgs e)
{
UCOMIConnectionPointContainer pConPtCon =
(UCOMIConnectionPointContainer)this.axWebBrowser1.GetOcx();
UCOMIConnectionPoint pConPt;
Guid guid = typeof(SHDocVw.DWebBrowserEvents2).GUID;
pConPtCon.FindConnectionPoint(ref guid, out pConPt);
pConPt.Unadvise(dwCookie);
base.OnClosed (e);
}Top
13 楼xuleicsu()回复于 2006-03-05 23:59:18 得分 0
按照你说的全部写好了,但是下面这个方法
[DispId(0x00000107)]
public void WindowClosing(bool IsChildWindow, ref bool Cancel)
{
//message box to the event handler works
MessageBox.Show("Closing Event", "IE", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
在测试时好像没有被调用,这是为什么?Top
14 楼xuleicsu()回复于 2006-03-06 00:08:34 得分 0
我在网上找到的新资料
http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms.controls/browse_thread/thread/b582c3b3800b1b8b/e496e22194cf497e?lnk=st&q=WebBrowser+WindowClosing+Event+C%23&rnum=2&hl=zh-CN#e496e22194cf497e
Hello,
Approximately 12/2002, I posted an issue with Framework
1.0 and the WebBrowser controls WindowClosing event. The
WindowClosing event was not being fired or sinked when
script executed window.close. A recommended solution
included implementing DWebBrowserEvents2 and adding the
WindowClosing DispId to the implementation. This took
care of the issue with .NET Framework 1.0. - Thank you.
The solution does not appear to work with Framework 1.1
and the WindowClosing event still does not fire. Also,
when window.close is executed via script the control
disappears and does not completely destroy itself.
Is there a workable C# solution or hotfix for Framework
1.1?
好像说的是上面这种方法在.NET Framework 1.1下,根本不起作用啊Top
15 楼panzi667(迅雷免费电影下载社区http://www.woyaola.net)回复于 2006-03-06 09:13:43 得分 0
能瞧瞧你的浏览器么?Top
16 楼xuleicsu()回复于 2006-03-06 09:56:27 得分 0
浏览器现在还有很多问题,调试好了再共享吧Top
17 楼misvcom(零下一度)回复于 2006-03-06 11:02:44 得分 0
你测试时有没有点击页面里面的“关闭窗体”?
我刚才试过了,是绝对可以的。点击“关闭窗体”它就会弹出Closing Event的对话框
你查到的资料的确没错,我早就告诉你这是一个Bug来的了,直到2005 beta 1都是一样见下面的连接:http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=7023178a-97e3-4d95-9dcb-d14a2ee357eb
就是按照正常情况它不会触发WindowClosing事件,所以才要按我提供的反方案来做啊Top
18 楼misvcom(零下一度)回复于 2006-03-06 11:04:23 得分 0
我就是在.NET 1.1下面做测试的Top
19 楼misvcom(零下一度)回复于 2006-03-06 11:07:48 得分 0
用我上面给你的这个网址;http://www.fly99.com/index/teach/javascript_tx/cook23.htm,来做测试。因为我看过它的脚本是正确的Top
20 楼xuleicsu()回复于 2006-03-06 20:10:28 得分 0
我现在试试Top
21 楼misvcom(零下一度)回复于 2006-03-06 20:27:02 得分 0
如果还是不行的话,就留一下邮件地址吧,我把我做测试时写的发过去给你
先自己试试吧Top
22 楼xuleicsu()回复于 2006-03-06 20:34:58 得分 0
非常感谢,谢谢你misvcom(零下一度) 。Top
23 楼xuleicsu()回复于 2006-03-06 20:37:51 得分 0
不用发了,刚测试了一下,没有问题了。再次感谢
以后有问题再向你请教吧,这个问题困扰了我很久,很感谢你Top
24 楼misvcom(零下一度)回复于 2006-03-06 20:41:11 得分 0
没什么的,互相帮助嘛^_^
结贴吧,我差20分就升级了^_^Top
25 楼misvcom(零下一度)回复于 2006-03-06 20:41:54 得分 0
麻烦你了^_^Top
26 楼xuleicsu()回复于 2006-03-06 20:44:14 得分 0
好,现在结贴
你有没有邮箱,下次有问题再请教你。Top
27 楼misvcom(零下一度)回复于 2006-03-06 20:46:30 得分 0
miracle@188.comTop
28 楼xuleicsu()回复于 2006-03-06 20:53:06 得分 0
还有一个问题就是,我做的浏览器是一个多页面的浏览器
好像在第一个窗口用window.close()脚本关闭页面时,能够触发WindowClosing事件
但是,在弹出窗口中却无法触发WindowClosing事件,直接就把窗口关闭了。
这是为什么啊?Top
29 楼misvcom(零下一度)回复于 2006-03-06 21:12:25 得分 0
那弹出的窗体是否同样有通过 Advise 建立通知连接?Top
30 楼misvcom(零下一度)回复于 2006-03-06 21:18:52 得分 0
你必须重新定义新建窗体的事件处理方法,用回你写的那个能正常激发WindowClosing事件的类的实例的窗体来打开,不能用一般单纯包含AxWebBrowse控件的窗体来打开,否则同样是不能激发WindowClosing事件的Top
31 楼xuleicsu()回复于 2006-03-06 21:20:21 得分 0
好的,试试看Top
相关问题
- 有没有简单的办法调试触发器?
- 标准事件触发问题,谁有办法?
- 有没有什么办法,不重启服务器就可以触发application_onstart?
- 有没有办法触发鼠标的右键,另存事件?(asp.net C#)
- asp.net中有办法让checkbox选中之后就触发事件吗?
- 触发器工作不正常,自动插入数据?求解决办法
- 请问:动态给checkbox组赋值,总要触发onclick事件,很影响速度.onclick事件只想在鼠标点击时触发,有没有办法.
- 触发器代码的优化,另外发现触发器动作频繁,系统内存占用严重,求解决办法
- 有谁知道什么办法可以触发combobox的KeyPress或KeyDown或KeyUp事件???
- 文本框中有没有办法实现即时触发判断输入值条件




