2008-12-08

滑鼠滾輪滾動時移動DBGrid 中的資料列

1.建立一個新類別借以開放OnMouseWheel
type
TMyGrid = Class(TDBGrid)
Public
property OnMouseWheel; // 開放事件
End;

2.在Form1新增一個事件程序
procedure MouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);

3.事件程序內容如下
procedure TForm1.MouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
If Sender Is TDBGrid Then Begin // 先判斷Sender是否為TDBGrid
If WheelDelta > 0 Then TDBGrid(Sender).Datasource.DataSet.Prior;
If wheelDelta < 0 Then TDBGrid(Sender).DataSource.DataSet.Next;
Handled := True;
End;
end;


4.最後在Form1.OnCreate事件中將上的事件程序掛上
procedure TForm1.FormCreate(Sender: TObject);
begin
// 將DBGrid_Name轉型為TMyGrid才可以指定OnMouseWheel事件
TMyGrid(DBGrid_Name).OnMouseWheel := MouseWheel ;
end;

沒有留言: