国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#窗體全屏功能實例代碼

2019-10-29 21:18:49
字體:
來源:轉載
供稿:網友

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

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#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铜陵市| 遵化市| 湛江市| 峨眉山市| 华坪县| 菏泽市| 黄石市| 太白县| 射洪县| 高邑县| 凤翔县| 通海县| 通辽市| 辉县市| 蛟河市| 邳州市| 五台县| 旺苍县| 贡山| 新津县| 柳林县| 新沂市| 沭阳县| 罗甸县| 吉木萨尔县| 花莲县| 丰镇市| 吉隆县| 宁明县| 梅河口市| 厦门市| 屏东市| 霍山县| 保德县| 横峰县| 香格里拉县| 嘉祥县| 九龙坡区| 昌宁县| 石首市| 定南县|