Hide TaskBar in WinForms Application [duplicate]

2019-02-10 19:36发布

This question already has an answer here:

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?

3条回答
混吃等死
2楼-- · 2019-02-10 19:54

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();
查看更多
▲ chillily
3楼-- · 2019-02-10 20:00

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;
    }
查看更多
Ridiculous、
4楼-- · 2019-02-10 20:08

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

查看更多
登录 后发表回答