C Form Full Screen Function Instance Code
- 2021-11-13 17:53:37
- OfStack
Recently, a friend asked me to get him a full-screen application function, such as the bank number retrieval program interface. Therefore, I inquired about 1 realization method from the Internet.
How to implement full screen display in C # application?
Just like the screen saver that comes with windows and many games, the taskbar is not displayed regardless of whether it is set to "keep the taskbar in front of other windows"
Implementation Mode 1
Find some simple ways to realize it on the Internet:
this.FormBorderStyle = FormBorderStyle.None; // Set the form to a borderless style
this.WindowState = FormWindowState.Maximized; // Maximize form
Then set the position and size of the form, for example, Width=1024 Height=768 Left=0 Top=0, etc
Put the above two sentences of code directly into the method of Form1_Load, which is OK. It is relatively simple, so I will not post the code.
Implementation 2
Call the API functions of the system, such as FindWindow and ShowWindow functions in user32.dll, and the specific code is as follows:
[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);
The code is as follows:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
namespace FullScr
{
public partial class Form1 : Form
{
Boolean m_IsFullScreen = false;// Whether the tag is full screen
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// Full screen button Click Events
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
m_IsFullScreen = !m_IsFullScreen;// Point 1 Sub-full screen, then click Restore.
this.SuspendLayout();
if (m_IsFullScreen)// Full screen , Execute in a specific order
{
SetFormFullScreen(m_IsFullScreen);
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.Activate();//
}
else// Restore in a specific order-form state, form border, set taskbar and work area
{
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = FormBorderStyle.Sizable;
SetFormFullScreen(m_IsFullScreen);
this.Activate();
}
this.ResumeLayout(false);
}
/// <summary>
/// Set full screen or cancel full screen
/// </summary>
/// <param name="fullscreen">true: Full screen false: Recovery </param>
/// <param name="rectOld"> When setting, this parameter returns to the original size, and when restoring, use this parameter to set the restoration </param>
/// <returns> Setting results </returns>
public Boolean SetFormFullScreen(Boolean fullscreen)//, ref Rectangle rectOld
{
Rectangle rectOld = Rectangle.Empty;
Int32 hwnd = 0;
hwnd = FindWindow("Shell_TrayWnd", null);// Gets a handle to the taskbar
if (hwnd == 0) return false;
if (fullscreen)// Full screen
{
ShowWindow(hwnd, SW_HIDE);// Hide taskbar
SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//get Screen range
Rectangle rectFull = Screen.PrimaryScreen.Bounds;// Full screen range
SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);// Full-screen display of forms
}
else// Restore
{
ShowWindow(hwnd, SW_SHOW);// Show taskbar
SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);// Form Restore
}
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
}
}
Improved code:
Thanks very much for @ iheartwater's enthusiastic help. The changed code can solve the problem that "the form can be restored to its original state after full screen, including location (Loaction) and size (Size)". Alas, in fact, the reason is quite simple.
Modified Code
public partial class FrmFullScreen : Form
{
Boolean m_IsFullScreen = false;// Whether the tag is full screen
public FrmFullScreen()
{
InitializeComponent();
}
/// <summary>
/// Full screen button Click Events
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFullScreen_Click(object sender, EventArgs e)
{
m_IsFullScreen = !m_IsFullScreen;// Point 1 Sub-full screen, then click Restore.
this.SuspendLayout();
if (m_IsFullScreen)// Full screen , Execute in a specific order
{
SetFormFullScreen(m_IsFullScreen);
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.Activate();//
}
else// Restore in a specific order-form state, form border, set taskbar and work area
{
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = FormBorderStyle.Sizable;
SetFormFullScreen(m_IsFullScreen);
this.Activate();
}
this.ResumeLayout(false);
}
/// <summary>
/// Full screen shortcut function, F11 Equivalent to stand-alone buttons; Esc Healthy, if full screen, exit full screen
/// </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 Keyboard exits full screen
{
if (m_IsFullScreen)
{
e.Handled = true;
this.WindowState = FormWindowState.Normal;// Restore
this.FormBorderStyle = FormBorderStyle.Sizable;
SetFormFullScreen(false);
}
}
}
/// <summary>
/// Set full screen or cancel full screen
/// </summary>
/// <param name="fullscreen">true: Full screen false: Recovery </param>
/// <param name="rectOld"> When setting, this parameter returns to the original size, and when restoring, use this parameter to set the restoration </param>
/// <returns> Setting results </returns>
public Boolean SetFormFullScreen(Boolean fullscreen)//, ref Rectangle rectOld
{
Rectangle rectOld=Rectangle.Empty;
Int32 hwnd = 0;
hwnd = FindWindow("Shell_TrayWnd", null);// Gets a handle to the taskbar
if (hwnd == 0) return false;
if (fullscreen)// Full screen
{
ShowWindow(hwnd, SW_HIDE);// Hide taskbar
SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//get Screen range
Rectangle rectFull = Screen.PrimaryScreen.Bounds;// Full screen range
SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);// Full-screen display of forms
}
else// Restore
{
ShowWindow(hwnd, SW_SHOW);// Show taskbar
SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);// Form Restore
}
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
}
Form full screen
Full-screen method of form:
Hide the taskbar and set the work area
Maximize the form and set the form border style