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.
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. 🙂