WinForm调用user32.dll实现全屏

摘要:
使用系统;使用System.Collections.Generic;使用System.ComponentModel;使用System.Data;使用System.Drawing;使用System.Linq;使用System.Runtime.InteropServices;使用System.Text;使用System.Threading。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//Boolean IsFullScreen = false;

private void Form1_Load(object sender, EventArgs e)
{

}

#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 int1, Int32 int2);

[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern Int32 FindWindow(string str1, string str2);

[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern Int32 SystemParametersInfo(Int32 int1, Int32 int2, ref Rectangle ref1, Int32 int3);

#endregion

public Boolean SetFullScreen(Boolean b)
{
Rectangle rectOld = Rectangle.Empty;

Int32 int1 = 0;

int1 = FindWindow("Shell_TrayWnd", null);

if (int1 == 0) return false;

if (b)
{
ShowWindow(int1, SW_HIDE);

SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//屏幕范围

Rectangle rectFull = Screen.PrimaryScreen.Bounds;//全屏范围

SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);//全屏幕显示
}
else//还原
{
ShowWindow(int1, SW_SHOW);//任务栏

SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//还原
}

return true;
}

private void button1_Click(object sender, EventArgs e)
{
//全屏

this.SuspendLayout();

SetFullScreen(true);

this.FormBorderStyle = FormBorderStyle.None;

this.WindowState = FormWindowState.Maximized;

this.Activate();

this.ResumeLayout(false);
}

private void button2_Click(object sender, EventArgs e)
{
//还原

this.WindowState = FormWindowState.Normal;

this.FormBorderStyle = FormBorderStyle.Sizable;

SetFullScreen(false);

this.Activate();

this.ResumeLayout(false);
}
}
}

免责声明:文章转载自《WinForm调用user32.dll实现全屏》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇flutter PopupMenuButton弹出式菜单列表NSLookup命令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

如何创建、发布Web Service和 C#WINFORM里如何调用WEB Service API

一、创建和发布Web Service        Web服务方法中可以返回一个DataSet对象        WEB服务可以说是下一代WEB应用程序的基础,无论客户端是WINDOWS应用、ASP.NET Web Form程序、甚至其他语言的客户端,都可以与同一个WEB服务通信,其平台和语言无关性使其有了广阔的发展空间。 利用VS2005和IIS我们可...

Winform开发框架之单据窗体生成(主从表,流水单号)

项目源码下载地址:https://github.com/GarsonZhang/GZFramework.Demo 前言 1.在开始本节前请先重置代码为 chapter-03-start 懒人地址:https://github.com/GarsonZhang/GZFramework.ShareDemo/tree/chapter-03-start 2.创建表...

WinForm 进程、线程

一、进程 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动。 它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体。 Process 类,用来操作进程。 命名空间:using System.Diagnostics; Process.Start("calc");   //打开计算器Process.Start("mspaint");   /...

在Winform项目和Web API的.NetCore项目中使用Serilog 来记录日志信息

在我们常规的调试或者测试的时候,喜欢把一些测试信息打印在控制台或者记录在文件中,对于.netframework项目来说,我们输出控制台的日志信息习惯的用Console.WriteLine来输出查看,不过对于.netcore的项目来说,这种输出看不到任何信息了,不过即使这样,我们建议还是把一些测试的日志记录在文件中,供查看对比。本篇随笔介绍使用Serilog...

解决winform中mdi子窗体加载时显示最大化最小化按钮的方法

场景:在mid加载子窗体的时候如果指定WindowState为Maximized,加载完成后主窗体会显示最大化、最小化、关闭的按钮图标。 解决方法: 1.更改主窗体FormMain的属性。制定MainMenuStrip的属性为menuStrip1控件。menuStrip1控件就是主窗体上的菜单栏。 2.在menuStrip1控件的ItemAdded事件中添...

Winform开发常用控件之DataGridView的简单数据绑定——代码绑定DataSet、DataTable、IList、SqlDataReader

前文介绍了Winform为DataGridView提供的数据自动绑定功能,下面介绍一下采用代码的数据绑定 1、用DataSet和DataTable为DataGridView提供数据源 先上代码 private void Form1_Load(objectsender, EventArgs e) { String st...