当前位置:网站首页>C displays and hides the taskbar corresponding to the window

C displays and hides the taskbar corresponding to the window

2021-10-14 05:06:06 Tang Song Yuan Ming Qing 2188

WPF Full screen window in , Will automatically hide the taskbar .

How to hide the taskbar in a non full screen window ? Is there even a scene , After hiding the taskbar, customize a set of system taskbar to display ?

Some concepts will be described in stages below , The task bar 、 Search window 、 Control window display .

1. Main screen taskbar

The task bar , In fact, it is also a window , The name of the taskbar on the main screen is "Shell_TrayWnd".

So you can find the window by name , Then display the window 、 Hide operations .

The following is the control operation of the main screen taskbar :

 1     public static class ScreenTaskBar
 2     {
 3         private const int SwHide = 0; // Hidden window 
 4         private const int SwRestore = 9;// Restore  window 
 5 
 6         [DllImport("user32.dll")]
 7         private static extern int ShowWindow(int hwnd, int nCmdShow);
 8         [DllImport("user32.dll")]
 9         private static extern int FindWindow(string lpClassName, string lpWindowName);
10         /// <summary>
11         ///  Show taskbar 
12         /// </summary>
13         public static void Show()
14         {
15             ShowWindow(FindWindow("Shell_TrayWnd", null), SwRestore);
16         }
17         /// <summary>
18         ///  Hide taskbar 
19         /// </summary>
20         public static void Hide()
21         {
22             ShowWindow(FindWindow("Shell_TrayWnd", null), SwHide);
23         }
24     }

2. Multi screen taskbar

If it's multi screen , The scenario of processing the taskbar , Generally, it is used to operate the taskbar corresponding to the window .

How to get the taskbar where any window is located ? Since the taskbar is also a window , So our focus is how to find the taskbar window .

User32 Yes EnumWindows function , You can traverse all windows of the current computer .

1     private delegate bool EnumWindowProc(IntPtr hWnd, int lParam);
2     [DllImport("user32")]
3     private static extern bool EnumWindows(EnumWindowProc lpEnumFunc, int lParam);

EnumWindowProc Defines the handling of delegates . Add callback method , The parameters returned are handle information :

1     bool OnEnumWindow(IntPtr hWnd, int lparam)
2     {
3         // Add code XXX
4         return true;
5     }

You can then add code inside the callback , According to the handle information of the window , Let's get some information from the window , Class name 、 Window title 、 Window Bounds( Location 、 size )

1     [DllImport("user32")]
2     private static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
3     [DllImport("user32")]
4     private static extern int GetWindowText(IntPtr hWnd, StringBuilder lptrString, int nMaxCount);
5     [DllImport("user32")]
6     private static extern bool GetWindowRect(IntPtr hWnd, ref LPRECT rect);

The following is some of the window class information traversed :

therefore , You can filter out those to TrayWnd At the end of a string , These are taskbar windows .

After that is how to filter out the taskbar we want , That is, the taskbar corresponding to the window .

Windows and taskbar , Are linked together through the screen . Get the current screen information through the window , Taskbar Bounds If with the screen Bounds The intersection , This means that the taskbar is on this screen .

1     var intPtr = new WindowInteropHelper(window).Handle;// Get the handle of the current window 
2     var screen = Screen.FromHandle(intPtr);// Get the current screen 
3     var currentScreenBounds = screen.Bounds;
4     var taskBars = windows.Where(i => i.ClassName.Contains("TrayWnd"));
5     var currentTaskBar = taskBars.FirstOrDefault(i => i.Bounds.IntersectsWith(currentScreenBounds));

Get taskbar , You can also get the screen through the handle of the taskbar , Determine whether it is the same as the screen where the main window is located .

After obtaining the specified taskbar information , We can control the taskbar display 、 Hide the . Call next user32 Lower function ShowWindow that will do :

 1     private const int SwHide = 0; // Hidden window 
 2     private const int SwRestore = 9;// Restore window 
 3     /// <summary>
 4     ///  Through handle , Form display function 
 5     /// </summary>
 6     /// <param name="hWnd"> Form handle </param>
 7     /// <param name="cmdShow"> Display mode </param>
 8     /// <returns> Return success or not </returns>
 9     [DllImport("user32.dll", EntryPoint = "ShowWindowAsync", SetLastError = true)]
10     public static extern bool ShowWindow(IntPtr hWnd, int cmdShow);

  there ShowWindow, Same as that called by the default taskbar operation above ShowWindow Dissimilarity , The handle parameter is IntPtr 

版权声明
本文为[Tang Song Yuan Ming Qing 2188]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/10/20211002145410606y.html

随机推荐