我來舉個簡單的例子,dbgrid的捲軸相關的。
dbgrid中假如記錄很多,使用滑鼠捲軸滾動的時候,只會在dbgrid當前頁面資料內滾動,而捲軸是不會動的,這樣操作很不方便,需要再拖動捲軸去滾動資料。加了TApplicationEvents就可以去判斷事件觸發對應的方法。
程式碼:新建應用程式工程,直接在介面上放個dbgrid,高度不要太高,寫form的oncreate事件和TApplicationEvents的onmessage事件。
不放和放TApplicationEvents,你可以看下效果。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,DBClient,DB, Grids, DBGrids, AppEvnts;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
ApplicationEvents1: TApplicationEvents;
procedure FormCreate(Sender: TObject);
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
private
{ Private declarations }
cdstemp:TClientDataSet;
ds:TDataSource;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
i:Integer;
begin
//建立物件
cdstemp:=TClientDataSet.Create(Application);
ds:=TDataSource.Create(Application);
//建立dataset列
with cdstemp do
Active:=False;
FieldDefs.Clear;
FieldDefs.Add("id",ftString,32);
CreateDataSet;
Active:=True;
//生成測試資料
for i:=0 to 20 do
cdstemp.Append;
cdstemp.FieldByName("id").AsInteger:=i;
cdstemp.Post;
//繫結
ds.DataSet:=cdstemp;
DBGrid1.DataSource:=ds;
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
//先判斷焦點在不在DBGrid上
if DBGrid1.Focused then
//判斷是不是滑鼠滾輪事件
if Msg.message = WM_MOUSEWHEEL then
if Msg.wParam > 0 then
//捲軸往上滾
SendMessage(DBGrid1.Handle, WM_KEYDOWN, VK_UP, 0)
else
//捲軸往下滾
SendMessage(DBGrid1.Handle, WM_KEYDOWN, VK_DOWN, 0);
Handled := True;
end.
我來舉個簡單的例子,dbgrid的捲軸相關的。
dbgrid中假如記錄很多,使用滑鼠捲軸滾動的時候,只會在dbgrid當前頁面資料內滾動,而捲軸是不會動的,這樣操作很不方便,需要再拖動捲軸去滾動資料。加了TApplicationEvents就可以去判斷事件觸發對應的方法。
程式碼:新建應用程式工程,直接在介面上放個dbgrid,高度不要太高,寫form的oncreate事件和TApplicationEvents的onmessage事件。
不放和放TApplicationEvents,你可以看下效果。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,DBClient,DB, Grids, DBGrids, AppEvnts;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
ApplicationEvents1: TApplicationEvents;
procedure FormCreate(Sender: TObject);
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
private
{ Private declarations }
cdstemp:TClientDataSet;
ds:TDataSource;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
i:Integer;
begin
//建立物件
cdstemp:=TClientDataSet.Create(Application);
ds:=TDataSource.Create(Application);
//建立dataset列
with cdstemp do
begin
Active:=False;
FieldDefs.Clear;
FieldDefs.Add("id",ftString,32);
CreateDataSet;
Active:=True;
end;
//生成測試資料
for i:=0 to 20 do
begin
cdstemp.Append;
cdstemp.FieldByName("id").AsInteger:=i;
cdstemp.Post;
end;
//繫結
ds.DataSet:=cdstemp;
DBGrid1.DataSource:=ds;
end;
procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
//先判斷焦點在不在DBGrid上
if DBGrid1.Focused then
begin
//判斷是不是滑鼠滾輪事件
if Msg.message = WM_MOUSEWHEEL then
begin
if Msg.wParam > 0 then
//捲軸往上滾
SendMessage(DBGrid1.Handle, WM_KEYDOWN, VK_UP, 0)
else
//捲軸往下滾
SendMessage(DBGrid1.Handle, WM_KEYDOWN, VK_DOWN, 0);
Handled := True;
end;
end;
end;
end.