Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a window to be topmost

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?

like image 606
Victor Avatar asked Feb 20 '13 20:02

Victor


People also ask

How do I make a window TopMost?

You can now press Ctrl+Space to set any currently active window to be always on top. Press Ctrl+Space again set the window to no longer be always on top. And if you don't like the Ctrl+Space combination, you can change the ^SPACE part of the script to set a new keyboard shortcut.

How do you Z-order a window?

You can use the GetTopWindow function to search all child windows of a parent window and return a handle to the child window that is highest in z-order. The GetNextWindow function retrieves a handle to the next or previous window in z-order.

What is TopMost in C#?

Gets or sets a value that indicates whether a window appears in the topmost z-order. public: property bool Topmost { bool get(); void set(bool value); }; C# Copy.


2 Answers

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)

like image 79
Amir Avatar answered Sep 29 '22 11:09

Amir


Use CreateWindowEx with (extended) window style WS_EX_TOPMOST.

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

like image 34
Cheers and hth. - Alf Avatar answered Sep 29 '22 09:09

Cheers and hth. - Alf