在Alt+Tab中使窗口不可见

发布时间 2023-10-20 17:15:15作者: DIO·SAMA
#region 在Alt+Tab中使窗口不可见
        // 定义用于修改窗口样式的常量
        private const int GWL_EXSTYLE = -20;
        private const int WS_EX_TOOLWINDOW = 0x00000080;

        // 导入 Windows API 函数
        [DllImport("user32.dll")]
        private static extern int GetWindowLong(IntPtr hwnd, int index);

        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
        #endregion

使用

            WindowInteropHelper wndHelper = new WindowInteropHelper(this);
            IntPtr hwnd = wndHelper.Handle;

            // 获取当前窗口样式
            int currentStyle = GetWindowLong(hwnd, GWL_EXSTYLE);

            // 设置 WS_EX_TOOLWINDOW 样式,这会使窗口在任务切换时不可见
            SetWindowLong(hwnd, GWL_EXSTYLE, currentStyle | WS_EX_TOOLWINDOW);