C# winform 無法屏蔽win 按鍵,系統是server 2008r2
使用了鉤子,注冊表等業務無法屏蔽win按鍵 求個方法
uj5u.com熱心網友回復:
public delegate int HookProc(int nCode, int wParam, IntPtr lParam);private static int hHook = 0;
public const int WH_KEYBOARD_LL = 13;
//LowLevel鍵盤截獲,如果是WH_KEYBOARD=2,并不能對系統鍵盤截取,會在你截取之前獲得鍵盤。
private static HookProc KeyBoardHookProcedure;
//鍵盤Hook結構函式
[StructLayout(LayoutKind.Sequential)]
public class KeyBoardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
//設定鉤子
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
//抽掉鉤子
public static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll")]
//呼叫下一個鉤子
public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
public static extern int GetCurrentThreadId();
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string name);
public static void Hook_Start()
{
// 安裝鍵盤鉤子
if (hHook == 0)
{
KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardHookProcedure,
GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
//如果設定鉤子失敗.
if (hHook == 0)
{
Hook_Clear();
}
}
}
//取消鉤子事件
public static void Hook_Clear()
{
bool retKeyboard = true;
if (hHook != 0)
{
retKeyboard = UnhookWindowsHookEx(hHook);
hHook = 0;
}
//如果去掉鉤子失敗.
if (!retKeyboard) throw new Exception("UnhookWindowsHookEx failed.");
}
//這里可以添加自己想要的資訊處理
private static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
{
if (nCode >= 0)
{
KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
if (kbh.vkCode == 91) // 截獲左win(開始選單鍵)
{
return 1;
}
if (kbh.vkCode == 92)// 截獲右win
{
return 1;
}
if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control) //截獲Ctrl+Esc
{
return 1;
}
if (kbh.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt) //截獲alt+f4
{
return 1;
}
if (kbh.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt) //截獲alt+tab
{
return 1;
}
if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Shift) //截獲Ctrl+Shift+Esc
{
return 1;
}
if (kbh.vkCode == (int)Keys.Space && (int)Control.ModifierKeys == (int)Keys.Alt) //截獲alt+空格
{
return 1;
}
if (kbh.vkCode == 241) //截獲F1
{
return 1;
}
if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt + (int)Keys.Delete) //截獲Ctrl+Alt+Delete
{
return 1;
}
if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Shift) //截獲Ctrl+Shift
{
return 1;
}
if (kbh.vkCode == (int)Keys.Space && (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt) //截獲Ctrl+Alt+空格
{
return 1;
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
public static void TaskMgrLocking(bool bLock)
{
if (bLock)
{
try
{
RegistryKey r = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
r.SetValue("DisableTaskmgr", "1"); //屏蔽任務管理器
}
catch
{
RegistryKey r = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
r.SetValue("DisableTaskmgr", "0");
}
}
else
{
Registry.CurrentUser.DeleteSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
}
}
//調用
private void Form22_Load(object sender, EventArgs e)
{
Hook_Start();
}
//取消
private void Form22_FormClosing(object sender, FormClosingEventArgs e)
{
Hook_Clear();
}
uj5u.com熱心網友回復:
用的就是這 不管用uj5u.com熱心網友回復:
我有個不成熟得想法,你寫個按鈕點擊事件,判斷要是key為win鍵,他點擊之后你讓他再點擊一次,win鍵點擊2次不就是沒有效果么,或許會閃一下,可以去試試uj5u.com熱心網友回復:
我剛剛試了一下,win7系統,64位,有效果的。不知道你什麼系統
uj5u.com熱心網友回復:
剛 win10 下測驗了,可以屏蔽 win鍵。屏蔽任務管理器的話,我系統默認沒有 System 項,需要手工增加上。
然后就可以作業了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/36325.html
標籤:C#
