C# 编程中如何屏蔽快捷键??就像锁屏工具一样 我做出来关键是现在屏蔽不了快捷键!!

谁能告诉我怎么弄?听说用什么消息钩子?谁能详细的告诉我!!!!... 谁能告诉我怎么弄?听说用什么消息钩子?谁能详细的告诉我!!!! 展开
 我来答
手麻子0f
2010-11-20 · TA获得超过106个赞
知道答主
回答量:118
采纳率:0%
帮助的人:129万
展开全部
下个钩子就行,代码在下面
由于是使用API,以前Hook在C++中使用的比较多,我在网上查到原来在C#中一样也能实现截取键盘的Hook,相关程序如下:

public class Browser : System.Windows.Forms.Form
{

public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

static int hHook = 0;
public const int WH_KEYBOARD_LL = 13; //LowLevel键盘截获,如果是WH_KEYBOARD=2,并不能对系统键盘截取,Acrobat Reader会在你截取之前获得键盘。

HookProc KeyBoardHookProcedure;

[StructLayout(LayoutKind.Sequential)]
public class KeyBoardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}

#region DllImport

[DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
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",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode,IntPtr wParam, IntPtr lParam);

#endregion

public static int KeyBoardHookProc(int nCode,IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
KeyBoardHookStruct kbh = (KeyBoardHookStruct) Marshal.PtrToStructure(lParam,typeof(KeyBoardHookStruct));

if(kbh.vkCode == (int)Keys.F8) // 截获F8
{
//MessageBox.Show ("Catch you!");
return 1;
}
if(kbh.vkCode == (int)Keys.S
&& (int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Shift ) //截获Ctrl+Shift+S
{
//MessageBox.Show ("No Save");
return 1;
}

}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}

public void Start()
{
// install Keyboard hook
if(hHook == 0)
{
KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);
hHook = SetWindowsHookEx( WH_KEYBOARD_LL,
KeyBoardHookProcedure,
Marshal.GetHINSTANCE(
Assembly.GetExecutingAssembly().GetModules()[0]),
0);

//If SetWindowsHookEx fails.
if(hHook == 0 )
{
Stop();
throw new Exception("SetWindowsHookEx ist failed.");
}
}
}

public void Stop()
{
bool retKeyboard = true;

if(hHook != 0)
{
retKeyboard = UnhookWindowsHookEx(hHook);
hHook = 0;
}

//If UnhookWindowsHookEx fails.
if (!retKeyboard) throw new Exception("UnhookWindowsHookEx failed.");
}

private void Browser_Load(object sender, System.EventArgs e)
{
this.Start ();
}

private void menuLogout_Click(object sender, System.EventArgs e)
{
this.Stop ();
this.Close ();
}

看上去C#里使用Hook并不比C++中难,有时还要简单!但是一般编程用不到这个,我做这个东西想的太复杂了!

附录:

以下是一位网友制作的封装Hook调用的C#类,放在这里作为参考

// ***********************************************************************
// LocalWindowsHook class
// Dino Esposito, summer 2002
//
// Provide a general infrastructure for using Win32
// hooks in .NET applications
//
// ***********************************************************************

using System;
using System.Runtime.InteropServices;

namespace Win32API
{

#region Class HookEventArgs
public class HookEventArgs : EventArgs
{
public int HookCode; // Hook code
public IntPtr wParam; // WPARAM argument
public IntPtr lParam; // LPARAM argument
}
#endregion

#region Enum HookType
// Hook Types
public enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
#endregion

#region WindowsHook
public class WindowsHook
{
// ************************************************************************
// Filter function delegate
public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
// ************************************************************************

// ************************************************************************
// Internal properties
protected IntPtr m_hhook = IntPtr.Zero;
protected HookProc m_filterFunc = null;
protected HookType m_hookType;
// ************************************************************************

// ************************************************************************
// Event delegate
public delegate void HookEventHandler(object sender, HookEventArgs e);
// ************************************************************************

// ************************************************************************
// Event: HookInvoked
public event HookEventHandler HookInvoked;
protected void OnHookInvoked(HookEventArgs e)
{
if (HookInvoked != null)
HookInvoked(this, e);
}
// ************************************************************************

// ************************************************************************
// Class constructor(s)
public WindowsHook(HookType hook)
{
m_hookType = hook;
m_filterFunc = new HookProc(this.CoreHookProc);
}
public WindowsHook(HookType hook, HookProc func)
{
m_hookType = hook;
m_filterFunc = func;
}
// ************************************************************************

// ************************************************************************
// Default filter function
protected int CoreHookProc(int code, IntPtr wParam, IntPtr lParam)
{
if (code < 0)
return CallNextHookEx(m_hhook, code, wParam, lParam);

// Let clients determine what to do
HookEventArgs e = new HookEventArgs();
e.HookCode = code;
e.wParam = wParam;
e.lParam = lParam;
OnHookInvoked(e);

// Yield to the next hook in the chain
return CallNextHookEx(m_hhook, code, wParam, lParam);
}
// ************************************************************************

// ************************************************************************
// Install the hook
public void Install()
{
m_hhook = SetWindowsHookEx(
m_hookType,
m_filterFunc,
IntPtr.Zero,
(int) AppDomain.GetCurrentThreadId());
}
// ************************************************************************

// ************************************************************************
// Uninstall the hook
public void Uninstall()
{
UnhookWindowsHookEx(m_hhook);
}
// ************************************************************************

#region Win32 Imports
// ************************************************************************
// Win32: SetWindowsHookEx()
[DllImport("user32.dll")]
protected static extern IntPtr SetWindowsHookEx(HookType code,
HookProc func,
IntPtr hInstance,
int threadID);
// ************************************************************************

// ************************************************************************
// Win32: UnhookWindowsHookEx()
[DllImport("user32.dll")]
protected static extern int UnhookWindowsHookEx(IntPtr hhook);
// ************************************************************************

// ************************************************************************
// Win32: CallNextHookEx()
[DllImport("user32.dll")]
protected static extern int CallNextHookEx(IntPtr hhook,
int code, IntPtr wParam, IntPtr lParam);
// ************************************************************************
#endregion
}
#endregion
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式