Back to Home

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

pinvoke · c · windows api · .net

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

    There were already several articles on Habr that talk about using the P / Invoke mechanism in C # projects. Basically, the articles had a strong bias towards theory and cited small indicative examples.
    I want to show a more obvious example, showing the capabilities of unmanaged code - we will hide the Start button and the taskbar.

    For the first time, I learned about the ability to use Windows system functions in my programs when I studied Visual Basic. This topic has captured me so much that I began to collect examples of using the Windows API functions in the collection. At first I collected examples all in a row. Then he began to approach the matter more legibly and began to select those functions and examples that he used in his projects. Over the course of several years, I typed more than 500 functions, and I designed all the examples for them as a CHM file for convenience. The demo version of the guide can still be downloaded from the site http://rusproject.narod.ru/guide.htm .
    When the .NET Framework and the C # and Visual Basic.NET languages ​​appeared, it smoothly migrated to these languages. And here I found my reference to the Windows API functions very useful. As they say in many smart books, the C # language has incorporated the best of Java, C ++, Visual Basic. Regarding P / Invoke, we can say that the technology for invoking unmanaged code from managed code was taken from Visual Basic. If in C ++ the call to system functions is transparent for the programmer, then for C # the required function needs to be declared (analogue of 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 could not find a worthwhile application 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 of ​​the Windows device, is effective in the eyes of beginning programmers and gives a more complete idea of ​​the functions used.
    So, let's start with the theory. Firstly, the Start button, taskbar, notification area and clock area are all windows. So, having gained access to such a window, you can change the usual behavior of an interface element. Second, 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:
    • Shell_TrayWnd - taskbar
    • Button - Start Button
    • TrayNotifyWnd - notification area with icons and a clock
    • TrayClockWClass - watch

    Now let's move on to the functions. For our purposes, we will need the FindWindows, FindWindowEx and ShowWindow functions. I will not provide descriptions of functions here - you can find out about them on the Internet or in my reference manual. 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. Using the FindWindow and FindWindowEx functions, the desired descriptors are found by the class name, which are then used in the ShowWindow function to hide or show the required 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 disappear at the same time. If you wish, you can comment out individual lines of code and hide only the taskbar or only the Start button. Pressing the button again will allow you to see your favorite Start button and the 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 descriptor
    
    IntPtr startWnd = FindWindowEx(taskBarWnd, IntPtr.Zero, "BUTTON", null);
    


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

    Probably you can’t do without screenshots. Here I hid the taskbar, but left the Start button.

    And here I hid only the clock area.


    A few words about the Start button. In Windows 98 / XP, you could hide the button without any problems (see note above). In Windows Vista, a round button appeared instead of a rectangular button and its behavior changed. It is no longer a child window of the taskbar. And the code given here works halfway. You can hide the taskbar and Start at the same time, or just the taskbar. But you cannot hide the Start button, but you cannot leave the taskbar. The button does not disappear, but gets crowded. In search of 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 designed for American Windows 7, there is no effect on Russian Windows (an attempt to change the word Start to Start in the code was unsuccessful).

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

    Read Next