C#表單全屏功能執行個體代碼_C#教程

來源:互聯網
上載者:User

最近有朋友讓我給他弄個應用程式全屏的功能,例如銀行的取號程式介面。所以我從網上查詢了一些實現的方法。

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 }

表單全屏

表單全屏的方法:

隱藏工作列、設定工作區域
表單最大化、設定表單邊框樣式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.