Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrollbox mouse wheel delphi

Tags:

delphi

vcl

How to add OnMouseWheel to the same form for two scrollboxes? I applied the method to ScrollBox1 but I did not know how to add the method to ScrollBox2

procedure TForm3.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
  LTopLeft, LTopRight, LBottomLeft, LBottomRight: SmallInt;
  LPoint: TPoint;
begin
   inherited;
  LPoint := ScrollBox1.ClientToScreen(Point(0,0));

  LTopLeft := LPoint.X;
  LTopRight := LTopLeft + ScrollBox1.Width;

  LBottomLeft := LPoint.Y;
  LBottomRight := LBottomLeft + ScrollBox1.Width;


  if (MousePos.X >= LTopLeft) and
    (MousePos.X <= LTopRight) and
    (MousePos.Y >= LBottomLeft)and
    (MousePos.Y <= LBottomRight) then
  begin
    ScrollBox1.VertScrollBar.Position :=
    ScrollBox1.VertScrollBar.Position - WheelDelta;

    Handled := True;
  end;
end;
like image 682
Alhmam Alhmam Avatar asked May 30 '26 02:05

Alhmam Alhmam


2 Answers

Assign the same event handler to both ScrollBox components, not to the Form. And then use the event's Sender parameter to know which component is calling the handler:

procedure TForm3.ScrollBoxMouseWheel(Sender: TObject;
  Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
  var Handled: Boolean);
var
  ScrollBox: TScrollBox;
  R: TRect;
begin
  ScrollBox := TScrollBox(Sender);
  R := ScrollBox.ClientToScreen(ScrollBox.ClientRect);
  if PtInRect(R, MousePos) then
  begin
    ScrollBox.VertScrollBar.Position := ScrollBox.VertScrollBar.Position - WheelDelta;
    Handled := True;
  end;
end;
like image 93
Remy Lebeau Avatar answered Jun 01 '26 22:06

Remy Lebeau


Uses Math;

procedure TMainForm.ScrollBoxMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
  ScrollBox: TScrollBox;
  NewPos: Integer;
begin
  ScrollBox := TScrollBox(Sender);

  NewPos:= ScrollBox.VertScrollBar.Position - WheelDelta div 10;  // sensitivity
  NewPos:= Max(NewPos, 0);
  NewPos:= Min(NewPos, ScrollBox.VertScrollBar.Range);

  ScrollBox.VertScrollBar.Position := NewPos;
  Handled := True;
end;
like image 30
Сергей Яшин Avatar answered Jun 01 '26 22:06

Сергей Яшин