Search code examples
c#wpfwindowsinteroptaskbar

Hide and Show taskbar on windows 10


I have a wpf application which is in maximized state always without showing taskbar. Here is the code for Hiding and showing taskbar.

    [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);
    }

This is working fine on windows 7. But when application runs on Windows 10.. taskbar didnt show up again by calling show().

Here is the part where I am calling 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

Solution

  • This works on the main display and is taken from here and converted to 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);
        }
    }