I have a Windows Hook DLL where I am trying to capture LButtonDown throughout the system to close one or more of a PopupMenu that I have created myself. It is descended from TComponent so it has nothing to do with the Windows common popupmenu control, but it must close itself if the user clicks anywhere else on the screen.
I am only getting the WM_LBUTTONDOWN message when I click on my app so it appears that I am not getting a system wide hook. It is behaving like an Application Hook.
Can someone look at the syntax because I've went over it 100 times and I am probably missing something stupid.
-----------------------------
here are the HookMouse,UnHookMouse and HookProc functions from the DLL,
you can ignore the parts about the FormDebug (that is working) and the ComponentHandles (there may be more than once instance of the component so I keep a list. That is also working)
---------------
function HookProc(nCode: Integer; MsgID: WParam; Data: LParam): LResult; stdcall;
var
ndx : Integer;
aHandle : Integer;
begin
Result := CallNextHookEx(Hook,nCode,MsgID,Data);
try
if MsgID=WM_LBUTTONDOWN then begin
if FormLog<>nil then
formLog.mmoLOG.Lines.Add('Left Mouse Down Received');
if ComponentHandles<>nil then
for ndx := 0 to ComponentHandles.Count-1 do begin
aHandle := StrToIntDef(ComponentHandles[ndx],0);
if (aHandle<>0) then
PostMessage(aHandle, WM_CloseIt, 0, 0);
end;
end;
except
end;
end;
procedure HookMouse(TheComponentHandle:HWnd;Debug:Boolean); stdcall;
begin
if ComponentHandles=nil then
ComponentHandles := TStringlist.Create;
if (FormLog=nil) and Debug then begin
FormLog := TFormLog.Create(nil);
FormLog.Show;
end;
ComponentHandles.Add(IntToStr(TheComponentHandle));
if Hook = 0 then
Hook:=SetWindowsHookEx(WH_MOUSE,HookProc,HInstance,0);
end;
procedure UnHookMouse(TheComponentHandle:HWnd); stdcall;
var
ndx : Integer;
begin
ndx := ComponentHandles.IndexOf(IntToStr(TheComponentHandle));
if ndx>=0 then
ComponentHandles.Delete(ndx);
if ComponentHandles.count=0 then begin
ComponentHandles.Free;
ComponentHandles := nil;
UnhookWindowsHookEx(Hook);
Hook:=0;
FormLog.Close;
FormLog.Free;
FormLog := nil;
end;
end;
Our community of experts have been thoroughly vetted for their expertise and industry experience.