C#窗體全屏功能實(shí)例代碼
最近有朋友讓我給他弄個(gè)應(yīng)用程序全屏的功能,例如銀行的取號(hào)程序界面。所以我從網(wǎng)上查詢了一些實(shí)現(xiàn)的方法。
C#應(yīng)用程序中如何實(shí)現(xiàn)全屏幕顯示功能?
效果就像windows自帶的屏幕保護(hù)程序和眾多的游戲那樣,無論是否設(shè)置了“將任務(wù)欄保持在其他窗口的前端”都不顯示任務(wù)欄
實(shí)現(xiàn)方式一
在網(wǎng)上找來一些簡(jiǎn)單的實(shí)現(xiàn)方式:
this.FormBorderStyle = FormBorderStyle.None; //設(shè)置窗體為無邊框樣式 this.WindowState = FormWindowState.Maximized; //最大化窗體
然后再設(shè)置窗體的位置和大小,例如:Width=1024 Height=768 Left=0 Top=0等size的值
把以上兩句代碼直接放到Form1_Load的方法中,就可以了,比較簡(jiǎn)單,我就不貼代碼了。
實(shí)現(xiàn)方式二
調(diào)用系統(tǒng)的API函數(shù),如user32.dll中的FindWindow和ShowWindow函數(shù),具體代碼如下:
[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;//標(biāo)記是否全屏 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;//點(diǎn)一次全屏,再點(diǎn)還原。 this.SuspendLayout(); if (m_IsFullScreen)//全屏 ,按特定的順序執(zhí)行 { SetFormFullScreen(m_IsFullScreen); this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; this.Activate();// } else//還原,按特定的順序執(zhí)行——窗體狀態(tài),窗體邊框,設(shè)置任務(wù)欄和工作區(qū)域 { this.WindowState = FormWindowState.Normal; this.FormBorderStyle = FormBorderStyle.Sizable; SetFormFullScreen(m_IsFullScreen); this.Activate(); } this.ResumeLayout(false); } /// <summary> /// 設(shè)置全屏或這取消全屏 /// </summary> /// <param name="fullscreen">true:全屏 false:恢復(fù)</param> /// <param name="rectOld">設(shè)置的時(shí)候,此參數(shù)返回原始尺寸,恢復(fù)時(shí)用此參數(shù)設(shè)置恢復(fù)</param> /// <returns>設(shè)置結(jié)果</returns> public Boolean SetFormFullScreen(Boolean fullscreen)//, ref Rectangle rectOld { Rectangle rectOld = Rectangle.Empty; Int32 hwnd = 0; hwnd = FindWindow("Shell_TrayWnd", null);//獲取任務(wù)欄的句柄 if (hwnd == 0) return false; if (fullscreen)//全屏 { ShowWindow(hwnd, SW_HIDE);//隱藏任務(wù)欄 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);//顯示任務(wù)欄 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的熱心幫助,更改后的代碼能夠解決”全屏后,窗體能夠恢復(fù)到原來的狀態(tài),包括位置(Loaction)和大?。⊿ize)“,唉,其實(shí),原因還挺簡(jiǎn)單的。
Modified Code public partial class FrmFullScreen : Form { Boolean m_IsFullScreen = false;//標(biāo)記是否全屏 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;//點(diǎn)一次全屏,再點(diǎn)還原。 this.SuspendLayout(); if (m_IsFullScreen)//全屏 ,按特定的順序執(zhí)行 { SetFormFullScreen(m_IsFullScreen); this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; this.Activate();// } else//還原,按特定的順序執(zhí)行——窗體狀態(tài),窗體邊框,設(shè)置任務(wù)欄和工作區(qū)域 { this.WindowState = FormWindowState.Normal; this.FormBorderStyle = FormBorderStyle.Sizable; SetFormFullScreen(m_IsFullScreen); this.Activate(); } this.ResumeLayout(false); } /// <summary> /// 全屏的快捷功能,F(xiàn)11相當(dāng)于單機(jī)按鈕;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> /// 設(shè)置全屏或這取消全屏 /// </summary> /// <param name="fullscreen">true:全屏 false:恢復(fù)</param> /// <param name="rectOld">設(shè)置的時(shí)候,此參數(shù)返回原始尺寸,恢復(fù)時(shí)用此參數(shù)設(shè)置恢復(fù)</param> /// <returns>設(shè)置結(jié)果</returns> public Boolean SetFormFullScreen(Boolean fullscreen)//, ref Rectangle rectOld { Rectangle rectOld=Rectangle.Empty; Int32 hwnd = 0; hwnd = FindWindow("Shell_TrayWnd", null);//獲取任務(wù)欄的句柄 if (hwnd == 0) return false; if (fullscreen)//全屏 { ShowWindow(hwnd, SW_HIDE);//隱藏任務(wù)欄 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);//顯示任務(wù)欄 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 }
窗體全屏
窗體全屏的方法:
隱藏任務(wù)欄、設(shè)置工作區(qū)域
窗體最大化、設(shè)置窗體邊框樣式
上一篇:微信公眾平臺(tái)開發(fā)教程(三) 基礎(chǔ)框架搭建
欄 目:C#教程
下一篇:C#在winform中實(shí)現(xiàn)數(shù)據(jù)增刪改查等功能
本文標(biāo)題:C#窗體全屏功能實(shí)例代碼
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6151.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法
- 01-10C#實(shí)現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10C#實(shí)現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復(fù)原窗體的方
- 01-10C#實(shí)現(xiàn)更改MDI窗體背景顏色的方法
- 01-10C#實(shí)現(xiàn)打開畫圖的同時(shí)載入圖片、最大化顯示畫圖窗體的方法
- 01-10C#實(shí)現(xiàn)將窗體固定在顯示器的左上角且不能移動(dòng)的方法
- 01-10C#禁用雙擊窗體圖標(biāo)關(guān)閉窗體的方法
- 01-10WinForm實(shí)現(xiàn)窗體最大化并遮蓋任務(wù)欄的方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?