本人用AxWebBrowser控件实现了一个浏览器,发现无法触发WindowClosing事件。
不知道为什么。请教高手。
如果这种方法不行的话,还有没有什么好的解决办法。
谢谢
不知道为什么。请教高手。
如果这种方法不行的话,还有没有什么好的解决办法。
谢谢
解决方案 »
- C#页面身份验证怎么做,要从web.config里配置的
- 帮我解释一下网上找到的一篇文章
- c# webservice获得客户端ip
- 如何把前台JS取的值传到后台中并写入表?AJAX?
- 求一算法:足球比赛赛程的安排
- C# 程序 创建一个BAT文件 并自动填充内容
- 请教各位,如何将变量插入到在水晶报表中,谢谢
- 如何在页面中让页面后退,就与浏览器的后退功能一样?
- 百分求 asp.net 下载方法 在线等
- 各位大哥!关于asp.net服务器组件问题!!(再有就是结交郑州的朋友)
- 在一个web页面里有一个Table表,这个表中有很多的记录,我想在每5行记录后面空上一行,这在怎么写代码啊!
- 有什么方法,可以用程序把数据库备份到本地,网上的资料好象都是备份到服务器的路径的???
最近在研究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, ;))
{}保存后,点击视图的类视图选项。展开树节点,直到找到IEEvents下面的DWebBrowserEvents2,按右键,点击添加-》实现接口
(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);这样就可以了
但是,你翻译成“最后override窗体的Closed方法,在开始处加上下面的代码:”好像不妥。窗体没有Closed方法,只有Close方法,但是不可以重载的。上面英文所说的“Close handler method”到底是指哪一个方法啊?谢谢指点(是不是Dispose()方法啊)
{
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);
}
[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);
}
在测试时好像没有被调用,这是为什么?
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#e496e22194cf497eHello,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下,根本不起作用啊
好像在第一个窗口用window.close()脚本关闭页面时,能够触发WindowClosing事件但是,在弹出窗口中却无法触发WindowClosing事件,直接就把窗口关闭了。
这是为什么啊?