C#使用Windows API 隐藏/显示 任务栏 (FindWindowEx, ShowWindow)

今天,有网友询问,如何显示和隐藏任务栏?

我这里,发布一下使用Windows API 显示和隐藏 Windows 的任务栏的方法,windows 的任务栏,其实是一个窗口(window),只要找到这个窗口的句柄,显示和隐藏就轻而易举了,任务栏是个没有标题的窗口,但它的类名是 Shell_TrayWnd,所以,可以用FindWindow 或 FindWindowEx 去查找它的句柄,而显示和隐藏窗口,使用的是 ShowWindow:

  • 引入Windows API 的声明
[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
  • 显示/隐藏任务栏窗口
IntPtr trayHwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);
if (trayHwnd != IntPtr.Zero)
{
    ShowWindow(trayHwnd, 0);
}

上面的代码中, ShowWindow 的第二参数, 1 表示显示, 0 表示隐藏

posted @ 2009-03-13 14:13  三角猫  阅读(2485)  评论(5编辑  收藏  举报