tags:

views:

24

answers:

1

Hi,

I'm really struggling to find a way of bringing forward another programs window.

For example, I use FindWindow to find the handle of Notepad. I then try to bring the window forward using SetWindowPos(hWnd, 0,0, 0, 0, 0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);

But it just doesnt work!! ShowWindow doesnt either!

Can you please help and maybe show me a snippet of code?

Thanks

A: 

Dunno if this is the same thing or not, but at some point in Windows development Microsoft added some too-clever-by-half "anti-popup" code that would prevent a program that does not have the focus from un-minimizing its windows... instead, the window's entry in the programs-bar would just blink. Perhaps there is similar logic preventing a non-foreground program from bringing its window forward?

In any case, here is some code to try, that might or might not help:

 // Check to see if we are the foreground thread
 DWORD foregroundThreadID = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
 DWORD ourThreadID = GetCurrentThreadId();

 // If not, attach our thread's 'input' to the foreground thread's
 if (foregroundThreadID != ourThreadID) AttachThreadInput(foregroundThreadID, ourThreadID, TRUE);

 // Bring our window to the foreground
 SetForegroundWindow(hWnd);

 // If we attached our thread, detach it now
 if (foregroundThreadID != ourThreadID) AttachThreadInput(foregroundThreadID, ourThreadID, FALSE);

 // Force our window to redraw
 InvalidateRect(hWnd, NULL, TRUE);
Jeremy Friesner
Hmm, you don't have enough appreciation for MSFT's efforts to put a stop to the "shove a window in the user's face" attitude. Shame on you.
Hans Passant
I have plenty of appreciation for it. I just think they should rename their SetForegroundWindow() function to SetForegroundWindowButNotUnlessIAddSomeExtraMumboJumboToShowThatIReallyMeanIt(), so that its name will accurately reflect its behavior.
Jeremy Friesner