我觉得SetWindowsHookEx的第三个参数是应该传递GetModuleHandle(null),这个函数在user32.dll中定义,返回类型IntPtr,传递参数null用来获得当前运行模块的句柄,正好最近在做个项目用到键盘和鼠标钩子,用来处理键盘和鼠标是否有响应,稍微贴几个代码给你看看:
[CODE]
// 安装钩子
public bool InstallIAHook()
{
// 获得当前实例的模块句柄
IntPtr hInstance = GetModuleHandle(null);
if (hInstance == IntPtr.Zero)
{
// 错误捕获
// 开发人员:
// 以下添加代码
return false;
}
CallBackMouseHook = new HookCallBack(MouseHookCallBack);
CallBackKeyHook = new HookCallBack(KeyHookCallBack);
hMouseHook = SetWindowsHookEx(WH_MOUSE, CallBackMouseHook , hInstance, GetCurrentThreadId());
hKeyHook = SetWindowsHookEx(WH_KEYBOARD, CallBackKeyHook , hInstance, GetCurrentThreadId());
if (hMouseHook == IntPtr.Zero)
{
// 错误捕获
// 开发人员:
// 以下添加代码
return false;
}
else if (hKeyHook == IntPtr.Zero)
{
// 错误捕获
// 开发人员:
// 以下添加代码
return false;
}
g_hook_flag = true;
return true;
}
// 拆除钩子
public void UnInstallIAHook()
{
if (g_hook_flag == true)
{
UnhookWindowsHookEx(hMouseHook);
UnhookWindowsHookEx(hKeyHook);
g_hook_flag = false;
}
}
private int MouseHookCallBack(short nCode, ushort wParam, int lParam)
{
if (nCode < 0)
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
if( nCode != HC_NOREMOVE )
{
if (g_timer_flag == true)
{
// 计时器被创建时,销毁计时器重新创建
if (KillTimer(IntPtr.Zero, hExpireTimer) == false)
{
// 进行错误捕获
// 开发人员:
}
}
else
{
// 设置计时器创建标志
g_timer_flag = true;
}
if (g_flash.Movie != "file://" + Application.StartupPath + "\\data\\line1map.swf" && g_cur_event == c_event_demo)
{
g_flash.LoadMovie(0, "file://" + Application.StartupPath + "\\data\\line1map.swf");
}
// 设置交互标志
g_interaction_flag = true;
for (int i = 0; i < g_cardCnt; i++)
ZT8403_DOAll((uint)(i + 1), 0x00000000);
// 创建定时器
CallBackExpireTimerHook = new TimerCallBackProc(ExpireTimerCallBack);
hExpireTimer = SetTimer(IntPtr.Zero, 0, c_time_out, CallBackExpireTimerHook);
if (hExpireTimer == (uint)(IntPtr.Zero.ToInt32()))
{
// 进行错误处理
// 开发人员:
}
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
private int KeyHookCallBack(short nCode, ushort wParam, int lParam)
{
if (nCode < 0)
return CallNextHookEx(hKeyHook, nCode, wParam, lParam);
if(nCode != HC_NOREMOVE)
{
if (g_timer_flag == true)
{
if (KillTimer(IntPtr.Zero, hExpireTimer) == false)
{
// 进行错误捕获
// 开发人员:
}
}
else
{
g_timer_flag = true;
}
if (g_flash.Movie != "file://" + Application.StartupPath + "\\data\\line1map.swf" && g_cur_event == c_event_demo)
{
g_flash.LoadMovie(0, "file://" + Application.StartupPath + "\\data\\line1map.swf");
}
g_interaction_flag = true;
for (int i = 0; i < g_cardCnt; i++)
ZT8403_DOAll((uint)(i + 1), 0x00000000);
// 创建定时器
CallBackExpireTimerHook = new TimerCallBackProc(ExpireTimerCallBack);
hExpireTimer = SetTimer(IntPtr.Zero, 0, c_time_out, new TimerCallBackProc(ExpireTimerCallBack));
if (hExpireTimer == (uint)(IntPtr.Zero.ToInt32()))
{
// 进行错误捕获
// 开发人员:
}
}
return CallNextHookEx(hKeyHook, nCode, wParam, lParam);
}
private void ExpireTimerCallBack(IntPtr hWnd, uint uMsg, uint idEvent, uint dwTime)
{
int i;
// 时间到期时,启动外触发
if (KillTimer(hWnd, idEvent) == false)
{
// 进行错误捕获
// 开发人员:
}
// 重置板卡LED
for (i = 0; i < g_cardCnt; i++)
ZT8403_DOAll((uint)(i + 1), 0x00000000);
g_interaction_flag = false;
g_timer_flag = false;
if (g_cur_event == c_event_demo)
{
// 加载demo的演示画面
g_flash.LoadMovie(0, "file://" + Application.StartupPath + "\\data\\main.swf");
}
else if(g_cur_event == c_event_di)
{
// 加载列队头上的介绍画面
}
}[/CODE]
以上程序演示了若有鼠标和键盘动作时切换到line1map.swf,若一段时间没有交互动作则返回main.swf,这个指示我做的一个框架,其中错误捕获代码没有添加,请忽略。
注意以上代码拥有版权,请不要随便转载,谢谢!!!