<p>I am writing an application that needs to bring window of an external app to the foreground, and not necessarily steal focus (there is a setting the user can toggle to steal/not steal focus).</p>

<p>What is the best way to go about this using the win32 API?  I have tried SetForeground() but it always steals focus and does not consistenly work.</p>

<p>What is the best way to go about this? Any thoughts?</p>

[{'answer_id': 2992, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': True, 'text': '<p>SetForegroundWindow is supposed to steal focus and there are certain cases where it will fail.</p>\n\n<blockquote>\n  <p>The SetForegroundWindow function puts the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window</p>\n</blockquote>\n\n<p>Try capturing the focus with <a href="http://msdn.microsoft.com/en-us/library/ms646262.aspx" rel="noreferrer">SetCapture</a> prior to making the call. Also look into different ways of bringing the window to the front: SetForeGroundWindow, SetActiveWindow, even simulating a mouse click can do this.</p>\n'}, {'answer_id': 3003, 'author': 'Ecton', 'author_id': 457, 'author_profile': 'https://Stackoverflow.com/users/457', 'pm_score': 2, 'selected': False, 'text': "<p>You can try the BringWindowToTop function to not steal focus. I haven't used it, but it seems to be what you're looking for.</p>\n"}, {'answer_id': 3359, 'author': 'Ecton', 'author_id': 457, 'author_profile': 'https://Stackoverflow.com/users/457', 'pm_score': 3, 'selected': False, 'text': "<blockquote>\n  <p>What is the difference between SetForeGroundWindow, SetActiveWindow, and BringWindowToTop? It appears as if they all do the same thing.</p>\n</blockquote>\n\n<p>According to MSDN, SetForeGroundWindow will activate the window and direct keyboard focus to it. This attempts to work even when your process is in the background. SetActiveWindow does the same thing as SetForeGroundWindow, but it doesn't do anything if your application isn't the frontmost application. Finally, BringWindowToTop only brings the window to the top, and doesn't change the keyboard focus.</p>\n"}, {'answer_id': 25374, 'author': 'Mike Thompson', 'author_id': 2754, 'author_profile': 'https://Stackoverflow.com/users/2754', 'pm_score': 2, 'selected': False, 'text': '<p>Have you tried using SetWindowPos. This is the canonical function for moving, resizing and setting z-order in Windows. There is a SWP_NOACTIVATE flag you can use. Look at <a href="http://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspx</a>. I have not tried this on a window belonging to another process, but it is probably worth a try.</p>\n'}, {'answer_id': 1926827, 'author': 'Kenny Zhou', 'author_id': 234374, 'author_profile': 'https://Stackoverflow.com/users/234374', 'pm_score': 1, 'selected': False, 'text': '<p>SetWindowPos + SWP_NOACTIVATE does the job.</p>\n'}, {'answer_id': 5913047, 'author': 'ManAmongHippos', 'author_id': 477013, 'author_profile': 'https://Stackoverflow.com/users/477013', 'pm_score': 0, 'selected': False, 'text': '<p>You could use FindWindow to get the HWND of the window, then use the BringWindowToTop function found in the Win32 API.</p>\n'}]
['https://Stackoverflow.com/questions/2987', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/324/']