C # form Full-screen feature Instance Code _c# tutorial

Source: Internet
Author: User

Recently, a friend asked me to get him an application full screen function, such as the bank's access code interface. So I look up some methods of implementation from the Internet.

How do I achieve full-screen display in C # applications?
The effect is like Windows ' own screen saver and numerous games, regardless of whether the taskbar is set to keep the taskbar in front of other windows

Implementation Way One

Find some simple ways to achieve this online:

This. FormBorderStyle = Formborderstyle.none;  Set form to no border style

Then set the position and size of the form, such as: width=1024 height=768 left=0 top=0 value of size

Put the above two lines of code directly into the Form1_Load method, it can be, relatively simple, I do not post code.

Implementation Mode Two

Invoke the system's API functions, such as the FindWindow and ShowWindow functions in User32.dll, with the following specific code:

 [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;//tag is full screen public Form1 ()
  {InitializeComponent (); The Click event for the private void Form1_Load (object sender, EventArgs e) {}///<summary>///full Screen button/// mary>///<param name= "sender" ></param>///<param name= "E" ></param> private void Button1 
   _click (object sender, EventArgs e) {m_isfullscreen =!m_isfullscreen;//point once full screen, then point to restore. This.
   SuspendLayout ();
    if (M_isfullscreen)//Full screen, executes {setformfullscreen (M_isfullscreen) in a specific order; This.
    FormBorderStyle = Formborderstyle.none; This.
    WindowState = formwindowstate.maximized; This. Activate ()//} else//restore, executed 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 this cancels full-screen///</summary>///<param name= "fullscreen" >true: Full Screen false: Restore & lt;/param>///<param name= "Rectold" > settings, this parameter returns the original size, with this parameter set restore </param>///<returns> Settings Results < /returns> Public Boolean Setformfullscreen (boolean fullscreen)//, ref Rectangle Rectold {Rectangle rectold = R Ectangle.
   Empty;
   Int32 hwnd = 0;

   hwnd = FindWindow ("Shell_traywnd", null);//Get the handle of 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_SET
    Workarea, 0, ref rectfull, Spif_updateinifile)//Form full screen display} 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, Strin

  G lpwindowname); [DllImport ("user32.dll", EntryPoint = "SystemParametersInfo")] private static extern Int32 SystemParametersInfo (Int32 u

  Action, Int32 Uparam, ref Rectangle Lpvparam, Int32 fuWinIni); #endregion}}

Perfect code:

Thanks very much for @iheartwater's enthusiastic help, the changed code can solve "full screen, the form can be restored to its original state, including position (loaction) and size", alas, in fact, the reason is quite simple.

Modified Code public partial class Frmfullscreen:form {Boolean M_isfullscreen = false;//tag is full screen public FRMFULLSC
  Reen () {InitializeComponent (); ///<summary>///Full Screen button click event///</summary>///<param name= "sender" ></param>///&L T;param name= "E" ></param> private void Btnfullscreen_click (object sender, EventArgs e) {m_isfullscreen = 
   !m_isfullscreen;//Point once full screen, then point to restore. This.
   SuspendLayout ();
    if (M_isfullscreen)//Full screen, executes {setformfullscreen (M_isfullscreen) in a specific order; This.
    FormBorderStyle = Formborderstyle.none; This.
    WindowState = formwindowstate.maximized; This. Activate ()//} else//restore, executed 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 features, F11 equivalent to a stand-alone button, ESC health, if full screen exit full screen///</summary> <param name= "Sender" ></param>///<param name= "E" ></param> private void Btnfullscreen_key
    Down (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 this cancels full-screen///</summary>///<param name= "fullscreen" >true: Full screen F Alse: Restore </param>///<param name= "Rectold" > settings, this parameter returns the original size, with this parameter set to restore </param>///<returns> Set results </returns> public boolean setformfullscreen (Boolean fullscreen)//, ref Rectangle Rectold {Rectangle rec
   Told=rectangle.empty;
   Int32 hwnd = 0;
hwnd = FindWindow ("Shell_traywnd", null);//Get the handle of the taskbar if (hwnd = = 0) return false;
   if (fullscreen)/full screen {ShowWindow (hwnd, sw_hide);//Hide Taskbar systemparametersinfo (spi_getworkarea, 0, ref recto LD, spif_updateinifile);//get screen range Rectangle rectfull = screen.primaryscreen.bounds;//Full Screen range SystemParametersInfo (SP 

    I_setworkarea, 0, ref rectfull, Spif_updateinifile)//Form full screen display} 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 (in
  T32 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 u
  Action, 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 l

  Pwindowname); #endregion}

Form Full Screen

The way the form is full screen:

Hide the taskbar, set the work area
Maximize form, set form border style

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.