wpf中全屏窗口,会自动隐藏任务栏。

那非全屏窗口如何隐藏任务栏?甚至有没有一种场景,隐藏任务后自定义一套系统任务栏来显示?

以下会分阶段讲述一些概念,任务栏、查找窗口、控制窗口显示。

1. 主屏任务栏

任务栏,其实也是一个窗口,主屏的任务栏名称是”shell_traywnd”。

所以可以通过名称查找窗口,然后对窗口进行显示、隐藏操作。

以下是主屏幕任务栏的控制操作:

public static class screentaskbar
    {
        private const int swhide = 0; //隐藏窗口
        private const int swrestore = 9;//还原窗口

        [dllimport("user32.dll")]
        private static extern int showwindow(int hwnd, int ncmdshow);
        [dllimport("user32.dll")]
        private static extern int findwindow(string lpclassname, string lpwindowname);
        /// <summary>
        /// 显示任务栏
        /// </summary>
        public static void show()
        {
            showwindow(findwindow("shell_traywnd", null), swrestore);
        }
        /// <summary>
        /// 隐藏任务栏
        /// </summary>
        public static void hide()
        {
            showwindow(findwindow("shell_traywnd", null), swhide);
        }
    }

2.多屏任务栏

如果是多屏,对任务栏进行处理的场景,一般是对窗口所对应的任务栏操作。

如何获取任意窗口所在的任务栏呢?既然任务栏也是窗口,那么我们的关注点就是如何找到任务栏窗口了。

user32有enumwindows函数,可以遍历当前电脑的所有窗口。

 private delegate bool enumwindowproc(intptr hwnd, int lparam);
     [dllimport("user32")]
    private static extern bool enumwindows(enumwindowproc lpenumfunc, int lparam);

enumwindowproc定义了委托的处理。添加回调方法,返回的参数是句柄信息:

bool onenumwindow(intptr hwnd, int lparam)
     {
        //添加代码xxx
         return true;
     }

然后可以在回调内部添加代码,根据窗口的句柄信息,我们去拿窗口的一些信息,类名、窗口标题、窗口的bounds(位置、大小)

[dllimport("user32")]
    private static extern int getclassname(intptr hwnd, stringbuilder lpstring, int nmaxcount);
    [dllimport("user32")]
    private static extern int getwindowtext(intptr hwnd, stringbuilder lptrstring, int nmaxcount);
    [dllimport("user32")]
    private static extern bool getwindowrect(intptr hwnd, ref lprect rect);

以下是部分遍历出来的窗口类信息:

所以,可以筛选出那些以traywnd字符串结尾的,这些都是任务栏窗口。

之后就是如何筛选出我们想要的任务栏,即窗口对应的任务栏。

窗口与任务栏,是通过屏幕关联在一起的。通过窗口获取当前屏幕信息,任务栏的bounds如果与屏幕bounds相交,则说明此任务栏在此屏幕内。

 var intptr = new windowinterophelper(window).handle;//获取当前窗口的句柄
     var screen = screen.fromhandle(intptr);//获取当前屏幕
     var currentscreenbounds = screen.bounds;
     var taskbars = windows.where(i => i.classname.contains("traywnd"));
     var currenttaskbar = taskbars.firstordefault(i => i.bounds.intersectswith(currentscreenbounds));

获取任务栏,也可以通过任务栏的句柄获取屏幕,与主窗口所在屏幕判断是否同一个。

获取了指定的任务栏信息后,我们就可以控制任务栏显示、隐藏了。调用下user32下函数showwindow即可:

private const int swhide = 0; //隐藏窗口
    private const int swrestore = 9;//还原窗口
    /// <summary>
    /// 通过句柄,窗体显示函数
    /// </summary>
    /// <param name="hwnd">窗体句柄</param>
    /// <param name="cmdshow">显示方式</param>
    /// <returns>返回成功与否</returns>
    [dllimport("user32.dll", entrypoint = "showwindowasync", setlasterror = true)]
    public static extern bool showwindow(intptr hwnd, int cmdshow);

这里的showwindow,与上面默认任务栏操作所调用的showwindow不一样,句柄参数是intptr

到此这篇关于c# 显示、隐藏窗口对应的任务栏的文章就介绍到这了,更多相关c# 显示隐藏窗口任务栏内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!