Delphi的TScrollBox本身並不響應鼠標滾輪事件(不知道為什么),但可以在ScrollBox的鼠標滾動事件中進行控制:
procedure TfrmTaskNoteEdit.ScrollBox1MouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean); begin if WheelDelta < 0 then SendMessage(scrollBox1.Handle,WM_VSCROLL, SB_LINEDOWN, 0) //向下滾 else SendMessage(scrollBox1.Handle,WM_VSCROLL, SB_LINEUP, 0); //向上滾 end;
測試通過,奇怪的是我在一個PageControl的兩個頁面中分別放置兩個ScrollBox時只有一個有響應,郁悶,后來只好調整到窗體的MouseWheel事件中:
procedure TfrmTaskNoteEdit.FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean); begin inherited; case RzPageControl1.ActivePageIndex of 0: begin if WheelDelta < 0 then ScrollBox1.Perform(WM_VSCROLL,SB_LINEDOWN,0) else ScrollBox1.Perform(WM_VSCROLL,SB_LINEUP,0); end; 2: begin if WheelDelta < 0 then ScrollBox2.Perform(WM_VSCROLL,SB_LINEDOWN,0) else ScrollBox2.Perform(WM_VSCROLL,SB_LINEUP,0); end; end; end;