views:

368

answers:

4

Is the there a way to force another window to be on top? Not the application's window, but another one, already running on the system. (Windows, C/C++/C#)

+3  A: 

You can use the Win32 API BringWindowToTop. It takes an HWND.

You could also use the Win32 API SetWindowPos which also allows you to do things like make the window a top-level window.

Brian R. Bondy
The seccond link is incorrect, you mean http://msdn.microsoft.com/en-us/library/ms633545%28VS.85%29.aspxCould you also suggest an example on this function?
Levo
@Levo: Thanks I mustn't have copied it to the clipboard correctly before I pasted.
Brian R. Bondy
+2  A: 
SetWindowPos(that_window_handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

BringWindowToTop moves the window to the top of the Z-order (for now) but does not make it a topmost window.

Jerry Coffin
Can you give an example please?
Levo
@Levo:just substitute the handle of the window you want on top in place of `that_window_handle` in the snippet above.
Jerry Coffin
A: 

I tried this from VB6 to now C# all because the customers wanted it - nothing really works, nothing holds the window on top, just bring it up for the moment.

But anyway, why should a window be on topmost as long as it is running? I can only imagine a Teacher/Test-Software, where the user should not be able to open up a browser and query google for answers. In such a case I recommend to capture the whole screen, or react on losing the focus/mouse.

BeowulfOF
I recommend not giving your testing computers Internet access...
OedipusPrime
Sure, but how to achive this, for cisco-exams? Or any other online-exam?
BeowulfOF
A: 

BringWindowToTop() has no effect if you want to bring a applications window from behind (or minimized) to front. The following code does this trick reliable:

ShowWindow(hwnd, SW_MINIMIZE);
ShowWindow(hwnd, SW_RESTORE);
RED SOFT ADAIR