窗體全屏的方法:
隱藏任務欄、設置工作區域
窗體最大化、設置窗體邊框樣式
全屏窗體代碼 public partial class FrmFullScreen : Form { Boolean m_IsFullScreen = false;//標記是否全屏 Rectangle rectOld = Rectangle.Empty; 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); this.SuspendLayout(); if (m_IsFullScreen)//全屏 { this.WindowState = FormWindowState.Maximized; this.FormBorderStyle = FormBorderStyle.None; } else//還原 TODO:還原後的窗體應該與全屏前的大小一致 { this.WindowState = FormWindowState.Normal; this.FormBorderStyle = FormBorderStyle.Sizable; } this.ResumeLayout(false); } /// <summary> /// 全屏的快捷功能,F11相當於單機按鈕;Esc健,如果全屏則退出全屏 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnFullScreen_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.F11) { btnFullScreen.PerformClick(); e.Handled = true; } else if (e.KeyCode == Keys.Escape)//esc鍵盤退出全屏 { if (m_IsFullScreen) { e.Handled = true; SetFormFullScreen(false); this.WindowState = FormWindowState.Normal;//還原 this.FormBorderStyle = FormBorderStyle.Sizable; } } } /// <summary> /// 設置全屏或這取消全屏 /// </summary> /// <param name="fullscreen">true:全屏 false:恢復</param> /// <param name="rectOld">設置的時候,此參數返回原始尺寸,恢復時用此參數設置恢復</param> /// <returns>設置結果</returns> public Boolean SetFormFullScreen(Boolean fullscreen)//, ref Rectangle rectOld { Rectangle rectOld=Rectangle.Empty; Int32 hwnd = 0; hwnd = FindWindow("Shell_TrayWnd", null);//獲取任務欄的句柄 if (hwnd == 0) return false; if (fullscreen)//全屏 { ShowWindow(hwnd, SW_HIDE);//隱藏任務欄 SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//get 屏幕范圍 Rectangle rectFull = Screen.PrimaryScreen.Bounds;//全屏范圍 SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);//窗體全屏幕顯示 } else//還原 { ShowWindow(hwnd, SW_SHOW);//顯示任務欄 SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//窗體還原 } return true; } #region user32.dll [DllImport("user32.dll", EntryPoint = "ShowWindow")] public static extern Int32 ShowWindow(Int32 hwnd, Int32 nCmdShow); public const Int32 SW_SHOW = 5; public const Int32 SW_HIDE = 0; [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")] private static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, ref Rectangle lpvParam, Int32 fuWinIni); public const Int32 SPIF_UPDATEINIFILE = 0x1; public const Int32 SPI_SETWORKAREA = 47; public const Int32 SPI_GETWORKAREA = 48; [DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern Int32 FindWindow(string lpClassName, string lpWindowName); #endregion }