把自己的UI永远至于其他进程的UI之上

无论如何切换,都让自己的UI始终处于其他进程的UI之上

需要的API

 [DllImport("user32.dll", SetLastError = true)]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpWindowClass, string lpWindowName);
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
        const int GWL_HWNDPARENT = -8;
        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll")]
        private static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
        static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        const int SWP_SHOWWINDOW = 0x0040;

使用的API设置自己的UI

            TestTopWin tmp = new TestTopWin();
            var handle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
            IntPtr hprog = FindWindow("Qt5QWindowIcon", "IMAgenGINE_MRDP");
            WindowInteropHelper tmp4 = new WindowInteropHelper(tmp) {Owner = hprog };
            IntPtr tttt = tmp4.Handle;
            SetWindowLong(handle, GWL_HWNDPARENT, hprog);
            tmp.Topmost = true;
            tmp.ShowDialog();

关键的API是 SetWindowLong

    LONG WINAPI SetWindowLong(
     _In_ HWND hWnd,
     _In_ int nIndex,
     _In_ LONG dwNewLong
    );

它有三个参数,第一个参数传入子窗口Handle, 第二个参数传入GWL_HWNDPARENT,第三个传入父窗口Handle


猜你喜欢

转载自blog.csdn.net/gao271003105/article/details/80589531