如何为两个滚动框将OnMouseWheel
添加到同一表单?我将方法应用于ScrollBox1
,但是我不知道如何将方法添加到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;
答案 0 :(得分:2)
将相同的处理程序分配给两个ScrollBox组件,而不是分配给Form,然后使用Sender
参数知道哪个组件正在调用该处理程序。
procedure TForm3.ScrollBoxMouseWheel(Sender: TObject;
Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
var Handled: Boolean);
var
LTopLeft, LTopRight, LBottomLeft, LBottomRight: SmallInt;
LPoint: TPoint;
ScrollBox: TScrollBox;
begin
ScrollBox := TScrollBox(Sender);
LPoint := ScrollBox.ClientToScreen(Point(0,0));
LTopLeft := LPoint.X;
LTopRight := LTopLeft + ScrollBox.ClientWidth;
LBottomLeft := LPoint.Y;
LBottomRight := LBottomLeft + ScrollBox.ClientWidth;
if (MousePos.X >= LTopLeft) and
(MousePos.X <= LTopRight) and
(MousePos.Y >= LBottomLeft) and
(MousePos.Y <= LBottomRight) then
begin
ScrollBox.VertScrollBar.Position := ScrollBox.VertScrollBar.Position - WheelDelta;
Handled := True;
end;
end;
答案 1 :(得分:0)
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;