0

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

SampleTime
  • 291
  • 3
  • 19
  • What browser are you using? [This SO question](http://stackoverflow.com/questions/16558816/open-url-with-shellexecute-sw-showmaximized-dont-active-window-in-c) talks about Chrome. –  Feb 09 '17 at 21:01
  • I use mozilla... but I would prefer a that works regardless of the browser in use since I cannot assume that every user of the GUI has also this browser as default (or even installed) – SampleTime Feb 09 '17 at 21:12
  • @RawN: `ShellExecuteEx` uses whichever default browser any given user has chosen. – IInspectable Feb 09 '17 at 21:21
  • @IInspectable Yes. The question was aimed at discovering if OP uses chrome as a default browser. –  Feb 09 '17 at 21:25
  • 2
    @SampleTime: what you are asking for is basically out of your hands. You can't really force the browser to gain focus, the browser will have to handle that on its own. – Remy Lebeau Feb 09 '17 at 22:00
  • @RemyLebeau But I can also set focus to other windows programmatically... all I would need is a reliable way to get the main window handle of the browser, then I could just manually set the focus. – SampleTime Feb 09 '17 at 22:31
  • That's not going to be possible given your other requirements. – David Heffernan Feb 09 '17 at 22:33
  • "*all I would need is a reliable way to get the main window handle of the browser*" - there is no single reliable way to do that. Win32 has no concept of a "main window" to begin with. A process has one or more threads, and a thread can have one or more GUI windows. Period. If you are lucky, `ShellExecute/Ex()` will actually spawn a new process that creates a GUI in its first thread. Then you can use `EnumThreadWindows()`, or at least use `EnumWindows()` and `GetWindowThreadProcessId()` to look for windows that belong to the newly spawned process... – Remy Lebeau Feb 09 '17 at 22:47
  • ... but things start getting tricky across different browsers that use different architectures to manage their GUIs. And even more complex when `ShellExecute/Ex()` communicates with an existing process (using DDE, COM, etc) instead of spawning a new process, or when a newly spawned process internally delegates to another existing process. Then all bets are off. You might just have to resort to scanning the existing windows one time, then spawn the browser, and scan windows again to see if any new windows appeared. – Remy Lebeau Feb 09 '17 at 22:50
  • *"[...] a thread can have [**zero**] or more GUI windows"*. Anyway, [WinEvents](https://msdn.microsoft.com/en-us/library/windows/desktop/dd373889.aspx) are a bit more robust in monitoring window creation. That, too, fails in case of a new, unknown browser, with a new window class and unexpected window title. – IInspectable Feb 09 '17 at 23:43
  • Maybe you could embed a browser window into your app and show the URL there instead of in the default browser app. http://stackoverflow.com/questions/7681420/embed-html-browser-into-a-native-c-win32-project-using-visual-studio – Dan Korn Feb 10 '17 at 00:10

1 Answers1

-2

Specify "open" as the second parameter (the operation) instead of 0/NULL.

Dan Korn
  • 1,274
  • 9
  • 14