您的位置:首页 > 编程语言 > C#

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

2016-05-16 16:19 1036 查看
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);



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: