Search code examples
delphidelphi-2010c++buildervirtualtreeviewc++builder-2010

How to disable MouseWheel if mouse is not over VirtualTreeView (TVirtualStringTree)


TVirtualStringTree behaves by default if it is focused - it will scroll on mouse wheel even if mouse is not over control (except if it is over another TVirtualStringTree).

Is there a quick and elegant way to disable this behaviour?

I already did this with OnMouseWheel event and checking with PtInRect if Mouse.CursorPos if it is over a control but I have a feeling that there is a better way to do the same because this way I'd have to define a new event for each TreeView I add and also handle when to focus/unfocus the control so I hope there must be a better way to disable this.

So to be clear, I want mousewheel function to work as usual, but only when mouse is over VirtualTreeView.


Solution

  • Drop down a TApplicationEvents control to the form

    in TApplicationEvents onMessage

     procedure TForm5.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
     var
      pnt: TPoint;
      ctrl: TWinControl;
     begin
      if Msg.message = WM_MOUSEWHEEL then
      begin
        if not GetCursorPos(pnt) then Exit;
        ctrl := FindVCLWindow(pnt);
        if Assigned(ctrl) then
          Msg.hwnd := ctrl.Handle;
      end;
     end;