- Bring VB.Net window on top of all windows
- 1 Answer 1
- Win32: Bring a window to top
- QML on Windows: make the window to stay on top
- 3 Ways to Keep a Window Always on Top on Windows 10
- 1. Use a Keyboard Shortcut with an Always on Top AutoHotkey Script
- 2. Use Your Mouse with DeskPins
- 3. Use a System Tray Menu with TurboTop
- Bring the Always-on-Top Feature to All Windows
- What are the differences between BringWindowToTop, SetForegroundwindow, SetWindowPos etc.?
- 2 Answers 2
Bring VB.Net window on top of all windows
I have a window which should stay on top of Power point slide shows. So it should be on top of all the windows. I did this easily using VB 6 using Lib «user32», but it seems to be difficut with VB.net.
This does not seem to work as it works only within the program.
This also gives a error! Any help is greatly appreciated! Thanks in advance,
1 Answer 1
If you want a window in your application to always appear on top of a window of a different application, then the BringWindowToTop function is definitely not what you want. For starters, as you’ve noticed, you have to repeatedly call the function using a timer. That should be your first clue that it’s the wrong API. Another problem is that it’s only bringing your window to the top of the Z order for its process, not all of the other processes running on the system. As the documentation explains,
Calling this function is similar to calling the SetWindowPos function to change a window’s position in the Z order. BringWindowToTop does not make a window a top-level window.
That last sentence should indicate that there is a better way. Windows has built-in support for top-level windows (i.e., those that should always appear on top of other windows): these are called top-most windows. This is exactly what you want. Top-most windows always appear above non-topmost windows.
Raymond Chen attempts to explain some of the confusion on his blog. Note that in this case, HWND_TOP is equivalent to BringWindowToTop . Instead, you want HWND_TOPMOST .
The simplest way of making a window top-most is to specify the WS_EX_TOPMOST flag when you create the window. The .NET Framework hides most of the window creation work behind the scenes, but you can customize the parameters when you need to by overriding the CreateParams property of your form class.
Here’s some sample code to make a form always top-most:
Win32: Bring a window to top
I have a Windows program which has two 2 windows in it:
When I double click on hwnd, I need hwnd2 to pop up and show some data, so I use this function to bring hwnd2 to top:
hwnd2 is brought to top, but there is one thing odd. When I click on hwnd2 again, hwnd (main interface) pops itself up again automatically. I tried to use the following function to solve this problem, but non of them works.
How could I solve this problem? Thanks in advance.
(for replying to aJ, hwnd2 doesn’t have parent because it needs to be a toplevel window so it can be in front/back of other windows)
(hwnd2 is a media player which is composed of several windows, one of the windows is for video dispaly, two other trackbar controls for progress bar and volume bar, one Toolbar control for control panel.)
(There is one this might help, no matter which window I click on hwnd2, hwnd pops up automatically as loong as «the mouse is on top of hwnd in Z-order», including menu bar and non-client area, etc.)
(This media player is writen in Direct Show. I use IVideoWindow::put_Owner to put video window as the video owner, Direct Show internally creates a sub-video window as a child of the video window. Except for this sub-video window which I can’t see the source code, I don’t see any thing suspicious in hwnd2.)
I found the reason, which is because of Direct Show. I use multithread to execute it, and then the problem’s solved. But. why??
This problem can be resolved by using PostMessage (rather than SendMessage).
QML on Windows: make the window to stay on top
I do really need to make my window to stay on top on Windows, but Windows itself does not seem willing to allow me to do this.
I cannot use the workaround with setting registry values because I am not able to ask the user to log-out/log-in.
Besides, I use QML and the solution with QWidget::raise() and QApplication::setActiveWindow() does not seem to work also because I have not managed to get the QML root object as a QWidget pointer with the following code:
I have also tried to make the window active right from the QML:
but with no luck also.
Is there, either way to bring the window on top on Windows without changing the registry and, preferably, from the QML purely?
Edit: currently used window flags are:
I am deploying Qt 5.7 app on the Windows 10 x64 machine. I have found this two bugfixes:
from which I can conclude that QWidget::activateWindow() and QWindow::requestActive() should work on Windows XP and Windows 7.
Here is my mcve, as @derM asked:
It was compiled under Windows 10 x64 with MinGW x32.
Easier way to reproduce: run in the command prompt
where debug\Test.exe is a path to the mcve binary, then open File Explorer and navigate somewhere. When the window opens, it won`t be in the foreground.
Harder way: If you just run it, the window will stay on top as it should. But if you press Run button in the Qt Creator and switch the focus (I suppose, mouse focus should be changed, just pressing Alt+Tab won`t help) to another process (in my case — File Explorer), the window is displayed under the current active File Explorer window, and even if I will bring it up by clicking, it will go background as soon as I choose any other application.
The real application is started from a service, so there often will be an app holding mouse focus when my app is started. I suppose that Qt ability to bring the window to foreground is implemented using SetForegroundWindow API call, which notes the following restrictions in it`s remarks:
- The process is the foreground process.
- The process was started by the foreground process.
- The process received the last input event.
- There is no foreground process.
- The process is being debugged.
- The foreground process is not a Modern Application or the Start Screen.
- The foreground is not locked (see LockSetForegroundWindow).
- The foreground lock time-out has expired (see — SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
- No menus are active.
So I wonder is it possible at all to bring the window to foreground if the process has been started by a service, not a user (i. e. the process has not been an active process during startup).
3 Ways to Keep a Window Always on Top on Windows 10
When you are working in Windows with limited screen space, it’s useful to have certain windows stay on top of others. Some programs in Windows automatically incorporate this “always on top” feature, but most programs don’t, and Windows doesn’t include this feature either.
Today we’ll cover three ways to easily add the “always on top” feature to every single program in Windows.
1. Use a Keyboard Shortcut with an Always on Top AutoHotkey Script
AutoHotkey is a free program that allows you to create scripts to perform multiple actions with a single keyboard shortcut. It runs in the background and sits in the system tray.
We’re going to use AutoHotkey to create a script that will keep a selected window on top of all others when you press Ctrl + Spacebar when that window is active. We’ll show you the script and explain how it works. But we’ll also provide a downloadable AutoHotkey script file you can use.
Download and install AutoHotkey and then run it.
Download the AlwaysOnTop.ahk script file, extract the .zip file, and put the script in a folder on your hard drive that won’t be moved or deleted.
The script contains some comments and then the following command:
This command sets the currently active window to always be on top when you press Ctrl + Spacebar. If you’d rather use a different key combination, you can change the first part of the command, “^SPACE“. The “^” character represents the Ctrl key. You’ll find the symbols to use for other hotkeys in the Hotkey Modifier Symbols section on this AutoHotkey help page.
You can put the script in the Windows startup folder (%AppData%\Microsoft\Windows\Start Menu\Programs\Startup), so it starts automatically when Windows starts.
The AutoHotkey script stays in the system tray.
To make the active window always on top, press Ctrl + Spacebar (or the keyboard shortcut you assigned). Press the keyboard shortcut again to disable “always on top” for the active window.
For script options, right-click on the AutoHotkey icon in the system tray.
2. Use Your Mouse with DeskPins
DeskPins provides pins you can grab and use to keep any window on top of all other windows. The program sits in the system tray and uses minimal resources.
Download and install DeskPins. Run the program, right-click the icon in the system tray, and select Options to change the Pins, Autopin, and Hotkeys settings.
On the Pins tab, you can change the color of the pins. The Tracking rate controls how often each pin checks the pinned window’s position and state. Lower values make the pins more responsive. You can enter any number from 10 to 1000 msec (milliseonds).
If you have an older computer, you may want to increase the Tracking rate number to avoid slowing down your computer.
You can also choose Single click or Double click under Tray Icon Activation to indicate how you get pins from the DeskPins system tray icon. The default is Single click.
You can have DeskPins automatically pin certain types of windows using rules on the Autopin tab.
Check the Enable box and then click Add to add a new rule. Use the check boxes next to the rules to enable and disable individual rules.
Click Help to access the DeskPins help file for more information about automatically pinning windows.
DeskPins provides default shortcuts for entering pin mode and toggling the active window pin. You can change these shortcuts on the Hotkeys tab.
To pin a window on top, click (or double-click, depending on the setting) the DeskPins icon in the system tray.
Then, click the title bar on the window you want to keep always on top.
A pin displays on the title bar of the chosen window in the color you specified, or in the default color red.
Click the pin again to disable always on top for the window.
3. Use a System Tray Menu with TurboTop
TurboTop also sits in your system tray like DeskPins. But it operates differently.
Download and install TurboTop. Then, run the program.
To keep a window always on top, click the TurboTop icon in the system tray. A list of all open windows displays. Select the title of the window you want.
Any windows you choose to be always on top are indicated with a check mark on the TurboTop menu.
To stop keeping a window always on top, click the TurboTop icon in the system tray and select that window again, so there’s no check mark next to the window name on the menu.
Bring the Always-on-Top Feature to All Windows
Whether you like using keyboard shortcuts or prefer using the mouse, there’s an easy solution for keeping windows always on top. If you’ve found other options for keeping windows always on top, let us know in the comments.
What are the differences between BringWindowToTop, SetForegroundwindow, SetWindowPos etc.?
What are the real differences between them? When and why would BringWindowToTop be preferred to SetForegroundWindow , or SwitchToThisWindow or even SetWindowPos with the flags set to activate and show?
2 Answers 2
There are many closely-related concepts involved, and related terms are often misused, even in the official documentation.
Important types of windows:
top-level windows: Windows that have no parent window. The main window for an application is almost always a top-level window. It does not have anything to do with z-order.
child windows: Windows that are contained by a parent window. Their position is always relative to the parent window’s area. Child windows are often «controls»: UI things like buttons and edit boxes.
parent windows: Windows that have child windows. Top-level windows often have children. But note that child windows may also have children and thus be both parent and child windows.
owned windows: Windows that are controlled by a another window, but aren’t necessarily children of the other window. An example is a floating tool palette: it’s owned by another window in the application, but it’s not locked to that other window’s area.
owner windows: Windows that own an owned window.
Often the distinction between an owner/owned relationship and a parent/child relationship isn’t important, so the parent and child terms are often used for both contexts, even in documentation. In some cases, parent fields and parameters are overloaded to mean parent and/or owner.
top of the z-order: This literally means the window that displays above other windows.
active window: A fuzzy concept, but it typically means the top-level window the user would consider the «current» window. The active window is typically drawn with a distinctive border and its tile on the task bar is highlighted. The active window is usually at or near to the top of the z-order among all other top-level windows, and it is the parent or owner (perhaps indirectly) of the window with keyboard focus.
keyboard focus: Indicates the window that will receive the keyboard messages. Conceptually, there is one window with keyboard focus. Often the window with focus is a child (or grandchild, etc.) of the active window.
foreground: The active window is typically in the foreground. The name seems to suggest that it’s at the top of the z-order, but it really means that the thread that created the window gets a slight priority boost. That active window is usually also the foreground window.
So let’s say you’ve got this browser window open, and you’ve also got an instance of Notepad running. If you click on the document in Notepad, a whole flurry of messages and state changes occur. You’re actually clicking on a big edit box, which is a child window of Notepad’s top-level window. That click causes the edit box to get activated, but child windows can’t really be the «active» window, so it just takes the keyboard focus and passes the activation message up through its ancestors until it gets to a top-level window. The top-level window «activates» by moving to the top of the z-order, highlighting its border, etc. It also becomes the foreground window, so its thread gets a little boost to make the UI a little more responsive than any other windows.
With these terms in mind, you can parse the MSDN descriptions for the functions you listed to tease out the subtle differences.
If you’re trying to lay out your window’s children, just use SetWindowPos (or MoveWindow, SizeWindow, and ShowWinow). Of the remaining functions, SwitchToThisWindow looks deprecated and essentially the same as SetForegroundWindow. (Note that, in many cases, SetForegroundWindow won’t do what you want unless you’re the active application or the active application has given you permission to use it.) BringWindowToTop is mostly about bringing a window to the top of the z-order (which you can do with SetWindowPos), with extra side effects that make it behave like SetForegroundWindow if you call it on a top-level window.
The concept of the foreground window was introduced when input was desynchronized in order to express the «really global active window», as opposed to SetActiveWindow, which continued to refer to the local active window.