WPF allows the window to be activated as the uppermost window in the foreground

In WPF, if you want to use code control, let a window be the logical focus window of the current user's input, that is, the uppermost window of the current user's active window. The Activate method is used by default. The device can do the activation window

But on some special devices, using the following code to call up the window just flashes the icon on the taskbar, without letting the window be placed on the top level

window.Show();
window.Activate();

On most devices, the combination of Show and Activate can make the window active as the current user, even if the window was minimized or hidden before, it can be displayed by the Show method

However, some device windows are covered under other windows, and the window.IsActive of the window at this time is still true, but calling Activate will not let the window be placed on the upper layer.

I saw a lot of friends call SetForegroundWindow method on the Internet, in fact, WPF is open source now, you can see the Window Activate method is written

        public bool Activate()
        {
            // this call ends up throwing an exception if Activate
            // is not allowed
            VerifyApiSupported();
            VerifyContextAndObjectState();
            VerifyHwndCreateShowState();

            // Adding check for IsCompositionTargetInvalid
            if (IsSourceWindowNull || IsCompositionTargetInvalid)
            {
                return false;
            }

            return UnsafeNativeMethods.SetForegroundWindow(new HandleRef(null, CriticalHandle));
        }

Source code please see github

That is, calling SetForegroundWindow is similar to calling the Activate method. If you call Activate, you should call SetForegroundWindow.

Through the correct use of the big guy's SetForegroundWindow-Subdock-Blog Garden You can understand that you need to follow these steps

    1.得到窗口句柄FindWindow 
    2.切换键盘输入焦点AttachThreadInput 
    3.显示窗口ShowWindow(有些窗口被最小化/隐藏了) 
    4.更改窗口的Zorder,SetWindowPos使之最上,为了不影响后续窗口的Zorder,改完之后,再还原 
    5.最后SetForegroundWindow 

The corresponding order of changing windows in WPF uses the Topmost property, and setting the order requires a certain small change

In WPF by c # - Bring a window to the front in WPF - Stack Overflow can learn how to use the method AttachThreadInput

The entire code is shown below. I have not written the specific win32 method, please add it by your friends

        private static void SetWindowToForegroundWithAttachThreadInput(Window window)
        {
            var interopHelper = new WindowInteropHelper(window);
            var thisWindowThreadId = Win32.User32.GetWindowThreadProcessId(interopHelper.Handle, IntPtr.Zero);
            var currentForegroundWindow = Win32.User32.GetForegroundWindow();
            var currentForegroundWindowThreadId = Win32.User32.GetWindowThreadProcessId(currentForegroundWindow, IntPtr.Zero);

            // [c# - Bring a window to the front in WPF - Stack Overflow](https://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf )
            // [SetForegroundWindow的正确用法 - 子坞 - 博客园](https://www.cnblogs.com/ziwuge/archive/2012/01/06/2315342.html )
            /*
                 1.得到窗口句柄FindWindow 
                2.切换键盘输入焦点AttachThreadInput 
                3.显示窗口ShowWindow(有些窗口被最小化/隐藏了) 
                4.更改窗口的Zorder,SetWindowPos使之最上,为了不影响后续窗口的Zorder,改完之后,再还原 
                5.最后SetForegroundWindow 
             */
            Win32.User32.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);

            window.Show();
            window.Activate();
            // 去掉和其他线程的输入链接
            Win32.User32.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);

            // 用于踢掉其他的在上层的窗口
            window.Topmost = true;
            window.Topmost = false;

I tested a few devices that did n’t have the window on the upper layer. The above code can be used to set it, but I do n’t know what the possible pits are for setting the above code

Comes with a testing tool for walterlv, which can be used to get the current GetForegroundWindow

Tools for walterlv

In addition, Shaojun friends wrote an interesting library, which encapsulates a lot of win32 methods, please see kkwpsv lsjutil

Guess you like

Origin www.cnblogs.com/lindexi/p/12749671.html