I use ShellExecute in a GUI Aplication (created with Win API) to open a given hyperlink in the default browser of the user like:
ShellExecute(0, 0, "www.stackoverflow.com", 0, 0, SW_SHOWMAXIMIZED); // Just an example
This works and the site is correctly openend in the default browser.
However, I want the browser to take focus once it is started. At the moment this works only sometimes: say I execute the function like shown above in a button-click callback. Then it works as expected and the browser takes focus.
However, it takes some time after the button click until the browser is opened. If I set the focus in the meantime to, say an edit control, the edit control (and with it the whole GUI) keeps the focus with the browser being opened in the background. But I want the behavior to be consistent such that the browser always takes the focus, regardless of the user interaction in the small time period after clicking on the button.
How can this be achieved?
I already tried ShellExecuteEx
and WaitForInputIdle
, then retrieving the main window handle from the process ID. This however doesn't work unfortunatly, because ShellExecuteEx
doesn't always return a process ID (even if SEE_MASK_NOCLOSEPROCESS
is set)...