本人用AxWebBrowser控件实现了一个浏览器,发现无法触发WindowClosing事件。
不知道为什么。请教高手。
如果这种方法不行的话,还有没有什么好的解决办法。
谢谢

解决方案 »

  1.   

    http://spaces.msn.com/yeweis/Blog/cns!1pzbzDqGFL-ryfS2jj6VsOSg!134.entry
      

  2.   

    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, ;))
      

  3.   

    http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=7023178a-97e3-4d95-9dcb-d14a2ee357eb
      

  4.   

    在定义使用AxWebBrowser控件的窗体的类里添加一个内部类:public class IEEvents: SHDocVw.DWebBrowserEvents2
    {}保存后,点击视图的类视图选项。展开树节点,直到找到IEEvents下面的DWebBrowserEvents2,按右键,点击添加-》实现接口
      

  5.   

    找到public void WindowClosing(bool IsChildWindow, ref bool Cancel)这个方法加上[DispId(0x00000107)]这个attribute然后将下面这段代码添加到窗体的InitializeComponent方法末尾,记得先定义int dwCookieUCOMIConnectionPointContainer 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);这样就可以了
      

  6.   

    可以到http://www.fly99.com/index/teach/javascript_tx/cook23.htm这个网页上面做测试还有记得加上using System.Runtime.InteropServices,否则用不了DispId
      

  7.   

    不好意思,上面的原文为:“5. Add the following lines of code to the beginning of the Forms Close handler method.”
    但是,你翻译成“最后override窗体的Closed方法,在开始处加上下面的代码:”好像不妥。窗体没有Closed方法,只有Close方法,但是不可以重载的。上面英文所说的“Close handler method”到底是指哪一个方法啊?谢谢指点(是不是Dispose()方法啊)
      

  8.   

    事实上只是为了终止先前通过 Advise 建立的通知连接。基本上只要在你不再需要的时候清,就可以了。这里我是在关闭窗体时清的,所以就直接重写了OnClosed:protected override void OnClosed(EventArgs e)
      

  9.   

    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);
    }
      

  10.   

    按照你说的全部写好了,但是下面这个方法
    [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);
    }
    在测试时好像没有被调用,这是为什么?
      

  11.   

    我在网上找到的新资料
    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下,根本不起作用啊
      

  12.   

    你测试时有没有点击页面里面的“关闭窗体”?我刚才试过了,是绝对可以的。点击“关闭窗体”它就会弹出Closing Event的对话框你查到的资料的确没错,我早就告诉你这是一个Bug来的了,直到2005 beta 1都是一样见下面的连接:http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=7023178a-97e3-4d95-9dcb-d14a2ee357eb就是按照正常情况它不会触发WindowClosing事件,所以才要按我提供的反方案来做啊
      

  13.   

    我就是在.NET 1.1下面做测试的
      

  14.   

    用我上面给你的这个网址;http://www.fly99.com/index/teach/javascript_tx/cook23.htm,来做测试。因为我看过它的脚本是正确的
      

  15.   

    非常感谢,谢谢你misvcom(零下一度) 。
      

  16.   

    还有一个问题就是,我做的浏览器是一个多页面的浏览器
    好像在第一个窗口用window.close()脚本关闭页面时,能够触发WindowClosing事件但是,在弹出窗口中却无法触发WindowClosing事件,直接就把窗口关闭了。
    这是为什么啊?
      

  17.   

    那弹出的窗体是否同样有通过 Advise 建立通知连接?
      

  18.   

    你必须重新定义新建窗体的事件处理方法,用回你写的那个能正常激发WindowClosing事件的类的实例的窗体来打开,不能用一般单纯包含AxWebBrowse控件的窗体来打开,否则同样是不能激发WindowClosing事件的