Set a window to be topmost
Asked Answered
A

4

23

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?

Apex answered 20/2, 2013 at 20:14 Comment(3)
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.Outsider
I meant the Topmost property.Apex
@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.Marven
M
29

Use CreateWindowEx with (extended) window style WS_EX_TOPMOST.

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

Marven answered 20/2, 2013 at 20:18 Comment(1)
I am not in front of a computer anymore, but tommorow I will test and mark your answer.Apex
B
52
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)

Brach answered 1/7, 2015 at 19:39 Comment(3)
Thank you this was an excellent answer for me! Would it work without passing SWP_NOMOVE and SWP_NOSIZE?Max
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 functionBrach
@Max Without SWP_NOMOVE and SWP_NOSIZE, the window would move to the corner of the screen and its width and height would become 0.Yacht
M
29

Use CreateWindowEx with (extended) window style WS_EX_TOPMOST.

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

Marven answered 20/2, 2013 at 20:18 Comment(1)
I am not in front of a computer anymore, but tommorow I will test and mark your answer.Apex
S
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.

Sugary answered 20/2, 2013 at 20:17 Comment(0)
B
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

Basement answered 4/8, 2015 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.