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
  • 13,296
  • 21
  • 55
  • 51

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
  • 455,417
  • 76
  • 598
  • 1,067
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
  • 327,498
  • 120
  • 583
  • 623
  • 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
  • 11,639
  • 10
  • 50
  • 86
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
  • 9,449
  • 19
  • 60
  • 105