你可以在所有者窗体上实现MouseWheel事件,然后在鼠标下测试Control是一个TScrollBox:
procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
i: Integer;
TheScrollBox: TScrollBox;
Control: TWinControl;
begin
Control := FindVCLWindow(Mouse.CursorPos);
Handled := Control is TScrollBox;
if not Handled then
exit;
TheScrollBox := Control as TScrollBox;
for i := 1 to Mouse.WheelScrollLines do
try
if WheelDelta > 0 then
TheScrollBox.Perform(WM_VSCROLL, SB_LINEUP, 0)
else
TheScrollBox.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
finally
TheScrollBox.Perform(WM_VSCROLL, SB_ENDSCROLL, 0);
end;
end;
另一种更通用的方法是实现 Application.OnMessage :
向主窗体添加一个TApplicationEvents 组件并实现一个 OnMessageEvent :
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
var
i, Count: Integer;
Control: TWinControl;
begin
if Msg.message <> WM_MOUSEWHEEL then
exit;
Control := FindVCLWindow(Mouse.CursorPos);
Handled := Control <> nil;
if not Handled then
exit;
Count := 1;
if Smallint(loWord(Msg.wParam)) = MK_CONTROL then
Count := 5;
try
for i := 1 to Count do
if Smallint(HiWord(Msg.wParam)) > 0 then
Control.Perform(WM_VSCROLL, SB_LINEUP, 0)
else
Control.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
finally
Control.Perform(WM_VSCROLL, SB_ENDSCROLL, 0);
end;
end;
PS:WPARAM 和 LPARAM 记录在 MSDN