Hide Start Button and TaskBar in Win7 or WinXP by Using C#

    If we expect that a winform program is shown in full screen mode without the title bar, the start button and the taskbar, we can use the following codes:
private void Form1_Load(object sender, EventArgs e)
{
   this.SetVisibleCore(false);
   this.FormBorderStyle = FormBorderStyle.None;
   this.WindowState = FormWindowState.Maximized;
   this.SetVisibleCore(true);
}

    I tried to use such codes in Win7. In most cases this method can work. Occasionally the start button and taskbar are shown. This phenomenon is annoying. On Stack Overflow website a programmer suggested using the following codes:

private void Form1_Load(object sender, EventArgs e)
{
this.SetVisibleCore(false);
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Normal;
this.WindowState = FormWindowState.Maximized;
this.SetVisibleCore(true);
}

    It's a trick to assign Normal to WindowState firstly. The start button and taskbar are shown with a lower probability. This method seems to be better in my computer.
    When I tried to create another process in the C# winform program, the new process would launch a new program window. When the screen switched to the new window, the start button and taskbar were show again. I searched the solution to avoiding this over the internet, and found there was a good way to hide start button and taskbar in Visual C++. The code snippet is as follows:

/**********************************************************/
void HideStartButtonInWin7(bool bHide)
/* Parameter:          */
/* bHide -- true    hide start button in Windows 7 */
/*       -- false   show start button in Windows 7 */
{
 int nCmdShow;
 HWND hWnd;

 hWnd = FindWindow(_T("Button"),NULL);
 if(bHide == true)
 {
  nCmdShow = SW_HIDE;
 }
 else
 {
  nCmdShow = SW_SHOW;
 }

 ShowWindow(hWnd,nCmdShow);
}

/**********************************************************/
void HideStartButtonInWinXp(bool bHide)
/* Parameter:          */
/* bHide -- true    hide start button in Windows XP */
/*       -- false   show start button in Windows XP */
{
 int nCmdShow;

 if(bHide == true)
 {
  nCmdShow = SW_HIDE;
 }
 else
 {
  nCmdShow = SW_SHOW;
 }

 ShowWindow(GetDlgItem(FindWindow(_T("Shell_TrayWnd"), NULL),0x130), nCmdShow); //0x130 is ID of start button in WinXP
}

/**********************************************************/
void HideTaskBar(bool bHide)
/* Parameter:          */
/* bHide -- true    hide windows taskbar */
/*       -- false   show windows taskbar */

{
 int nCmdShow;
 HWND hWnd;
 hWnd = FindWindow(_T("Shell_TrayWnd"),NULL);
 if(bHide == true)
 {
  nCmdShow = SW_HIDE;
 }
 else
 {
  nCmdShow = SW_SHOW;
 }
}

    A direct way in C# couldn't be found. So I thought the ultimate way in C# was to invoke Windows API in the similar way. I created a file named by 'HideTaskBarAndStartButton.cs'. Its content is as follows:

/**************************************************   
* Author: HAN Wei   
* Author's blog: http://blog.csdn.net/henter/   
* Date: May 16th, 2016 
* Description: demonstrate how to hide and retrive
*              taskbar and start button in WinXp and Win7
**************************************************/ 

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace HideTaskBarAndStartButtonTools
{
    class HideAndRetrive
    {
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string ClassName, string WindowName);

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

        [DllImport("user32.dll")]
        private static extern IntPtr GetDlgItem(IntPtr WindowHandle, int nIDDlgItem);

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

        /**********************************************************/
        /* HideStartButtonInWin7 
         * Parameter
         * bHide -- true     hide start button in Win7
         *       -- false    retrieve start button in Win7       */
        /**********************************************************/
        public static void HideStartButtonInWin7(bool bHide)
        {
            int nCmdShow;
            IntPtr hWnd;

            hWnd = FindWindow("Button", null);
            if (bHide == true)
            {
                nCmdShow = SW_HIDE;
            }
            else
            {
                nCmdShow = SW_SHOW;
            }
            ShowWindow(hWnd, nCmdShow);
        }

        /**********************************************************/
        /* HideStartButtonInWinXP
         * Parameter
         * bHide -- true     hide start button in Win7
         *       -- false    retrieve start button in Win7       */
        /**********************************************************/
        public static void HideStartButtonInWinXP(bool bHide)
        {
            int nCmdShow;

            if (bHide == true)
            {
                nCmdShow = SW_HIDE;
            }
            else
            {
                nCmdShow = SW_SHOW;
            }
            ShowWindow(GetDlgItem(FindWindow("Shell_TrayWnd", null), 0x130), nCmdShow); //0x130 is ID of start button in WinXP
        }

        /**********************************************************/
        /* HideTaskBar
         * Parameter
         * bHide -- true     hide windows taskbar
         *       -- false    retrieve windows taskbar             */
        /**********************************************************/
        public static void HideTaskBar(bool bHide)
        {
            int nCmdShow;
            IntPtr hWnd;

            hWnd = FindWindow("Shell_TrayWnd", null);
            if (bHide == true)
            {
                nCmdShow = SW_HIDE;
            }
            else
            {
                nCmdShow = SW_SHOW;
            }
            ShowWindow(hWnd, nCmdShow);
        }
    }
}

When you want to hide start button and the taskbar in C#, you can use the following codes:

In Win7:

using HideTaskBarAndStartButtonTools;


HideAndRetrive.HideStartButtonInWin7(true);
HideAndRetrive.HideTaskBar(true);

In WinXP:

using HideTaskBarAndStartButtonTools;

HideAndRetrive.HideStartButtonInWinXP(true);
HideAndRetrive.HideTaskBar(true);

When you want to retrieve start button and the taskbar, you can use the following codes:

In Win7:

using HideTaskBarAndStartButtonTools;

HideAndRetrive.HideStartButtonInWin7(false);
HideAndRetrive.HideTaskBar(false);

In WinXP:

using HideTaskBarAndStartButtonTools;

HideAndRetrive.HideStartButtonInWinXP(false);
HideAndRetrive.HideTaskBar(false);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值