📜 ⬆️ ⬇️

Using P / Invoke: hiding the Start button and the taskbar in Windows

On Habré already there were several articles telling about use of the P / Invoke mechanism in projects on C #. Basically, the articles had a strong bias towards the theory and gave small illustrative examples.
I want to show a more illustrative example showing the possibilities of unmanaged code - we will hide the Start button and the taskbar.

For the first time, I learned about the ability to use the Windows system functions in my programs when learning Visual Basic. This topic so captured me that I began to collect examples of using the Windows API functions in the collection. First, I collected examples of everything. Then he began to approach the matter more legibly and began to select those functions and examples that he used in his projects. For several years, I scored more than 500 functions, and all the examples to them for convenience, designed in the form of a CHM-file. The demo version of the reference book can still be downloaded from http://rusproject.narod.ru/guide.htm .
When the .NET Framework and the C # and Visual Basic .NET languages ​​appeared, it migrated smoothly to these languages. And this is where my Windows API reference book came in handy. As written in many smart books, the C # language has incorporated the best of Java, C ++, Visual Basic. Regarding P / Invoke, it can be said that the technology of invoking unmanaged code from managed code was taken from Visual Basic. If in C ++ the call of system functions is transparent to the programmer, then for C # the required function must be declared (analogue of the Declare in VB 6.0). From my collection of examples, I decided to show a project that allows you to hide the Start button and the taskbar in Windows XP / Vista / 7.

In fact, the example does not have any practical value (in any case, I was not able to find a worthwhile use for it). The only thing that comes to mind is the creation of a comic program that on April 1 will hide the usual interface elements on the table of an unprepared user. But, on the other hand, the example gives some idea about the Windows device, is effective in the eyes of novice programmers and gives a more complete picture of the functions used.
So let's start with the theory. First, the Start button, the taskbar, the notification area and the clock area are all windows. So, getting access to this window, you can change the usual behavior of the interface element. The second is that all these windows belong to certain classes. And, precisely, by the name of the classes, you can find the descriptors of the windows we need to do experiments on them. Here are the class names:

Now go to the functions. For our purposes, we need the FindWindows, FindWindowEx, and ShowWindow functions. I will not give here descriptions of functions - you can independently learn about them on the Internet or in my directory. I will give only their declarations and a couple of related constants:
// , ,
// , ,
[DllImport( "user32.dll" )]
private static extern IntPtr FindWindow( string ClassName, string WindowName);

[DllImport( "user32.dll" )]
private static extern IntPtr FindWindowEx(
IntPtr hwndParent, IntPtr hwndChildAfter,
string className, string windowName);

[DllImport( "user32.dll" )]
private static extern bool ShowWindow( IntPtr hWnd, int nCmdShow);

const int SW_HIDE = 0;
const int SW_SHOW = 5;


* This source code was highlighted with Source Code Highlighter .


And then everything is very simple. The FindWindow and FindWindowEx functions use the class name to find the necessary descriptors, which are then used in the ShowWindow function to hide or show the desired element.
')
bool show = false ;

//
private void butHideTaskbar_Click( object sender, EventArgs e)
{
//
IntPtr taskBarWnd = FindWindow( "Shell_TrayWnd" , null );
//
IntPtr startWnd = FindWindow( "Button" , null );

// /
if (taskBarWnd != IntPtr .Zero)
{
ShowWindow(taskBarWnd, show ? SW_SHOW : SW_HIDE);
}

// /
if (startWnd != IntPtr .Zero)
{
ShowWindow(startWnd, show ? SW_SHOW : SW_HIDE);
}
show = !show;
}

//
private void butHide_Click( object sender, EventArgs e)
{
//
IntPtr taskBarWnd = FindWindow( "Shell_TrayWnd" , null );
//ShowWindow(taskBarWnd, SW_HIDE);

//
IntPtr tray = FindWindowEx(taskBarWnd, IntPtr .Zero, "TrayNotifyWnd" , null );

//
// ShowWindow(tray, SW_HIDE);

//
IntPtr trayclock = FindWindowEx(tray, IntPtr .Zero, "TrayClockWClass" , null );
//
ShowWindow(trayclock, SW_HIDE);
}

* This source code was highlighted with Source Code Highlighter .


For example, I placed two buttons on the form. When you click on the first button, the taskbar and the Start button simultaneously disappear. If you wish, you can comment out individual lines of code and hide only the taskbar or just the Start button. Pressing the button again will allow you to see your favorite Start button and taskbar again.

Upd. An important addition: in Windows XP and below, the Start button is a child window of the taskbar, and you need to use the code to get its handle.
  <code>
 IntPtr startWnd = FindWindowEx (taskBarWnd, IntPtr.Zero, "BUTTON", null);
 </ code> 


When you click on the second button, you can hide certain elements of the taskbar - I commented out part of the code, leaving only the hiding area with the clock. Be careful — an example is written for educational purposes. If you hide the clock, then they themselves will not appear. You will have to restart your computer to return everything as it was. Therefore, independently modify the example in order to be able to show hidden elements.

Without screenshots, probably not enough. Here I hid the taskbar, but left the Start button

And here I hid only the area of ​​the clock.


A few words about the Start button. In Windows 98 / XP it was possible to hide the button without any problems (see the note above). In Windows Vista, instead of a rectangular button, a round button appeared and its behavior changed. It is no longer a child window of the taskbar. And the code given here works in half. You can hide both the taskbar and Start, or just the taskbar. And hide Start, but leave the taskbar does not work. The button does not disappear, but it shrinks. Looking for a solution to the problem, I came across a couple of articles on CodeProject ( http://www.codeproject.com/KB/miscctrl/Hide_Vista_Start_Orb_Simp.aspx and http://www.codeproject.com/KB/miscctrl/hide_vista_start_orb.aspx . But, Unfortunately, these examples did not work for me. They are intended for American Windows 7, there is no effect on Russian Windows (there was no success in changing Start code to Start).

This and other examples with Windows API functions in the .NET Framework can be found in my reference book at http://developer.alexanderklimov.ru/guide.php . The reference book itself is paid, but if you have free time, you can find examples of any function on the Internet (Google will help you). And also I can advise a very useful resource in English pinvoke.net: the interop wiki! on which, on the Wikipedia principle, enthusiasts add examples related to P / Invoke.
Happy programming!

Source: https://habr.com/ru/post/68613/


All Articles