您的位置:首页 > 编程语言 > C#

c#隐藏/显示Windows任务栏

2009-04-16 22:12 411 查看
Windows的任务栏实际上是一个没有标题的窗体,所以只要找到这个窗体,就能隐藏和显示了

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

        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr trayHwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);//在窗口列表中寻找与指定条件相符的第一个子窗口,如果未找到返回0
            //IntPtr trayHwnd = FindWindow("Shell_TrayWnd", null);//寻找窗口列表中第一个符合指定条件的顶级窗口,可用api函数GetWindowText取得这个窗口的名称
			/*hwnd
			The handle of the window to change the show status of. 
			nCmdShow
			Exactly one of the following flags specifying how to show the window: 

			SW_HIDE = 0
			Hide the window. 
			SW_MAXIMIZE = 3
			Maximize the window. 
			SW_MINIMIZE = 6
			Minimize the window. 
			SW_RESTORE = 9
			Restore the window (not maximized nor minimized). 
			SW_SHOW = 5
			Show the window. 
			SW_SHOWMAXIMIZED = 3
			Show the window maximized. 
			SW_SHOWMINIMIZED = 2
			Show the window minimized. 
			SW_SHOWMINNOACTIVE = 7
			Show the window minimized but do not activate it. 
			SW_SHOWNA = 8
			Show the window in its current state but do not activate it. 
			SW_SHOWNOACTIVATE = 4
			Show the window in its most recent size and position but do not activate it. 
			SW_SHOWNORMAL = 1
			Show the window and activate it (as usual).*/
            if (button1.Text == "隐藏")
            {
                if (trayHwnd != IntPtr.Zero)
                {
                    ShowWindow(trayHwnd, 0);
                    button1.Text = "显示";
                }

            }
            else
            {
                ShowWindow(trayHwnd,1);
                button1.Text = "隐藏";
            }
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: