如果是想让选中的颜色也跟着移动,那就得这样 procedure TFrm_MaturePawn.DBGrid_InfoDrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); begin if gdSelected in State then //判断是否选中状态 begin //DBGrid_Info.Canvas.Font.Color:=clWindowText; //字体颜色(你可以不改变) DBGrid_Info.Canvas.Brush.Color:=clWindow; //背景颜色 DBGrid_Info.DefaultDrawColumnCell(Rect,DataCol,Column,State); //重绘 end; end; 这个我没经过测试(在别的事件中用过)。你自己实验一下吧
想要实现显示数据很容易. 写一个函数如下; procedure BindData; begin if ADOQuery.active then begin Edit1.Text := ADOQUery.FieldByName('F_Name').asstring; ... end; end; 而这个函数就在鼠标滚动事件中调用.但是如果你用ADOQuery的话,最好是写到ADOQuery的事件中.我忘了是那个事件了.
2.不知道你用什么和Datasorce绑定的,如果是adoquery他有个过程是afterscroll 操作可以的!
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, Grids, DBGrids;type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Table1: TTable;
procedure FormCreate(Sender: TObject);
procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
OldGridProc: TWndMethod;
procedure GridWindowProc(var Message: TMessage);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
begin
OldGridProc := DBGrid1.WindowProc;
DBGrid1.WindowProc := GridWindowProc;
end;procedure TForm1.GridWindowProc(var Message: TMessage);
begin
OldGridProc(Message);
if Message.Msg = WM_VSCROLL then
begin
Table1.MoveBy(Message.WParam);
end;
end;
要區分 滾動條上下移動的狀況
if Message.Msg = WM_VSCROLL then
begin
Table1.MoveBy(Message.WParam);//這里應該要區分
end;
OldGridWnd : TWndMethod;
//鼠标滚动事件
procedure NewGridWnd (var Message : TMessage);
初始化窗体时加入
OldGridWnd := DB_Soft.WindowProc ; //DB_Soft就是DBGrid.
DB_Soft.WindowProc := NewGridWnd; //这两句是指定事件和句柄
事件NewGridWnd如下:
//鼠标滚动消息------------------------------------------------------------------
procedure TFrm_PersonInfo.NewGridWnd(var Message: TMessage);
var
IsNeg : Boolean;
begin
if Message.Msg = WM_MOUSEWHEEL then //判断是否鼠标滚动事件
begin
//这是判断数据集是否打开
if (ADOQuery_Web.Active=False)or(ADOQuery_Web.RecordCount=0) then
begin
OldGridWnd(Message); //让程序自己处理
Exit;
end;
IsNeg := Short(Message.WParamHi) < 0; //判断鼠标是向前还是向后滚动
if IsNeg then
ADOQuery_Web.Next //向后
else
ADOQuery_Web.Prior; //向前
end;
end
else
OldGridWnd(Message); //如果不是滚动消息让程序自己处理
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, ADODB;type
TForm1 = class(TForm)
con1: TADOConnection;
able1: TADOTable;
ds1: TDataSource;
DBGrid1: TDBGrid;
qry1: TADOQuery;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
OldGridWnd : TWndMethod;
//鼠标滚动事件
procedure NewGridWnd (var Message : TMessage);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
qry1.Active := True;
OldGridWnd := DBGrid1.WindowProc ; //DB_Soft就是DBGrid.
DBGrid1.WindowProc := NewGridWnd; //这两句是指定事件和句柄
end;procedure TForm1.NewGridWnd(var Message: TMessage);
var
IsNeg : Boolean;
begin
if Message.Msg = WM_MOUSEWHEEL then //判断是否鼠标滚动事件
begin
//这是判断数据集是否打开
if (qry1.Active=False)or(qry1.RecordCount=0) then
begin
OldGridWnd(Message); //让程序自己处理
Exit;
end;
IsNeg := Short(Message.WParamHi) < 0; //判断鼠标是向前还是向后滚动
if IsNeg then
qry1.Next //向后
else
qry1.Prior; //向前
end
else
OldGridWnd(Message); //如果不是滚动消息让程序自己处理
end;end.
是这个意思吗?怎么还是不出效果?
procedure TFrm_MaturePawn.DBGrid_InfoDrawDataCell(Sender: TObject;
const Rect: TRect; Field: TField; State: TGridDrawState);
begin
if gdSelected in State then //判断是否选中状态
begin
//DBGrid_Info.Canvas.Font.Color:=clWindowText; //字体颜色(你可以不改变)
DBGrid_Info.Canvas.Brush.Color:=clWindow; //背景颜色
DBGrid_Info.DefaultDrawColumnCell(Rect,DataCol,Column,State); //重绘
end;
end;
这个我没经过测试(在别的事件中用过)。你自己实验一下吧
inherited Options := [goFixedHorzLine, goFixedVertLine, goHorzLine,
goVertLine, goColSizing, goColMoving, goTabs, goEditing];加上goThumbTracking,变成:
inherited Options := [goFixedHorzLine, goFixedVertLine, goHorzLine,
goVertLine, goColSizing, goColMoving, goTabs, goEditing,goThumbTracking];
另外
DBGrid是作了”优化“处理的,只绘制DBGRID.BOUNDRECT大小的内容。
写一个函数如下;
procedure BindData;
begin
if ADOQuery.active then
begin
Edit1.Text := ADOQUery.FieldByName('F_Name').asstring;
...
end;
end;
而这个函数就在鼠标滚动事件中调用.但是如果你用ADOQuery的话,最好是写到ADOQuery的事件中.我忘了是那个事件了.