views:

219

answers:

2

I am using the Win32 API call SetForegroundWindow to set focus to another application's window. I am then using SendKeys to send keystrokes to that window.

This works well apart from when a modal window (such as a File Open dialog) is open. In this scenario I want to set that modal child window as the foreground window.

Using my main application's hWnd, how can I set any modal child window as the active window? (Note that there may be a modal child that itself has another modal child open, so ideally I would like things to work in this scenario as well).

I am coding this in C#, .NET 3.5.

+2  A: 

See the GetLastActivePopup API.

RichieHindle
+1  A: 

GetTopWindow will give you the highest Z-order decedent of a given HWND, which would be the last modal dialog in the event one exists.

GetLastActivePopup will also work.

Kevin Montrose