窗体全屏的方法
隐藏任务栏设置工作区域
窗体最大化设置窗体边框样式
全屏窗体代码 public partial class FrmFullScreen : Form { Boolean m_IsFullScreen = false;//标记是否全屏 Rectangle rectOld = RectangleEmpty; public FrmFullScreen() { InitializeComponent(); } /// <summary> /// 全屏按钮的Click事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnFullScreen_Click(object sender EventArgs e) { m_IsFullScreen = !m_IsFullScreen;//点一次全屏再点还原 SetFormFullScreen(m_IsFullScreen); thisSuspendLayout(); if (m_IsFullScreen)//全屏 { thisWindowState = FormWindowStateMaximized; thisFormBorderStyle = FormBorderStyleNone; } else//还原 TODO:还原后的窗体应该与全屏前的大小一致 { thisWindowState = FormWindowStateNormal; thisFormBorderStyle = FormBorderStyleSizable; } thisResumeLayout(false); } /// <summary> /// 全屏的快捷功能F相当于单机按钮Esc健如果全屏则退出全屏 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnFullScreen_KeyDown(object sender KeyEventArgs e) { if (eKeyCode == KeysF) { btnFullScreenPerformClick(); eHandled = true; } else if (eKeyCode == KeysEscape)//esc键盘退出全屏 { if (m_IsFullScreen) { eHandled = true; SetFormFullScreen(false); thisWindowState = FormWindowStateNormal;//还原 thisFormBorderStyle = FormBorderStyleSizable; } } } /// <summary> /// 设置全屏或这取消全屏 /// </summary> /// <param name="fullscreen">true:全屏 false:恢复</param> /// <param name="rectOld">设置的时候此参数返回原始尺寸恢复时用此参数设置恢复</param> /// <returns>设置结果</returns> public Boolean SetFormFullScreen(Boolean fullscreen)// ref Rectangle rectOld { Rectangle rectOld=RectangleEmpty; Int hwnd = ; hwnd = FindWindow("Shell_TrayWnd" null);//获取任务栏的句柄 if (hwnd == ) return false; if (fullscreen)//全屏 { ShowWindow(hwnd SW_HIDE);//隐藏任务栏 SystemParametersInfo(SPI_GETWORKAREA ref rectOld SPIF_UPDATEINIFILE);//get 屏幕范围 Rectangle rectFull = ScreenPrimaryScreenBounds;//全屏范围 SystemParametersInfo(SPI_SETWORKAREA ref rectFull SPIF_UPDATEINIFILE);//窗体全屏幕显示 } else//还原 { ShowWindow(hwnd SW_SHOW);//显示任务栏 SystemParametersInfo(SPI_SETWORKAREA ref rectOld SPIF_UPDATEINIFILE);//窗体还原 } return true; } #region userdll [DllImport("userdll" EntryPoint = "ShowWindow")] public static extern Int ShowWindow(Int hwnd Int nCmdShow); public const Int SW_SHOW = ; public const Int SW_HIDE = ; [DllImport("userdll" EntryPoint = "SystemParametersInfo")] private static extern Int SystemParametersInfo(Int uAction Int uParam ref Rectangle lpvParam Int fuWinIni); public const Int SPIF_UPDATEINIFILE = x; public const Int SPI_SETWORKAREA = ; public const Int SPI_GETWORKAREA = ; [DllImport("userdll" EntryPoint = "FindWindow")] private static extern Int FindWindow(string lpClassName string lpWindowName); #endregion }