代码改变世界

C#窗体全屏功能实例

2013-01-02 13:33  ArcScofield  阅读(20837)  评论(13编辑  收藏  举报

概要:简要介绍窗体全屏的方法,列出实现窗体的全屏功能以及全屏功能的快捷键的代码;

 ps:非常感谢@iheartwater的热心帮助,代码已做完善,请直接查看修改后的代码

导航:  


1.窗体全屏

窗体全屏的方法:

  1. 隐藏任务栏、设置工作区域
  2. 窗体最大化、设置窗体边框样式

2.代码

全屏窗体代码
  1   public partial class FrmFullScreen : Form
  2     {
  3         Boolean m_IsFullScreen = false;//标记是否全屏
  4         Rectangle rectOld = Rectangle.Empty;
  5         public FrmFullScreen()
  6         {
  7             InitializeComponent();
  8         }
  9         /// <summary>
 10         /// 全屏按钮的Click事件
 11         /// </summary>
 12         /// <param name="sender"></param>
 13         /// <param name="e"></param>
 14         private void btnFullScreen_Click(object sender, EventArgs e)
 15         {
 16             m_IsFullScreen = !m_IsFullScreen;//点一次全屏,再点还原。  
 17     
 18             SetFormFullScreen(m_IsFullScreen);
 19 
 20             this.SuspendLayout();
 21             if (m_IsFullScreen)//全屏 
 22             {
 23                 this.WindowState = FormWindowState.Maximized; 
 24                 this.FormBorderStyle = FormBorderStyle.None;
 25             }
 26             else//还原 TODO:还原后的窗体应该与全屏前的大小一致
 27             {
 28                 this.WindowState = FormWindowState.Normal;
 29                 this.FormBorderStyle = FormBorderStyle.Sizable;
 30             }
 31             this.ResumeLayout(false);
 32         }
 33         /// <summary>
 34         /// 全屏的快捷功能,F11相当于单机按钮;Esc健,如果全屏则退出全屏
 35         /// </summary>
 36         /// <param name="sender"></param>
 37         /// <param name="e"></param>
 38         private void btnFullScreen_KeyDown(object sender, KeyEventArgs e)
 39         {
 40             if (e.KeyCode == Keys.F11)
 41             {
 42                 btnFullScreen.PerformClick();
 43                 e.Handled = true;
 44             }
 45             else if (e.KeyCode == Keys.Escape)//esc键盘退出全屏
 46             {
 47                 if (m_IsFullScreen)
 48                 {
 49                     e.Handled = true;
 50                     SetFormFullScreen(false);
 51                     this.WindowState = FormWindowState.Normal;//还原  
 52                     this.FormBorderStyle = FormBorderStyle.Sizable;
 53                 }
 54             }
 55         }
 56         /// <summary>  
 57         /// 设置全屏或这取消全屏  
 58         /// </summary>  
 59         /// <param name="fullscreen">true:全屏 false:恢复</param>  
 60         /// <param name="rectOld">设置的时候,此参数返回原始尺寸,恢复时用此参数设置恢复</param>  
 61         /// <returns>设置结果</returns>  
 62         public Boolean SetFormFullScreen(Boolean fullscreen)//, ref Rectangle rectOld
 63         {
 64             Rectangle rectOld=Rectangle.Empty;
 65             Int32 hwnd = 0;
 66             hwnd = FindWindow("Shell_TrayWnd", null);//获取任务栏的句柄
 67 
 68             if (hwnd == 0) return false;
 69 
 70             if (fullscreen)//全屏
 71             {
 72                 ShowWindow(hwnd, SW_HIDE);//隐藏任务栏
 73 
 74                 SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//get  屏幕范围
 75                 Rectangle rectFull = Screen.PrimaryScreen.Bounds;//全屏范围
 76                 SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);//窗体全屏幕显示
 77             }
 78             else//还原 
 79             {
 80                 ShowWindow(hwnd, SW_SHOW);//显示任务栏
 81 
 82                 SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//窗体还原
 83             }
 84             return true;
 85         }
 86 
 87         #region user32.dll
 88 
 89         [DllImport("user32.dll", EntryPoint = "ShowWindow")]
 90         public static extern Int32 ShowWindow(Int32 hwnd, Int32 nCmdShow);
 91         public const Int32 SW_SHOW = 5; public const Int32 SW_HIDE = 0;
 92 
 93         [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
 94         private static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, ref Rectangle lpvParam, Int32 fuWinIni);
 95         public const Int32 SPIF_UPDATEINIFILE = 0x1;
 96         public const Int32 SPI_SETWORKAREA = 47;
 97         public const Int32 SPI_GETWORKAREA = 48;
 98 
 99         [DllImport("user32.dll", EntryPoint = "FindWindow")]
100         private static extern Int32 FindWindow(string lpClassName, string lpWindowName);
101 
102         #endregion
103     }

窗体:

 

3.完善后的代码:

非常感谢@iheartwater的热心帮助,更改后的代码能够解决”全屏后,窗体能够恢复到原来的状态,包括位置(Loaction)和大小(Size)“,唉,其实,原因还挺简单的。

Modified Code
  1  public partial class FrmFullScreen : Form
  2     {
  3         Boolean m_IsFullScreen = false;//标记是否全屏
  4 
  5         public FrmFullScreen()
  6         {
  7             InitializeComponent();
  8         }
  9         /// <summary>
 10         /// 全屏按钮的Click事件
 11         /// </summary>
 12         /// <param name="sender"></param>
 13         /// <param name="e"></param>
 14         private void btnFullScreen_Click(object sender, EventArgs e)
 15         {
 16             m_IsFullScreen = !m_IsFullScreen;//点一次全屏,再点还原。  
 17             this.SuspendLayout();
 18             if (m_IsFullScreen)//全屏 ,按特定的顺序执行
 19             {
 20                 SetFormFullScreen(m_IsFullScreen);
 21                 this.FormBorderStyle = FormBorderStyle.None;
 22                 this.WindowState = FormWindowState.Maximized;
 23                 this.Activate();//
 24             }
 25             else//还原,按特定的顺序执行——窗体状态,窗体边框,设置任务栏和工作区域
 26             {
 27                 this.WindowState = FormWindowState.Normal;
 28                 this.FormBorderStyle = FormBorderStyle.Sizable;
 29                 SetFormFullScreen(m_IsFullScreen);
 30                 this.Activate();
 31             }
 32             this.ResumeLayout(false);
 33         }
 34         /// <summary>
 35         /// 全屏的快捷功能,F11相当于单机按钮;Esc健,如果全屏则退出全屏
 36         /// </summary>
 37         /// <param name="sender"></param>
 38         /// <param name="e"></param>
 39         private void btnFullScreen_KeyDown(object sender, KeyEventArgs e)
 40         {
 41             if (e.KeyCode == Keys.F11)
 42             {
 43                 btnFullScreen.PerformClick();
 44                 e.Handled = true;
 45             }
 46             else if (e.KeyCode == Keys.Escape)//esc键盘退出全屏
 47             {
 48                 if (m_IsFullScreen)
 49                 {
 50                     e.Handled = true;
 51                     this.WindowState = FormWindowState.Normal;//还原  
 52                     this.FormBorderStyle = FormBorderStyle.Sizable;
 53                     SetFormFullScreen(false);
 54                 }
 55             }
 56         }
 57         /// <summary>  
 58         /// 设置全屏或这取消全屏  
 59         /// </summary>  
 60         /// <param name="fullscreen">true:全屏 false:恢复</param>  
 61         /// <param name="rectOld">设置的时候,此参数返回原始尺寸,恢复时用此参数设置恢复</param>  
 62         /// <returns>设置结果</returns>  
 63         public Boolean SetFormFullScreen(Boolean fullscreen)//, ref Rectangle rectOld
 64         {
 65             Rectangle rectOld=Rectangle.Empty;
 66             Int32 hwnd = 0;
 67             hwnd = FindWindow("Shell_TrayWnd", null);//获取任务栏的句柄
 68 
 69             if (hwnd == 0) return false;
 70 
 71             if (fullscreen)//全屏
 72             {
 73                 ShowWindow(hwnd, SW_HIDE);//隐藏任务栏
 74 
 75                 SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//get  屏幕范围
 76                 Rectangle rectFull = Screen.PrimaryScreen.Bounds;//全屏范围
 77                 SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);//窗体全屏幕显示
 78             }
 79             else//还原 
 80             {
 81                 ShowWindow(hwnd, SW_SHOW);//显示任务栏
 82 
 83                 SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//窗体还原
 84             }
 85             return true;
 86         }
 87 
 88         #region user32.dll
 89 
 90         [DllImport("user32.dll", EntryPoint = "ShowWindow")]
 91         public static extern Int32 ShowWindow(Int32 hwnd, Int32 nCmdShow);
 92         public const Int32 SW_SHOW = 5; public const Int32 SW_HIDE = 0;
 93 
 94         [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
 95         private static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, ref Rectangle lpvParam, Int32 fuWinIni);
 96         public const Int32 SPIF_UPDATEINIFILE = 0x1;
 97         public const Int32 SPI_SETWORKAREA = 47;
 98         public const Int32 SPI_GETWORKAREA = 48;
 99 
100         [DllImport("user32.dll", EntryPoint = "FindWindow")]
101         private static extern Int32 FindWindow(string lpClassName, string lpWindowName);
102 
103         #endregion
104     }

 

 4.参考:

 http://blog.csdn.net/wuyazhe/article/details/5728286

 SystemParametersinfo百度百科