关于全局鼠标钩子的实现,是否要用内存映射?我要传一个treeview参数给dll,用来记录鼠标的点击事件,请问该怎么实现?见http://community.csdn.net/Expert/topic/3266/3266456.xml?temp=.843197 没人帮忙解决,我不懂vc++,我这样为什么不行?
unit UnMouseHook;interface
uses windows,sysutils,classes,messages,comctrls,forms;function FreeHook: Boolean; stdcall;export;
function SetHook(T:TTreeView): Boolean; stdcall;export;
function HookProc(code:integer;wp:WPARAM;lp:LPARAM):Lresult;stdcall;export;implementationfunction CreateMMF(Name: string; Size: Integer): THandle;
begin
Result := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, Size, PChar(Name));
if Result <> 0 then
begin
if GetLastError = ERROR_ALREADY_EXISTS then
begin
CloseHandle(Result);
Result := 0;
end;
end;
end;function OpenMMF(Name: string): THandle;
begin
Result := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, PChar(Name));
// The return value is an open handle to the specified file-mapping object.
end;
function MapMMF(MMFHandle: THandle): Pointer;
begin
Result := MapViewOfFile(MMFHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
end;function UnMapMMF(P: Pointer): Boolean;
begin
Result := UnmapViewOfFile(P);
end;function CloseMMF(MMFHandle: THandle): Boolean;
begin
Result := CloseHandle(MMFHandle);
end;const MMFName = 'MsgFilterHookDemo';type PMMFData=^TMMFData;
TMMFData=record
NextHook:HHook;
Tree:TTreeView;
end;var
MMFHandle: THandle;
MMFData: PMMFData;
function UnMapAndCloseMMF: Boolean;
begin
Result := False;
if UnMapMMF(MMFData) then
begin
MMFData := nil;
if CloseMMF(MMFHandle) then
begin
MMFHandle := 0;
Result := True;
end;
end;
end;function SetHook(T:TTreeView): Boolean; stdcall;
begin
Result := False;
if (MMFData = nil) and (MMFHandle = 0) then
begin
MMFHandle := CreateMMF(MMFName, SizeOf(TMMFData));
if MMFHandle <> 0 then
begin
MMFData := MapMMF(MMFHandle);
if MMFData <> nil then
begin
MMFData^.Tree:=TTreeView.Create(Application);
MMFData^.Tree:=T;
MMFData^.NextHook:= SetWindowsHookEx(WH_MOUSE,@HookProc, HInstance, 0);
if MMFData^.NextHook = 0 then
UnMapAndCloseMMF
else
Result := True;
end
else
begin
CloseMMF(MMFHandle);
MMFHandle := 0;
end;
end;
end;
end;function FreeHook: Boolean; stdcall;
begin
Result := False;
if (MMFData <> nil) and (MMFHandle <> 0) then
if UnHookWindowsHookEx(MMFData^.NextHook) then
Result := UnMapAndCloseMMF;
end;function HookProc(code:integer;wp:WPARAM;lp:LPARAM):Lresult;stdcall;
var
info:string;
begin
Result := 0;
MMFHandle := OpenMMF(MMFName);
if MMFHandle <> 0 then
begin
MMFData := MapMMF(MMFHandle);
if MMFData <> nil then
begin
if code<0 then callnexthookex(MMFData.NextHook,code,wp,lp);
info:='';
case wp of
WM_LBUTTONDOWN:
begin
info:='左键单击';
end;
WM_RBUTTONDOWN:
begin
info:='右键单击';
end;
WM_LBUTTONDBLCLK:
begin
info:='左键双击';
end;
WM_RBUTTONDBLCLK:
begin
info:='右键双击';
end;
end;
if not(info='') then
begin
info:=info+' 坐标X='+inttostr(PMouseHookStruct(lp).pt.X)+','+'Y='+inttostr(PMouseHookStruct(lp).pt.Y);
MMFData^.Tree.Items.AddChild(nil,info);
end;
result:=callnexthookex(MMFData.NextHook,code,wp,lp);
end;
UnMapMMF(MMFData);
end;
CloseMMF(MMFHandle);
end;
initialization
begin
MMFHandle := 0;
MMFData := nil;
end;finalization
FreeHook;end.
unit UnMouseHook;interface
uses windows,sysutils,classes,messages,comctrls,forms;function FreeHook: Boolean; stdcall;export;
function SetHook(T:TTreeView): Boolean; stdcall;export;
function HookProc(code:integer;wp:WPARAM;lp:LPARAM):Lresult;stdcall;export;implementationfunction CreateMMF(Name: string; Size: Integer): THandle;
begin
Result := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, Size, PChar(Name));
if Result <> 0 then
begin
if GetLastError = ERROR_ALREADY_EXISTS then
begin
CloseHandle(Result);
Result := 0;
end;
end;
end;function OpenMMF(Name: string): THandle;
begin
Result := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, PChar(Name));
// The return value is an open handle to the specified file-mapping object.
end;
function MapMMF(MMFHandle: THandle): Pointer;
begin
Result := MapViewOfFile(MMFHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
end;function UnMapMMF(P: Pointer): Boolean;
begin
Result := UnmapViewOfFile(P);
end;function CloseMMF(MMFHandle: THandle): Boolean;
begin
Result := CloseHandle(MMFHandle);
end;const MMFName = 'MsgFilterHookDemo';type PMMFData=^TMMFData;
TMMFData=record
NextHook:HHook;
Tree:TTreeView;
end;var
MMFHandle: THandle;
MMFData: PMMFData;
function UnMapAndCloseMMF: Boolean;
begin
Result := False;
if UnMapMMF(MMFData) then
begin
MMFData := nil;
if CloseMMF(MMFHandle) then
begin
MMFHandle := 0;
Result := True;
end;
end;
end;function SetHook(T:TTreeView): Boolean; stdcall;
begin
Result := False;
if (MMFData = nil) and (MMFHandle = 0) then
begin
MMFHandle := CreateMMF(MMFName, SizeOf(TMMFData));
if MMFHandle <> 0 then
begin
MMFData := MapMMF(MMFHandle);
if MMFData <> nil then
begin
MMFData^.Tree:=TTreeView.Create(Application);
MMFData^.Tree:=T;
MMFData^.NextHook:= SetWindowsHookEx(WH_MOUSE,@HookProc, HInstance, 0);
if MMFData^.NextHook = 0 then
UnMapAndCloseMMF
else
Result := True;
end
else
begin
CloseMMF(MMFHandle);
MMFHandle := 0;
end;
end;
end;
end;function FreeHook: Boolean; stdcall;
begin
Result := False;
if (MMFData <> nil) and (MMFHandle <> 0) then
if UnHookWindowsHookEx(MMFData^.NextHook) then
Result := UnMapAndCloseMMF;
end;function HookProc(code:integer;wp:WPARAM;lp:LPARAM):Lresult;stdcall;
var
info:string;
begin
Result := 0;
MMFHandle := OpenMMF(MMFName);
if MMFHandle <> 0 then
begin
MMFData := MapMMF(MMFHandle);
if MMFData <> nil then
begin
if code<0 then callnexthookex(MMFData.NextHook,code,wp,lp);
info:='';
case wp of
WM_LBUTTONDOWN:
begin
info:='左键单击';
end;
WM_RBUTTONDOWN:
begin
info:='右键单击';
end;
WM_LBUTTONDBLCLK:
begin
info:='左键双击';
end;
WM_RBUTTONDBLCLK:
begin
info:='右键双击';
end;
end;
if not(info='') then
begin
info:=info+' 坐标X='+inttostr(PMouseHookStruct(lp).pt.X)+','+'Y='+inttostr(PMouseHookStruct(lp).pt.Y);
MMFData^.Tree.Items.AddChild(nil,info);
end;
result:=callnexthookex(MMFData.NextHook,code,wp,lp);
end;
UnMapMMF(MMFData);
end;
CloseMMF(MMFHandle);
end;
initialization
begin
MMFHandle := 0;
MMFData := nil;
end;finalization
FreeHook;end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货