Question

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?

Was it helpful?

Solution 2

Use CreateWindowEx with (extended) window style WS_EX_TOPMOST.

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

OTHER TIPS

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)

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top