Set a window to be topmost

Victor picture Victor · Feb 20, 2013 · Viewed 37k times · Source

I am trying to keep my window on top of the all others. I am new to C++ Win32 programming. This is my initialization of my window in WinMain:

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

I previously worked with dialogs, so the topmost property was really easy to use. But here, on a window I don't know how to set it. I also want to be able to trigger it. Can anybody help me?

Answer

Amir picture Amir · Jul 1, 2015
SetWindowPos(hwnd01, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

Note: SWP_NOMOVE | SWP_NOSIZE are for ignoring 3rd, 4th, 5th, 6th parameters of the SetWindowPos function.

The second parameter can be:

  • HWND_BOTTOM

  • HWND_NOTOPMOST (set window to be a normal window)

  • HWND_TOP

  • HWND_TOPMOST (set window to be always on top)