This paper introduces several methods to keep applications at the top.
Problem description
Coupon website https://www.cps3.cn/Generally, if you want to put an application on the top level, you can set its TopMost property to true. For multiple applications with TopMost attribute set, the one activated after is above.
However, some applications, such as the global shortcut toolbar, need to be on top of all applications, even those with TopMost set.
Solution ideas
Note: making an application never be overwritten by other applications is a false proposition in itself. Because if two programs (A and B) do this, drag two windows to make them overlap. One of the two windows must be above the other, which is logically contradictory.
Therefore, we should try to avoid this situation. If we have to do so, this paper provides the following methods to achieve it (do not overlap two such applications, otherwise we will keep putting them on the top).
First, the application needs to set its TopMost property to true, so that the ordinary window itself will be under it. This article mainly discusses how the window is placed on the window with the TopMost attribute set.
Scheme 1: capture WM_ Windowschanging message
We know that using the SetWindowPos interface of Win32 can change the Z Order of the window. We can guess that when another application is on the top, our application will change its Z Order. Therefore, we can try to capture WM_ Windowschanging message.
When the size, position and Z-order of the window change, the window will receive WM_ For the windowschanging message, we can use WndProc to process the window message. When the message is captured, we can try to put the application on the top again. The key codes are as follows. The test is feasible, but it is uncertain whether there are side effects:
/// <summary> ///Scheme 1: capture WM_ Windowschanging message, if there is no swp_ The nozorder sign is placed on the top /// </summary> private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { switch (msg) { case Win32Api.WM_WINDOWPOSCHANGING: Win32Api.WINDOWPOS wp = (Win32Api.WINDOWPOS)Marshal.PtrToStructure( lParam, typeof(Win32Api.WINDOWPOS)); if ((wp.flags & Win32Api.SWP_NOZORDER) == 0) _ = SetTopMostLater(); // The compiler will issue a warning if it does not use discards break; } return IntPtr.Zero; } private async Task SetTopMostLater() { await Task.Delay(300); var interopHelper = new WindowInteropHelper(this); Win32Api.SetWindowPos(interopHelper.Handle, Win32Api.HWND_TOPMOST, 0, 0, 0, 0, Win32Api.TOPMOST_FLAGS); }
Scheme 2: cyclic topping
This is an easy scheme to think of. TopMost is set for the application every certain time. This scheme is also feasible:
/// <summary> ///Scheme 2: cyclic topping /// </summary> /// <returns></returns> private async Task SetTopMostLoop() { while (true) { await Task.Delay(2000); var interopHelper = new WindowInteropHelper(this); Win32Api.SetWindowPos(interopHelper.Handle, Win32Api.HWND_TOPMOST, 0, 0, 0, 0, Win32Api.TOPMOST_FLAGS); } }
Scheme 3: use hook
Think about it. In fact, in most cases, the use of other input devices such as mouse or keyboard will cause the top of the window to be robbed. Therefore, you can use the global hook to capture input events and then process them.
This scheme is flawed because there is a case where an application is opened without using the input device. In this case, the top setting effect will be preempted by the newly opened top setting application.
// Scheme 3: set the top when the mouse is pressed (only the mouse is considered) private void MouseHook_OnMouseActivity(object sender, System.Windows.Forms.MouseEventArgs e) { Console.WriteLine("mouse down......"); _ = SetTopMostLater(); } private MouseHook _mouseHook;
Finally, this article is some solutions to this problem. The task manager of Windows system can run on the top of all applications. Perhaps Microsoft does not open the interface because of the pseudo proposition mentioned above. Welcome to discuss it.
For the complete demo of the three schemes in this article, see GitHub. The links you can refer to (the discussion on this topic is older): link 1 and link 2.