Understanding window handling of IE Modal Dialogs


One of the things that I was working on in last couple of months was to do with Internet Explorer. One of the things was to explore what are the different ways in which I can host a web page within my windows app. But still interesting was the part that followed – how do I manage the IE modal dialog boxes? For instance, lets say in my MDI application, one of the dialogs display a web page. This web page opens a new modal web dialog. Now when I move to another screen in my MDI application, I want this dialog to disappear. That led me on an interesting quest to understand how the modal web dialogs behave.

Usually, the window of a modal dialog would have an owner and its modality would be restricted to that owner.  For instance, if you open a modal dialog in your windows application, the application main window owns the new modal dialog and then the dialog retains its modality only to the parent. In case of the IE modal dialogs however, these dialogs belong not to the IE window or document, but to the desktop. Yes that’s exactly where it starts behaving quite unlike the normal dialogs. So if you iterate on all the windows belonging to the IE window, be assured that you will not find the modal dialogs.

This is the behavior when we use the window.ShowModalDialog() function in javascript.  In case you use the window.open() (without the modal=’yes’ parameter), the resulting child window will still belong to the IE window hence making it easier for you to enlist it with a EnumChildWindows() Win32 API call.

So as we see, in case of modal dialogs, things get slightly more complex. Here is the approach that we figured out in order to find all the modal dialogs for a given IE instance –

– Use the EnumChildWindows() Win32 API to enumerate all the child windows belonging to the desktop. In order to do this, the parent hWnd parameter should get its value from the GetDesktopWindow() API.

– Each EnumChildWindows() API call requires a postback that is called whenever the window is found. In this postback method, derive the process Id from the Window Handle of the dialog using the GetWindowThreadProcessId() API and then match it with your IE process.

I am not sure if there is still a better way of doing this thing. But if you know of a better way, I would be glad to take a second look here. 🙂

–Sanket


Comments (5)

  1. Sanki!

    Long time to see you. Just read your blog, saw you at flickr makes me dlighted and get me back to memories of 2 years back, when we work togther and had a great time.

    We had a very good time at Nawras. Good to read your blog, and to know about your vacations, and everything.

    Development at my side : I switched to CresBank (Samba Bank), 1 month back, a career move from service industry to corporate sector. I am working as a PM in Cres Bank PMO. Very different learning, and enjoying it. Life after marriage is also going good.

    Rest is fine.

    Take Care & Keep Rocking.

    Wajahat Abbas

    (wajahat.abbas@cresbank.com)

    http://www.wajahatabbas.com

  2. Sanki!

    Long time to see you. Just read your blog, saw you at flickr makes me dlighted and get me back to memories of 2 years back, when we work togther and had a great time.

    We had a very good time at Nawras. Good to read your blog, and to know about your vacations, and everything.

    Development at my side : I switched to CresBank (Samba Bank), 1 month back, a career move from service industry to corporate sector. I am working as a PM in Cres Bank PMO. Very different learning, and enjoying it. Life after marriage is also going good.

    Rest is fine.

    Take Care & Keep Rocking.

    Wajahat Abbas

    (wajahat.abbas@cresbank.com)

    http://www.wajahatabbas.com

  3. http://bharath-marrivada.blogspot.com/ says:

    Hello Sanket,

    Can you prove me a sample code on how to control the model dialog elements using COM.

    Regards

    Bharath.

  4. Premjeet Singh says:

    Your technique works great in capturing IE Modal Dialogs.

    I can't find any other reference to this problem.

    Thanks a lot for sharing your solution.

Skip to main content