Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to show or hide the corresponding taskbar of the window with C#

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to show or hide the corresponding taskbar with C#". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

The full-screen window in WPF automatically hides the taskbar.

How does a non-full-screen window hide the taskbar? Is there even a scenario in which a hidden task is displayed by customizing a set of system taskbars?

The following will describe some concepts in stages, including the taskbar, the search window, and the control window display.

1. Home screen taskbar

The taskbar is actually a window, and the name of the taskbar on the main screen is "Shell_TrayWnd".

So you can find the window by name, and then show and hide the window.

The following are the controls for the main screen taskbar:

Public static class ScreenTaskBar {private const int SwHide = 0; / / Hidden window private const int SwRestore = 9 user32.dll / restore window [DllImport ("user32.dll")] private static extern int ShowWindow (int hwnd, int nCmdShow); [DllImport ("user32.dll")] private static extern int FindWindow (string lpClassName, string lpWindowName) / display the taskbar / public static void Show () {ShowWindow (FindWindow ("Shell_TrayWnd", null), SwRestore) } / hide the taskbar / public static void Hide () {ShowWindow (FindWindow ("Shell_TrayWnd", null), SwHide);}} 2. Multi-screen taskbar

If it is multi-screen, the scene where the taskbar is processed is generally operated on the taskbar corresponding to the window.

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

User32 has an EnumWindows function that can traverse all the windows of the current computer.

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

EnumWindowProc defines the handling of delegates. Add a callback method, and the returned parameter is handle information:

Bool OnEnumWindow (IntPtr hWnd, int lparam) {/ / add code XXX return true;}

Then you can add code inside the callback. According to the handle information of the window, we get some information about the window, such as the class name, the window title, the Bounds (location, size) of the window.

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

The following is the window class information that is partially traversed:

So, you can filter out those that end in the TrayWnd string, which are taskbar windows.

Then there is how to filter out the taskbar we want, that is, the taskbar corresponding to the window.

The window is associated with the taskbar through the screen. Get the current screen information through the window. If the Bounds of the taskbar intersects the screen Bounds, the taskbar is in this screen.

Var intPtr = new WindowInteropHelper (window) .Handle;// gets the handle of the current window var screen = Screen.FromHandle (intPtr); / / gets the current screen var currentScreenBounds = screen.Bounds; var taskBars = windows.Where (I = > i.ClassName.Contains ("TrayWnd")); var currentTaskBar = taskBars.FirstOrDefault (I = > i.Bounds.IntersectsWith (currentScreenBounds))

Get the taskbar, or you can get the screen through the handle of the taskbar to determine whether it is the same as the screen where the main window is located.

After getting the specified taskbar information, we can control the taskbar to show and hide. Call the function ShowWindow under user32:

Private const int SwHide = 0; / / Hidden window private const int SwRestore = 9 handle / restore window / through the handle, the form displays the function / form handle / / display mode / whether the return is successful [DllImport ("user32.dll", EntryPoint = "ShowWindowAsync", SetLastError = true)] public static extern bool ShowWindow (IntPtr hWnd, int cmdShow)

The ShowWindow here is different from the ShowWindow called by the default taskbar operation above, and the handle parameter is IntPtr

This is the end of the content of "how to show or hide the corresponding taskbar of the window with C#". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report