7

How can I hide the Windows taskbar when I run my C# WinForms application?

I tried some code, but it opens in maximized view with the taskbar.

Do you have any sample code or suggestions?

Archit
  • 630
  • 3
  • 10
  • 29
Hetvi
  • 132
  • 1
  • 1
  • 8
  • `ShowInTaskbar = false;` – Altivo Nov 26 '15 at 14:19
  • Why on earth do you want to hide the taskbar? If you want to take your app fullscreen, just resize it to fill the screen and the taskbar will get out of your way. See https://devblogs.microsoft.com/oldnewthing/20050505-04/?p=35703. – Joe White Jun 23 '20 at 20:17

3 Answers3

21

Just add this class into your project .it works as you expected.

using System;
using System.Runtime.InteropServices;

public class 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);

    [DllImport("user32.dll")]
    public static extern int FindWindowEx(int parentHandle, int childAfter, string className, int windowTitle);

    [DllImport("user32.dll")]
    private static extern int GetDesktopWindow();

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

    protected static int Handle
    {
        get
        {
            return FindWindow("Shell_TrayWnd", "");
        }
    }

    protected static int HandleOfStartButton
    {
        get
        {
            int handleOfDesktop = GetDesktopWindow();
            int handleOfStartButton = FindWindowEx(handleOfDesktop, 0, "button", 0);
            return handleOfStartButton;
        }
    }

    private Taskbar()
    {
        // hide ctor
    }

    public static void Show()
    {
        ShowWindow(Handle, SW_SHOW);
        ShowWindow(HandleOfStartButton, SW_SHOW);
    }

    public static void Hide()
    {
        ShowWindow(Handle, SW_HIDE);
        ShowWindow(HandleOfStartButton, SW_HIDE);
    }
}

USAGE:

Taskbar.Hide();
Thilina H
  • 5,754
  • 6
  • 26
  • 56
  • 3
    Exists any way that works to hide taskbar on second monitor – Randall Sandoval Nov 01 '16 at 19:04
  • Yes thats also possible. It is named `Shell_SecondaryTrayWnd`. I think you should be able to adjust the code above accordingly. Keep the fact in mind, that it can be `null` on older systems or if there is no secondary screen present. – Christoph Meißner Dec 02 '19 at 16:09
  • This removes the Taskbar, but the space taken up by the taskbar is still remains. It just shows as the background color, but no window can expand beyond it. Is there a way to completely hide the taskbar so that all windows will maximize to the full extent of the screen? – ChE Junkie Feb 28 '21 at 18:16
  • @ChEJunkie All I did was set my Taskbar to Auto Hide. Then when I use this, the windows can use the full screen real estate. – esac Oct 14 '22 at 16:48
4

You need to set WinForms application from property like below

private void Form1_Load(object sender, EventArgs e)
    {
        this.TopMost = true;
        this.FormBorderStyle = FormBorderStyle.None;
        this.WindowState = FormWindowState.Maximized;
    }
Archit
  • 630
  • 3
  • 10
  • 29
3

You need to use P/INVOKE

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

int hwnd = FindWindow("Shell_TrayWnd","");
ShowWindow(hwnd,SW_HIDE);

I hope that helps

Saddam Abu Ghaida
  • 6,381
  • 2
  • 22
  • 29