Question: I’m using SHDocVw.InternetExplorer APIs in my Vb.Net WinForms application to capture requests sent from IE in my application.

As given on [this](http://msdn.microsoft.com/en-us/library/aa768337%28v=vs.85%29.aspx) link

> 
> "The NewWindow3 event is only fired when a new instance of Internet
>  Explorer is about to be created. Calling showModalDialog or
>  showModelessDialog does not trigger an event because they are not new
>  instances of Internet Explorer"
> 
> 
> 

I am trying to automate user actions in Internet Explorer for automated testing. So I want to know that due to some user action a modal dialog is opened or some alert window is opened.

So my question is:

* How do I know that a modal dialog window is opened in Internet Explorer?
* Is there any other event raised by Internet Explorer in this case?
* If it's not possible directly then is there some other way to handle this scenario?

Answer: 
The only thing I was able to find is the WindowStateChanged event <http://msdn.microsoft.com/en-us/library/aa768307(v=vs.85).aspx> which mentions: 

> 
> The WindowStateChanged event is raised when the state of a content
>  window, such as the browser window or a tab, might have changed. The
>  following actions raise this event.
> 
> 
> * The browser window is minimized or restored.
> * An active tab becomes inactive.
> * An inactive tab becomes active.
> * **The browser window is enabled or disabled due to a modal dialog box.**
> 
> 
> 

Here is a VB.NET code example of how to use the WindowStateChanged event to check the enabled/disabled and visible flags:

```
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Code to instantiate the IE COM object'
        Dim IE As New SHDocVw.InternetExplorer()
        AddHandler IE.WindowStateChanged, AddressOf OnWindowStateChanged
        'do stuff here'
        IE.Quit()
    End Sub

    Public Sub OnWindowStateChanged(ByVal dwWindowStateFlags As UInteger, ByVal dwValidFlagsMask As UInteger)
        Const OLECMDIDF_WINDOWSTATE_USERVISIBLE As UInteger = 1
        Const OLECMDIDF_WINDOWSTATE_ENABLED As UInteger = 2

        'Check if the window is disabled'
        If dwValidFlagsMask And OLECMDIDF_WINDOWSTATE_ENABLED = OLECMDIDF_WINDOWSTATE_ENABLED Then
            If dwWindowStateFlags And OLECMDIDF_WINDOWSTATE_ENABLED <> OLECMDIDF_WINDOWSTATE_ENABLED Then
                'The browser window has been disabled.... possibly by a modal popup'
            Else
                'The browser is enabled.'
            End If
        Else
            'The enabled/disabled flag has not been initialized, so we cannot check the state during this event handler execution'
        End If

        'Check if window is visible: '
        If dwValidFlagsMask And OLECMDIDF_WINDOWSTATE_USERVISIBLE = OLECMDIDF_WINDOWSTATE_USERVISIBLE Then
            If dwWindowStateFlags And OLECMDIDF_WINDOWSTATE_USERVISIBLE <> OLECMDIDF_WINDOWSTATE_USERVISIBLE Then
                'The browser window is NOT visible'
            Else
                'The browser window is visible'
            End If
        Else
            'The visible flag has not been initialized, so we cannot check the state during this event handler execution'
        End If

    End Sub
End Class

```
There is no a such event, but you can use some other approaches:

1. Webbrowser.Document.Window.Error is fired before modal window will
be displayed
2. You can implement INewWindowManager interface - I don't know exactly, but I think it can give some information to you
3. You can implement IDocHostUIHandler and use ShowUI method - but it executed not only for displaying new window, you should use it
carefully
4. IOleCommandTarget - 'Exec' method with cmdId
OLECMDID\_SHOWSCRIPTERROR, OLECMDID\_SHOWMESSAGE, OLECMDID\_SHOWFIND,
 OLECMDID\_SHOWPAGESETUP, OLECMDID\_SHOWPRINT means that new window
 will be opened