C#应用程序中如何实现全屏幕显示功能?
效果就像windows自带的屏幕保护程序和众多的游戏那样,无论是否设置了“将任务栏保持在其他窗口的前端”都不显示任务栏
实现方式一
在网上找来一些简单的实现方式:
this.FormBorderStyle = FormBorderStyle.None; //设置窗体为无边框样式 this.WindowState = FormWindowState.Maximized; //最大化窗体
或者
this.TopMost= True; //设置窗体为最前端显示 this.WindowState = FormWindowState.Maximized; //最大化窗体
把以上两句代码直接放到Form1_Load的方法中,就可以了,比较简单,我就不贴代码了。
参考出处:http://blog.csdn.net/friendan/article/details/7350570
============================================================
实现方式二
调用系统的API函数,如user32.dll中的FindWindow和ShowWindow函数,具体代码如下:
[DllImport("user32.dll", EntryPoint = "ShowWindow")] public static extern Int32 ShowWindow(Int32 hwnd, Int32 nCmdShow); [DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern Int32 FindWindow(string lpClassName, string lpWindowName);
代码如下:
using System; using System.Windows.Forms; using System.Drawing; using System.Runtime.InteropServices; namespace FullScr { public partial class Form1 : Form { Boolean m_IsFullScreen = false;//标记是否全屏 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } /// <summary> /// 全屏按钮的Click事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { m_IsFullScreen = !m_IsFullScreen;//点一次全屏,再点还原。 this.SuspendLayout(); if (m_IsFullScreen)//全屏 ,按特定的顺序执行 { SetFormFullScreen(m_IsFullScreen); this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; this.Activate();// } else//还原,按特定的顺序执行——窗体状态,窗体边框,设置任务栏和工作区域 { this.WindowState = FormWindowState.Normal; this.FormBorderStyle = FormBorderStyle.Sizable; SetFormFullScreen(m_IsFullScreen); this.Activate(); } this.ResumeLayout(false); } /// <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 public const Int32 SPIF_UPDATEINIFILE = 0x1; public const Int32 SPI_SETWORKAREA = 47; public const Int32 SPI_GETWORKAREA = 48; public const Int32 SW_SHOW = 5; public const Int32 SW_HIDE = 0; [DllImport("user32.dll", EntryPoint = "ShowWindow")] public static extern Int32 ShowWindow(Int32 hwnd, Int32 nCmdShow); [DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern Int32 FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")] private static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, ref Rectangle lpvParam, Int32 fuWinIni); #endregion } }
实现方式三
窗体最大化,不占用任务栏,具体实现如下:
this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea; //设置最大化的大小为工作区域
this.WindowState = FormWindowState.Maximized;
参考出处: http://www.cnblogs.com/ArcScofield/archive/2013/01/02/Form_Fullscreen.html