C#窗体全屏功能
2013-01-02 19:32:13 来源:WEB开发网核心提示: 窗体全屏的方法:隐藏任务栏、设置工作区域窗体最大化、设置窗体边框样式全屏窗体代码 public partial class FrmFullScreen : Form { Boolean m_IsFullScreen = false;//标记是否全屏 Rectangle re
窗体全屏的方法:
隐藏任务栏、设置工作区域
窗体最大化、设置窗体边框样式
全屏窗体代码 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 }
- ››功能强更实用 Win7库变为“计算机”
- ››功能最全!网管必备子网划分计算工具!
- ››功能完整的 Google Reader 离线浏览器 Byline 介绍...
- ››窗体中字段顺序不同于Access 2007中记录源中字段顺...
- ››功能才是硬道理 色彩风暴图片处理
- ››窗体中字段顺序不同于 Access 2007 中记录源中字段...
- ››功能完备 Google Talk新版试用体验
- ››功能区工具栏基本操作——PowerPoint 2007多媒体课...
- ››功能强大桌面便签YNotes新版发布
- ››功能再改进!Foxmail 6.5Beta2发布
- ››功能完善的Java连接池调用实例
- ››功能强大的全新 T-SQL 语法极大地提升了 SQL Serv...
更多精彩
赞助商链接