Programming magic, glory, and juices.

SetForegroundWindow, Bringing Window To Top

October 2nd, 2009


When using SetForegroundWindow/SetActiveWindow sometimes it does not bring the window into the foreground window. There is a great article on Dr. Dobb’s Journal that explains why this happens and how you can bring your window into the foreground.

HWND ForegroundWindow;
HWND BringToForegroundWindow;
DWORD ForegroundWindowThread;
DWORD BringToForegroundThread;

// Set BringToForegroundWindow

ForegroundWindow = GetForegroundWindow();
ForegroundWindowThread = GetWindowThreadProcessId(ForegroundWindow, NULL);
BringToForegroundThread = GetWindowThreadProcessId(BringToForegroundWindow, NULL);

AttachThreadInput(ForegroundWindowThread, BringToForegroundThread, TRUE);
SetForegroundWindow(BringToForegroundWindow);
AttachThreadInput(ForegroundWindowThread, BringToForegroundThread, FALSE);
SetActiveWindow(BringToForegroundWindow);
Leave a Reply