cyberfan's blog

正其谊不谋其利,明其道不计其功

  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  15 随笔 :: 489 文章 :: 44 评论 :: 0 Trackbacks
如果你不需要屏蔽Ctrl+Alt+Del组合键,可以使用低级键盘钩子(WH_KEYBOARD_LL)与低级鼠标钩子(WH_MOUSE_LL),这两种消息钩子的好处是不需要放在动态链接库中就可以作全局钩子,将键盘消息与鼠标消息截获.

unit uHookKeyAndMouse;
{ 该单元利用WH_KEYBOARD_LL与WH_MOUSE_LL两种类型的钩子分别截获键盘消息与鼠标消息}
{ 由于这里只是需要将消息屏蔽,故只需对钩子函数的返回结果设为1即可. }
{ 提供两个函数StartHookKeyMouse与StopHookKeyMouse两个函数. }

interface

uses
Windows, Messages, SysUtils;

const
WH_KEYBOARD_LL =13;
WH_MOUSE_LL =14;

procedure StartHookKeyMouse;
procedure StopHookKeyMouse;

implementation

var
hhkLowLevelKybd:HHook=0;
hhkLowLevelMouse:HHook=0;

function LowLevelKeyboardProc(nCode:Integer; WParam:WPARAM; LParam:LPARAM):LRESULT; stdcall;
begin
Result:=1;
if nCode<>0 then Result:=CallNextHookEx(0,nCode,WParam,LParam);
end;

function LowLevelMouseProc(nCode:Integer; WParam:WPARAM; LParam:LPARAM):LRESULT; stdcall;
begin
Result:=1;
if nCode<>0 then Result:=CallNextHookEx(0,nCode,WParam,LParam);
end;

procedure StartHookKeyMouse;
begin
if hhkLowLevelKybd = 0 then
begin
hhkLowLevelKybd := SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, Hinstance, 0);
end;
if hhkLowLevelMouse = 0 then
begin
hhkLowLevelMouse:=SetWindowsHookEx(WH_MOUSE_LL,LowlevelMouseProc,HInstance,0);
end;
end;

procedure StopHookKeyMouse;
begin
if hhkLowLevelKybd <> 0 then
begin
UnhookWindowsHookEx(hhkLowLevelKybd);
hhkLowLevelKybd:=0;
end;
if hhkLowLevelMouse <> 0 then
begin
UnHookWindowsHookEx(hhkLowLevelMouse);
hhkLowLevelMouse:=0;
end;
end;

initialization
hhkLowLevelKybd:=0;
hhkLowLevelMouse:=0;
finalization
if hhkLowLevelKybd <> 0 then UnhookWindowsHookEx(hhkLowLevelKybd);
if hhkLowLevelMouse <> 0 then UnhookWindowsHookEx(hhkLowLevelMouse);
end.

来自:conworld, 时间:2005-2-24 16:06:31, ID:2996263
高手终于出现了,谢谢
你的方法确实实现了锁定鼠标,但是我想达到的效果是:
1.锁定键盘
2.鼠标只能在我的程序窗口中操作
谢谢

来自:smokingroom, 时间:2005-2-24 17:01:12, ID:2996381
要求2(鼠标只能在我的程序窗口中操作)的实现:
修改LowLevelMouseProc过程如下:

type
PMSLLHOOKSTRUCT=^MSLLHOOKSTRUCT;
MSLLHOOKSTRUCT = record
pt:TPoint;
mouseData:DWORD;
flags:DWORD;
time:DWORD;
dwExtraInfo:DWORD;
end;

var
MouseRect:TRect; //这是你需要限制的Mouse活动范围.

function LowLevelMouseProc(nCode:Integer; WParam:WPARAM; LParam:LPARAM):LRESULT; stdcall;
var
p:PMSLLHOOKSTRUCT;
begin
Result:=0;
if nCode=HC_ACTION then
begin
p:=PMSLLHOOKSTRUCT(LParam);
if (p.pt.X < MouseRect.Left) or (p.pt.X > MouseRect.Right) or
(p.pt.Y < MouseRect.Top) or (p.pt.Y > MouseRect.Bottom) then
Result:=1;
end else
if nCode<>0 then Result:=CallNextHookEx(0,nCode,WParam,LParam);
end;

附取得MouseRect的代码,假定你的主窗体体为MainFrm
MouseRect:=MainFrm.ClientRect;
MouseRect.TopLeft:=MainFrm.ClientToScreen(MouseRect.TopLeft);
MouseRect.BottomRight:=MainFrm.ClientToScreen(MouseRect.BottomRight);

另在Result:=1之前加多一个ClipCursor(@MouseRect)效果会更好,可以有效解决当按下Ctrl+Alt+Del后将Mouse移出窗体后,Mouse失效的情况.
if (p.pt.X < MouseRect.Left) or (p.pt.X > MouseRect.Right) or
(p.pt.Y < MouseRect.Top) or (p.pt.Y > MouseRect.Bottom) then
begin
ClipCursor(@MouseRect)
Result:=1;
end
posted on 2005-08-12 16:08 cyberfan 阅读(489) 评论(0)  编辑 收藏 引用 所属分类: delphi
只有注册用户登录后才能发表评论。