Vibeeshan Mahadeva
Vibeeshan Mahadeva

Reputation: 7248

Mouse wheel event

I have a form with many controls (treeview , memo ,listbox ,panels etc ).

i want to move the scrollbars of these controls automatically when the mouse is over the components and the wheel is scrolled.

Just as how rad studios inspector bar , tool box , project manger works.

And it is impassible to type the same code on each and every controls(more than 11 controls up to now)

[Edited]

Thanks for all of your answers but

controls like buttons don't have scroll-bars so their parents (like panels , frames ) must be moved when mouse wheel is moved over the buttons (child controls)

Upvotes: 2

Views: 1617

Answers (1)

Andreas Rejbrand
Andreas Rejbrand

Reputation: 109068

Add a TApplicationEvents to your form, and add a OnMessage handler:

procedure TForm1.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
    begin
      SendMessage(ctrl.Handle, Msg.message, Msg.wParam, Msg.lParam);
      Handled := true; // or maybe Msg.message := WM_NULL;
    end;
  end;
end;

Update

David Heffernan [see the comments] came up with a smart way of improving this code:

procedure TForm1.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;

Upvotes: 4

Related Questions