C#窗体全屏功能实例代码

所属分类: 软件编程 / C#教程 阅读数: 98
收藏 0 赞 0 分享

最近有朋友让我给他弄个应用程序全屏的功能,例如银行的取号程序界面。所以我从网上查询了一些实现的方法。

C#应用程序中如何实现全屏幕显示功能?
效果就像windows自带的屏幕保护程序和众多的游戏那样,无论是否设置了“将任务栏保持在其他窗口的前端”都不显示任务栏

实现方式一

在网上找来一些简单的实现方式:

this.FormBorderStyle = FormBorderStyle.None;  //设置窗体为无边框样式
this.WindowState = FormWindowState.Maximized; //最大化窗体 

然后再设置窗体的位置和大小,例如:Width=1024 Height=768 Left=0 Top=0等size的值

把以上两句代码直接放到Form1_Load的方法中,就可以了,比较简单,我就不贴代码了。

实现方式二

调用系统的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

 }
}

完善后的代码:

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

Modified Code
 public partial class FrmFullScreen : Form
 {
  Boolean m_IsFullScreen = false;//标记是否全屏

  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;//点一次全屏,再点还原。 
   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>
  /// 全屏的快捷功能,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;
     this.WindowState = FormWindowState.Normal;//还原 
     this.FormBorderStyle = FormBorderStyle.Sizable;
     SetFormFullScreen(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

  [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
 }

窗体全屏

窗体全屏的方法:

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

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多