Jump to content
Squall_FF8

How to capture a mouse click outside a modal window?

Recommended Posts

Hi guys,

 

I have a modal window above my main form. I want to do something when I click outside the modal window. How I can do that?

I tried with MouseMove, but it returns <X,Y> as <-1,-1> when the cursor is outside the modal window.
Unfortunately that is not enough because, the same values are returned when you try to do things like resize,.. so there is no guarantee that <-1,-1> is what we need.
On top of that MouseUp/Down is not even triggered when you click outside. 

Edited by Squall_FF8
more details

Share this post


Link to post

Clicking outside the modal form will not register a mouse down, even through TApplicationEvents.OnMessage. I suspect you might have to use Windows System Hooks with WH_MOUSE. I have not done this before though and as such cannot confirm if this would be the most appropriate way to do it.

 

 

Edited by PeaShooter_OMO

Share this post


Link to post

will this help?

 

 
 
function MouseHook (Code: integer; wParam: WPARAM; lParam: LPARAM): LRESULT; 
stdcall;
var
  ClientPt : TPoint;
begin
  {------------------------}
  { Handle Mouse Movements }
  {------------------------}
  if (GlobData <> nil) and
     (Code = HC_ACTION) and
     ((wParam = WM_MOUSEMOVE) or (wParam = WM_NCMOUSEMOVE)) then
  begin
    with PMouseHookStruct(lParam)^ do
    begin
      {-----------------------------}
      { Send the screen coordinates }
      {-----------------------------}
      PostMessage(GlobData^.ActiveHandle, WM_APP+2, Pt.X, Pt.Y);

      {-----------------------------}
      { Send the window coordinates }
      {-----------------------------}
      ClientPt := Pt;
      ScreenToClient(hwnd, ClientPt);
      PostMessage(GlobData^.ActiveHandle, WM_APP+3, ClientPt.X, ClientPt.Y);
    end;
  end;
  {-----------------------------------------------}
  { Call the next hook in the chain }
  {-----------------------------------------------}
  Result := CallNextHookEx(0, Code, wParam, lParam);
end;

function InstallMouseHook (Wnd: HWND): Boolean; stdcall;
begin
  Result := False;
  if (GlobData = nil) then Exit;
  if (GlobData^.THook = 0) then
  begin
    GlobData^.THook := SetWindowsHookEx(WH_MOUSE, @MouseHook, HInstance, 0);
    if GlobData^.THook = 0 then Exit;
  end;
  GlobData^.ActiveHandle := Wnd;
  Result := True;
end;

function UninstallMouseHook: Boolean; stdcall;
begin
  Result := True;
  if (GlobData = nil) then Exit;
  if (GlobData^.THook <> 0) then
  begin
    if not UnhookWindowsHookEx(GlobData^.THook) then Exit;
    GlobData^.THook = 0;
  end;
  GlobData^.ActiveHandle := 0;
  Result := True;
end;

 

Share this post


Link to post

Can you narrow it down further if the click you want to catch will be

  1. Still inside your own application
  2. Outside of your application, causing it to lose focus
  3. If mouse coordinates matter of you're just interested that a click happened
Edited by Der schöne Günther

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×