This action is not "focus"; this is "activate".
In the simplest case, you can use combination of
SetActiveWindow
with
SetForegroundWindow
. However, it works poorly in one case: the moment of time when the window to be activated opens a modal dialog.
So, the comprehensive solution is this: first, using the main window handle, you have to find the handle of this dialog, if any. This is done using the function
GetLastActivePopup
. This function will return either the handle of the dialog, or the handle of the main window which you already know. Activate that window.
Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633507%28v=vs.85%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646311%28v=vs.85%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633539%28v=vs.85%29.aspx[
^].
—SA