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

时间:2018-10-19 09:08:52

标签: 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

1 个答案:

答案 0 :(得分:2)

这在主显示屏上起作用,取自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);
    }
}