C#實現(xiàn)WinForm全屏置頂?shù)氖纠a
應(yīng)用需求
我們在運行一些 Windows 應(yīng)用程序的時候,需要將其運行在窗體置頂?shù)哪J剑ㄊ蛊渌鼞?yīng)用窗體無法遮擋在置頂應(yīng)用窗體之上),并且進(jìn)入全屏狀態(tài)。本文將介紹如何使用 C# 來實現(xiàn) WinForm 的全屏置頂?shù)幕竟δ堋?/p>
基本功能主要實現(xiàn)以下幾點:
(1)改變WinForm的一些外觀屬性,包括無邊框、最大化和置頂屬性。
(2)屏蔽一些鍵盤操作,以阻止關(guān)閉應(yīng)用程序或切換到其它的應(yīng)用程序?;究梢云帘巫笥襑IN菜單鍵、關(guān)閉窗口組合鍵(Alt+F4)、切換窗口組合鍵(Alt+Tab)、開始菜單鍵(Ctrl+Esc)。
設(shè)計
設(shè)計 CraneofficeWinLock 類,該類可以實現(xiàn)一些方法,自動設(shè)置 WinForm 的一些屬性、屏蔽一些鍵盤操作,其主要設(shè)計如下表:
序號 | 名稱 | 成員類型 | 類型 | 說明 |
---|---|---|---|---|
1 | form | 屬性 | System.Windows.Forms | 指定要自動設(shè)置屬性的窗體 |
2 | OnKeyPress | 方法 | void | 處理屏蔽鍵盤操作的方法 |
3 | Start | 方法 | void | 主入口方法,啟動程序,需要傳遞OnKeyPress方法句柄 |
4 | Stop | 方法 | void | 停止所有屏蔽操作 |
范例運行環(huán)境
操作系統(tǒng): Windows 11、Windows 10 、Windows 2019 Server
.net版本: .netFramework4.7.2 或以上
開發(fā)工具:VS2019 C#
運行設(shè)備:Microsoft Surface Pro
實現(xiàn)代碼
核心代碼
代碼如下:
public class CraneofficeWinLock { private const int WH_KEYBOARD_LL = 13; //鍵盤 public Form form = null; private delegate int HookHandle(int nCode, int wParam, IntPtr lParam); public delegate void ProcessKeyHandle(HookStruct param, out bool handle); private static int _hHookValue = 0; private HookHandle _KeyBoardHookProcedure; [StructLayout(LayoutKind.Sequential)] public class HookStruct { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; } [DllImport("user32.dll")] private static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] private static extern bool UnhookWindowsHookEx(int idHook); [DllImport("user32.dll")] private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam); [DllImport("kernel32.dll")] private static extern int GetCurrentThreadId(); [DllImport("kernel32.dll")] private static extern IntPtr GetModuleHandle(string name); private IntPtr _hookWindowPtr = IntPtr.Zero; public CraneofficeWinLock() { } private static ProcessKeyHandle _clientMethod = null; public void Start(ProcessKeyHandle clientMethod) { if (form != null) { form.WindowState = FormWindowState.Maximized; form.FormBorderStyle = FormBorderStyle.None; form.TopMost = true; } _clientMethod = clientMethod; if (_hHookValue == 0) { _KeyBoardHookProcedure = new HookHandle(OnHookProc); _hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); _hHookValue = SetWindowsHookEx( WH_KEYBOARD_LL, _KeyBoardHookProcedure, _hookWindowPtr, 0); //如果設(shè)置鉤子失敗. if (_hHookValue == 0) Stop(); } } public void Stop() { if (_hHookValue != 0) { bool ret = UnhookWindowsHookEx(_hHookValue); if (ret) _hHookValue = 0; } } private static int OnHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); if (_clientMethod != null) { bool handle = false; _clientMethod(hookStruct, out handle); if (handle) return 1; //1:表示鍵盤,return 退出 } } return CallNextHookEx(_hHookValue, nCode, wParam, lParam); } public void OnKeyPress(CraneofficeWinLock.HookStruct hookStruct, out bool handle) { handle = false; if (hookStruct.vkCode == 91) // 左win(開始菜單鍵) { handle = true; } if (hookStruct.vkCode == 92)// 右win { handle = true; } if (hookStruct.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control) { handle = true; } if (hookStruct.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt) { handle = true; } if (hookStruct.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt) { handle = true; } if (hookStruct.vkCode == (int)Keys.F1) { handle = true; } Keys key = (Keys)hookStruct.vkCode; } }
調(diào)用示例
示例代碼如下:
private CodnHBuilder.CraneofficeWinLock _winlock = null; private void Form1_Load(object sender, EventArgs e) { _winlock = new CodnHBuilder.CraneofficeWinLock(); _winlock.form = this; _winlock.Start(_winlock.OnKeyPress); }
小結(jié)
我們可以在退出代碼中停止屏蔽的操作,如下代碼:
if (_winlock != null) _winlock.Stop(); Application.Exit();
另外,為防止一些其它未考慮的情況,比較懶,寫了一個計時器(時長1000毫秒)代碼,實時激活窗體的狀態(tài),以保持窗體永遠(yuǎn)在最上層,如下代碼:
private void timer1_Tick(object sender, EventArgs e) { this.Activate(); }
到此這篇關(guān)于C#實現(xiàn)WinForm全屏置頂?shù)氖纠a的文章就介紹到這了,更多相關(guān)C# WinForm全屏置頂內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WinForm實現(xiàn)為ComboBox綁定數(shù)據(jù)源并提供下拉提示功能
這篇文章主要介紹了WinForm實現(xiàn)為ComboBox綁定數(shù)據(jù)源并提供下拉提示功能,是非常實用的功能,需要的朋友可以參考下2014-08-08C#實現(xiàn)一鍵換IP、重置DNS、網(wǎng)關(guān)及掩碼的方法
這篇文章主要介紹了C#實現(xiàn)一鍵換IP、重置DNS、網(wǎng)關(guān)及掩碼的方法,很實用的功能,需要的朋友可以參考下2014-07-07