22

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?

Victor
  • 13,914
  • 19
  • 78
  • 147
  • 1
    You cannot keep your window on top of all the others. There are multiple windows in the system. When all of them try to be the top window, not all of them can win. – David Heffernan Feb 20 '13 at 20:54
  • I meant the Topmost property. – Victor Feb 20 '13 at 20:56
  • @DavidHeffernan: as it happens you're right, for Windows 8.1. i'm using a splendid little utility called ClockX. it can be configured to increase transparency when you mouse over it, and it supports click-through, so I have it topmost, on top of all other windows. however, once in a while windows messes that up, and the clock disappears. all it takes to fix it is a right-click of its system tray icon, but it's still very annoying. and so it is with a great many windows "technologies". they have sort of deteriorated as windows has evolved. – Cheers and hth. - Alf Jun 22 '14 at 10:34

4 Answers4

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

Victor
  • 13,914
  • 19
  • 78
  • 147
Amir
  • 1,638
  • 19
  • 26
  • Thank you this was an excellent answer for me! Would it work without passing `SWP_NOMOVE` and `SWP_NOSIZE`? – Noitidart Jul 15 '15 at 05:30
  • 1
    you can retrieve all information about the specified window by GetWindowLong and change an attribute of the specified window by SetWindowLong. but SetWindowLong will not take effect until you call the SetWindowPos function – Amir Jan 26 '16 at 20:45
  • Thanks Amir very much! – Noitidart Jan 27 '16 at 02:29
  • 1
    @Noitidart Without `SWP_NOMOVE` and `SWP_NOSIZE`, the window would move to the corner of the screen and its width and height would become 0. – Anonymous Apr 15 '20 at 22:34
27

Use CreateWindowEx with (extended) window style WS_EX_TOPMOST.

Disclaimer: it's about 15 years or so since I touched that stuff.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
12

see SetWindowPos, hWndInsertAfter parameter. passing HWND_TOPMOST should do what you want.

additionally, you may want to pass SWP_NOMOVE | SWP_NOSIZE to uFlags parameter if you want to keep position and size unchanged.

Hayri Uğur Koltuk
  • 2,970
  • 4
  • 31
  • 60
3

SWP_NOMOVE Retains the current position (ignores X and Y parameters). SWP_NOSIZE Retains the current size (ignores the cx and cy parameters). If you don't set these flags you should specify position and size instead of passing 0, 0, 0, 0

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
vik_78
  • 1,107
  • 2
  • 13
  • 20