开发者

C# hide taskbar in Windows 7

开发者 https://www.devze.com 2023-02-28 04:43 出处:网络
I am trying to hide the Windows 7 taskbar in C#. I looked at this post andthispage, but after following all the instructions, I ended up with a plain black bar at the bottom.

I am trying to hide the Windows 7 taskbar in C#. I looked at this post and this page, but after following all the instructions, I ended up with a plain black bar at the bottom. I ended up with a taskbar class like this:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

public static class Taskbar
{
    [DllImport("user32.dll")]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern bool EnumThreadWindows(int threadId, EnumThreadProc pfnEnum, IntPtr lParam);
    [DllImport("user32.dll", SetLastError = true)]
    private static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
    [DllImport("user32.dll")]
    private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
    [DllImport("user32.dll")]
    private static extern uint GetWindowThreadProcessId(IntPtr hwnd, out int lpdwProcessId);

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

    private const string VistaStartMenuCaption = "Start";
    private static IntPtr vistaStartMenuWnd = IntPtr.Zero;
    private delegate bool EnumThreadProc(IntPtr hwnd, IntPtr lParam);

    public static void Show()
    {
        SetVisibility(true);
    }

    public static void Hide()
    {
        SetVisibility(false);
    }

    public static bool Visible
    {
        set { SetVisibility(value); }
    }

    private static void SetVisibility(bool show)
    {
        IntPtr taskBarWnd = FindWindow("Shell_TrayWnd", null);

        IntPtr startWnd = FindWindowEx(taskBarWnd, IntPtr.Zero, "Button", "Start");
        if (startWnd == IntPtr.Zero)
        {
            startWnd = FindWindow("Button", null);

            if (startWnd == IntPtr.Zero)
            {
                startWnd = GetVistaStartMenuWnd(taskBarWnd);
            }
        }

        ShowWindow(taskBarWnd, show ? SW_SHOW : SW_HIDE);
        ShowWindow(startWnd, show ? SW_SHOW : SW_HIDE);
    }

    private static IntPtr GetVistaStartMenuWnd(IntPtr taskBarWnd)
    {
        int procId;
        GetWindowThreadProcessId(taskBarWnd, out procId);

        Process p = Process.GetProcessById(procId);
        if (p != null)
        {
            foreach (ProcessThread t in p.Threads)
            {
                EnumThreadWindows(t.Id, MyEnumThreadWindowsProc, IntPtr.Zero);
            }
        }
        return vistaStartMenuWnd;
    }

    private static bool MyEnumThreadWindowsProc(IntPtr hWnd, IntPtr lParam)
    {
        StringBuilder buffer = new StringBuilder(256);
        if (GetWindowText(hWnd, buffer, buffer.Capacity) > 0)
        {
            Console.WriteLine(buffer);
            if (buffer.ToString() == VistaStartMenuCaption)
            {
                vistaStartMenuWnd = hWnd;
                return false;
            }
        }
        return true;
    }
}

I've taken a screenshot of the problem:

Thank开发者_StackOverflow社区 you in advance.


It would make more sense to me to show your app full screen. Poking at the internals of the taskbar like this is surely going to cause trouble.

Users are likely going to be annoyed at losing their taskbar. The taskbar belongs to the user not the app.


Do you try this?

  • ITaskbarList::AddTab
  • ITaskbarList::DeleteTab

And also pinvoke.net: ITaskbarList

FROM MSDN:

Version 4.71 and later of Shell32.dll adds the capability to modify the contents of the taskbar. From an application, you can now add, remove, and activate taskbar buttons. Activating the item does not activate the window; it shows the item as pressed on the taskbar.

The taskbar modification capabilities are implemented in a Component Object Model (COM) object (CLSID_TaskbarList) that exposes the ITaskbarList interface (IID_ITaskbarList). You must call the ITaskbarList::HrInit method to initialize the object. You can then use the methods of the ITaskbarList interface to modify the contents of the taskbar.


If you have a WPF window, set its WindowStyle to None, and maximize it, it covers the whole screen (covering the TaskBar). If you use Windows Forms, something similar may also work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号