让窗口显示在所有虚拟桌面上

功能如图

C#代码实现如下

复制代码
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ShowInTaskbar = false;
        this.Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        IntPtr a = new System.Windows.Interop.WindowInteropHelper(this).Handle;

        IntPtr hWnd = new WindowInteropHelper(this).Handle;
        IntPtr longhWnd = GetWindowLongPtr(hWnd, GetWindowLongFields.GWL_EXSTYLE);
        var re = SetWindowLongPtr(hWnd, GetWindowLongFields.GWL_EXSTYLE, new IntPtr((int)longhWnd | 0x08000000));
    }


    [DllImport("user32.dll")]
    static extern int SetWindowLongPtr(IntPtr hWnd, GetWindowLongFields nIndex, IntPtr dwNewLong);

    [DllImport("user32.dll")]
    static extern IntPtr GetWindowLongPtr(IntPtr hWnd, GetWindowLongFields nIndex);

    private const int GWL_EXSTYLE = -20;
    private const int WS_EX_TOOLWINDOW = 0x00000080; // 使窗口不会在任务栏显示
    private const int WS_EX_APPWINDOW = 0x00040000; // 使窗口出现在任务栏
    private const int WS_EX_NOREDIRECTIONBITMAP = 0x00200000;
}
public enum GetWindowLongFields
{
    /// <summary>
    /// 设定一个新的扩展风格
    /// Retrieves the extended window styles
    /// </summary>
    GWL_EXSTYLE = -20,
    /// <summary>
    /// 设置一个新的应用程序实例句柄
    /// Retrieves a handle to the application instance
    /// </summary>
    GWL_HINSTANCE = -6,
    /// <summary>
    /// 改变子窗口的父窗口
    /// Retrieves a handle to the parent window, if any
    /// </summary>
    GWL_HWNDPARENT = -8,
    /// <summary>
    ///  设置一个新的窗口标识符
    /// Retrieves the identifier of the window
    /// </summary>
    GWL_ID = -12,
    /// <summary>
    /// 设定一个新的窗口风格
    /// Retrieves the window styles
    /// </summary>
    GWL_STYLE = -16,
    /// <summary>
    /// 设置与窗口有关的32位值。每个窗口均有一个由创建该窗口的应用程序使用的32位值
    /// Retrieves the user data associated with the window. This data is intended for use by the application that created the window. Its value is initially zero
    /// </summary>
    GWL_USERDATA = -21,
    /// <summary>
    /// 为窗口设定一个新的处理函数
    /// Retrieves the address of the window procedure, or a handle representing the address of the window procedure. You must use the CallWindowProc function to call the window procedure
    /// </summary>
    GWL_WNDPROC = -4,
}
复制代码

特别提醒一句 

ShowInTaskbar = false;
posted @   stweily  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
点击右上角即可分享
微信分享提示