C#實現(xiàn)WinForm全屏置頂?shù)氖纠a
應(yīng)用需求
我們在運行一些 Windows 應(yīng)用程序的時候,需要將其運行在窗體置頂?shù)哪J剑ㄊ蛊渌鼞?yīng)用窗體無法遮擋在置頂應(yīng)用窗體之上),并且進入全屏狀態(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)文章
C#中GraphicsPath的AddString方法用法實例
這篇文章主要介紹了C#中GraphicsPath的AddString方法用法,實例分析了AddString方法添加字符串的相關(guān)使用技巧,需要的朋友可以參考下2015-06-06
C#使用log4net結(jié)合sqlite數(shù)據(jù)庫實現(xiàn)記錄日志
因為結(jié)構(gòu)化的數(shù)據(jù)庫存儲的日志信息,可以寫專門的軟件讀取歷史日志信息,通過各種條件篩選,可操作性極大增強,有這方面需求的開發(fā)人員可以考慮,本文給大家介紹了C#使用log4net結(jié)合sqlite數(shù)據(jù)庫記錄日志,需要的朋友可以參考下2024-10-10
C#中面向?qū)ο缶幊虣C制之繼承學(xué)習(xí)筆記
這篇文章主要介紹了C#中面向?qū)ο缶幊虣C制之繼承學(xué)習(xí)筆記,本文給出一個簡單子實例講解C#中的繼承,并講解了一些C#繼承的知識技巧,需要的朋友可以參考下2015-01-01

