c# - 在 Windows 10 上隐藏和显示任务栏

标签 c# wpf windows interop taskbar

我有一个 wpf 应用程序,它始终处于最大化状态而不显示任务栏。 这是隐藏和显示任务栏的代码。

    [DllImport("user32.dll")]
    private static extern int FindWindow(string className, string windowText);

    [DllImport("user32.dll")]
    private static extern int ShowWindow(int hwnd, int command);

    private const int SW_HIDE = 0;
    private const int SW_SHOW = 1;

    static int hwnd = FindWindow("Shell_TrayWnd", "");

    public static new void Hide()
    {
        ShowWindow(hwnd, SW_HIDE);    
    }
    public static new void Show()
    {
        ShowWindow(hwnd, SW_SHOW);
    }

这在 Windows 7 上运行良好。但是当应用程序在 Windows 10 上运行时.. 任务栏没有通过调用 show() 再次显示。

这是我调用 show() 的部分

  #region Show Desktop
    private void Desktop_MouseUp(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left )
        {
            this.WindowState = System.Windows.WindowState.Minimized;
            Shell32.Shell objShel = new Shell32.Shell();              
            objShel.MinimizeAll();
            Show();
        }

    }

#endregion

最佳答案

这适用于主显示器,取自 here并转换为 C#。

public static class Taskbar
{
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern IntPtr FindWindow(
        string lpClassName,
        string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int SetWindowPos(
        IntPtr hWnd,
        IntPtr hWndInsertAfter,
        int x,
        int y,
        int cx,
        int cy,
        uint uFlags
    );

    [Flags]
    private enum SetWindowPosFlags : uint
    {
        HideWindow = 128,
        ShowWindow = 64
    }

    public static void Show()
    {
        var window = FindWindow("Shell_traywnd", "");
        SetWindowPos(window, IntPtr.Zero, 0, 0, 0, 0, (uint) SetWindowPosFlags.ShowWindow);
    }

    public static void Hide()
    {
        var window = FindWindow("Shell_traywnd", "");
        SetWindowPos(window, IntPtr.Zero, 0, 0, 0, 0, (uint)SetWindowPosFlags.HideWindow);
    }
}

关于c# - 在 Windows 10 上隐藏和显示任务栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52889210/

相关文章:

c# - 创建长期运行的工作流服务

c# - 如何使用powershell获取所有证书?

WPF 数据绑定(bind)和格式化

c# - WPF 中的类概念

windows - 如何通过 Windows cmd 中的进程 ID 杀死 Java 应用程序?

c# - 为什么不能总是像函数一样传递委托(delegate)

c# - 通用通信框架的模式(处理和公开接收到的数据)

wpf - 有没有办法使用 Ruby 和 WPF?

windows - 使用 -replace 从 csv 中的单元格中删除带有特殊字符的字符串

windows - 在 Windows 上将 GitHub 存储库下载为 zip,包括 CRLF