6

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#)

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52

4 Answers4

10
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
  • 476,176
  • 80
  • 629
  • 1,111
6

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
  • 339,232
  • 124
  • 596
  • 636
  • The seccond link is incorrect, you mean http://msdn.microsoft.com/en-us/library/ms633545%28VS.85%29.aspx Could you also suggest an example on this function? –  Dec 09 '09 at 14:39
  • @Levo: Thanks I mustn't have copied it to the clipboard correctly before I pasted. – Brian R. Bondy Dec 09 '09 at 14:40
3

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
  • 12,032
  • 10
  • 54
  • 92
0
BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lParam) {
     wchar_t buff[255];

    if (IsWindowVisible(hWnd)) {
        GetWindowText(hWnd, (LPWSTR) buff, 254);
        //wprintf(L"%s\n", buff);
        wstring ws = buff;
        if (ws.find(L"Firefox") != ws.npos)
        {
            ::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
        }
    }
    return TRUE;
}

int main(){
    BOOL enumeratingWindowsSucceeded = ::EnumWindows( EnumWindowsProc, NULL );
}
camino
  • 10,085
  • 20
  • 64
  • 115