程序在全屏时,通常任务栏是不隐藏的,特殊要求下,我们需要让程序真正全屏,也就是全屏时隐藏任务栏,退出全屏时再显示。比如给学生上课的时候,不想让他们看到任务栏。下面就是实现这个功能的方法。
效果如图:新建项目,窗体添加2个按钮:代码如下:using System.Runtime.InteropServices;
namespace FullScreen
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 引用WINAPI
[DllImport("user32.dll", EntryPoint = "ShowWindow")]
public static extern int ShowWindow(int hwnd, int nCmdShow);
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
private static extern int SystemParametersInfo(int uAction, int uParam, ref Rectangle lpvParam, int fuWinIni);
public const int SPIF_UPDATEINIFILE = 0x1;
public const int SPI_SETWORKAREA = 47;
public const int SPI_GETWORKAREA = 48;
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
#endregion
Rectangle rect = new Rectangle();
//全屏显示标识
public const int SW_SHOW = 5;
//正常显示标识
public const int SW_HIDE = 0;
private void button1_Click(object sender, EventArgs e)
{
//全屏
ShowAsFullScreen(ref rect, SW_HIDE);
this.WindowState = FormWindowState.Maximized;
}
private void button2_Click(object sender, EventArgs e)
{
//取消全屏
ShowAsFullScreen(ref rect, SW_SHOW);
this.WindowState = FormWindowState.Normal;
}
public bool ShowAsFullScreen(ref Rectangle rectOld, int Mode)
{
int Hwnd = 0;
Hwnd = FindWindow("Shell_TrayWnd", null);
if (Hwnd == 0) return false;
ShowWindow(Hwnd, Mode);
Rectangle rectFull = Screen.PrimaryScreen.Bounds;
SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);
SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);
return true;
}
}
}
当然,如果为了让学生不能做其它操作,禁用键鼠是必要的,这个以后再讨论。