Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WebBrowser WindowClosing Event C#

1,117 views
Skip to first unread message

JC

unread,
Dec 4, 2002, 10:03:45 AM12/4/02
to
NO RESPONSE RECEIVED - Reposted on 12/4/02

>-----Original Message-----
>Another ?:
>Attached is the message box displayed prior to the
>close. Can this be overrode? Can the text be changed?
>Standard users may not realize the application is going
>to close.
>
>Thanks..
>
>>-----Original Message-----
>>I created a user control that contains the WebBrowser
>>control and other controls. The user control is
utilized
>>on a form which is added to a tabcontrol of a tabpage
in
>>an MDI application.
>>
>>If javascript window.close is executed from a webpage
>the
>>main application window is closed.
>>
>>One recommended option was to utilize the WindowClosing
>>event of the WebBrowser control to cancel the close,
>>however, it appears this event is never fired. Is this
>a
>>known bug? Is there a work-around to cancel the close?
>>.
>>

Eldon Lei

unread,
Dec 4, 2002, 9:31:33 PM12/4/02
to
Hi JC:

Thanks for your patient, we are currently looking into the issue and should
get back to you shortly.

Base on the description, when you call window.close, does the application
brings up a confirmation dialog before closing the entire window? In IE, if
a javascript call window.close and if the window is dynamically created
(using window.open, for example), there will be a confirmation dialog pops
up before the window is shut down.

Thanks,
Eldon

JC

unread,
Dec 4, 2002, 11:52:19 PM12/4/02
to
Hi Eldon,

Yes, a confirmation dialog is displayed. Unfortunately,
users may not understand that choosing yes to close the
winow will exit the MDI application. We also do not want
users to close the application in this manner.

The pop-up windows, which we are handling via the
NewWindow2 Event function ok. In this case the
confirmation dialog is not displayed and the new form
simply closes..

Thanks,
John

>.
>

Eldon Lei

unread,
Dec 9, 2002, 7:32:24 PM12/9/02
to
Hi John:

I have reproduced the behavior you have been describing and able to find a
workaround that could allow you to capture the event. The steps are the
following:

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. Within the IEEvents class you should '#region Implementation of
WebBrowserEnvets2' followed by all the methods of DWebBrowserEvents2.
Before the
end of the class you will see '#end region}'. Edit this line so that the
'}' shows
up in the next line (otherwise you'll get a compiler error).

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);
}
The DispId value is from the typelib shdocvw.dll obtained using the tool
oleview. I
have attached a file shdocvw.idl which has the DispId for the all the
methods. So
far I have needed to apply 'DispId' attribute only to WindowClosing. So if
an event
does not fire you can try adding 'DispId' to that method.

4. Make you are using namespace System.Runtime.InteropServices. Add
following line
to top of your .cs file.

using System.Runtime.InteropServices;

5. Add the following lines of code to the end of the Forms
'InitializeComponent'
method.

//axWebBrowser1 represents the web browser control
//get the IConnectionPointContainer interface of web browser control
UCOMIConnectionPointContainer pConPtCon =
(UCOMIConnectionPointContainer)this.axWebBrowser1.GetOcx();
UCOMIConnectionPoint pConPt;
//get IID of DWebBrowserEvents2
Guid guid = typeof(SHDocVw.DWebBrowserEvents2).GUID;
//find the IConnectionPoint interface corresponding to GUID
pConPtCon.FindConnectionPoint(ref guid, out pConPt);
//create a instance to sink object
IEEvents e = new IEEvents();
//pass sink object to Web browser so it can fire events
//make sure you declare
private int
dwCookie in the form class but outside this method
pConPt.Advise(e, out dwCookie);

6. Add the following lines of code to the beginning of the Forms Close
handler
method.

//axWebBrowser1 represents the web browser control
//get the IConnectionPointContainer interface of web browser control
UCOMIConnectionPointContainer pConPtCon =
(UCOMIConnectionPointContainer)this.axWebBrowser1.GetOcx();
UCOMIConnectionPoint pConPt;
//get IID of DWebBrowserEvents2
Guid guid = typeof(SHDocVw.DWebBrowserEvents2).GUID;
//find the IConnectionPoint interface corresponding to GUID
pConPtCon.FindConnectionPoint(ref guid, out pConPt);
//disconnect sink object from Web browser
pConPt.Unadvise(dwCookie);

Hope this helps,
Eldon

JC

unread,
Dec 10, 2002, 2:32:05 PM12/10/02
to
Hi Eldon,

Thanks for your time on this. I updated my code with
your work-around and it is working great.

I did not see the attached file you referenced so I have
attached it for others.

Thanks Again,
John

> //find the IConnectionPoint
interface corresponding to GUID
> pConPtCon.FindConnectionPoint(ref
guid, out pConPt);
> //disconnect sink object from Web
browser
> pConPt.Unadvise(dwCookie);
>
>Hope this helps,
>Eldon
>

>.
>

shdocvw.IDL
0 new messages