How to prevent focused control from scrolling when the mouse isn't over it?
Asked Answered
L

1

2

Refer to this prior related question. While the answers there do work, I have further issues when it comes to certain types of controls such as a TDBGrid. If the TDBGrid currently has focus, but the mouse is pointed over another control to scroll, the TDBGrid scrolls anyway, thus resulting in two different controls scrolling at the same time. This happens in every single solution which I've found so far related to scrolling the control underneath the mouse.

How can I prevent this behavior and ensure that only the control under the mouse scrolls, and nothing else?

Literary answered 8/12, 2015 at 0:29 Comment(2)
Does all the event handlers for another scrolling controls set Handled:=true in all cases? If not, that's the natural behaviour for message handling system to find 'somebody else' to do the work, i.e. focused control. Does in fact all the controls have MouseWheel event handlers written by you, or some should work by themselves like TMemo or TListView? Some of them have implementation where they MUST be focused to react to mousewheel event.Remuneration
Do some debugging. Work out why two controls are handling the mouse scroll messages.Ives
L
0

This code works fine for me.

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
var
  Control: TWinControl;
begin

  if Msg.message = WM_MOUSEWHEEL then
  begin
    // Find control at mouse cursor
    Control := FindVCLWindow(Msg.pt);

    if Assigned(Control) then
    begin
      // Try to scroll
      if Control.Perform(CM_MOUSEWHEEL, Msg.wParam, Msg.lParam) <> 0 then
        Handled := True
      else
      // If no scroll was performed by control,
      // then detrmine if message control is at mouse cursor.
      // If not, then supress message
      if Control.Handle <> Msg.hwnd then
        Handled := True;
    end;
  end;

end;
Limited answered 11/3, 2016 at 12:54 Comment(1)
A little explanation would boost the quality of your answer.Truehearted

© 2022 - 2024 — McMap. All rights reserved.