procedure TfrmChoiceGoods.dbgrdhGoodsDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
begin
{ with dbgrdhGoods do
begin
if (gdSelected in State) then //如果是被选中的
begin
Canvas.Brush.Color:= $00FFBB95 ;//被选中的背景色
Canvas.Font.Color:= clRed; //被选中的字体色
end
else
if DataSource.DataSet.RecNo mod 2=1 then
begin
Canvas.Brush.Color:= $00D8E9D8 ; //单数行颜色 $00FFFFCC
end
else
begin
CanVas.Brush.Color:= $00FFFBF7 ; //双数行颜色
end;
DefaultDrawcolumnCell(rect,DataCol,Column,State);
end; }
end;形式是: Procedure gsChange(objDBGridEh:TDBGridEh);
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
begin
{ with dbgrdhGoods do
begin
if (gdSelected in State) then //如果是被选中的
begin
Canvas.Brush.Color:= $00FFBB95 ;//被选中的背景色
Canvas.Font.Color:= clRed; //被选中的字体色
end
else
if DataSource.DataSet.RecNo mod 2=1 then
begin
Canvas.Brush.Color:= $00D8E9D8 ; //单数行颜色 $00FFFFCC
end
else
begin
CanVas.Brush.Color:= $00FFFBF7 ; //双数行颜色
end;
DefaultDrawcolumnCell(rect,DataCol,Column,State);
end; }
end;形式是: Procedure gsChange(objDBGridEh:TDBGridEh);
这种形式不好,因为你是把TDBGridEh作为一个对象 ,DrawColumnCell可以将cell作为一个对象,并可以访问其中的rect等属性,如果TDBGridEh作为对象,你还要去取得其中的cell对象,然后再去画,效率低
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
里写这个过程试试。
procedure DrawColorRow(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
//colorful dbgrid
with (Sender As TDBgrid) do
begin
if DataSource.DataSet.IsEmpty then
exit;
if (state=[gdselected]) or (state=[gdselected,gdfocused]) then
begin
canvas.font.color:=clRed;//clyellow;
canvas.brush.color:=ClWhite;
end
else
begin
if datasource.dataset.RecNo mod 2<>0 then
Canvas.brush.Color:=clInactiveCaptionText//ClWhite
else
Canvas.brush.Color :=CLAqua;
end;
DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
end;
修改源码?我用的是DBGirdEh,如何才能达到效果呢?如果在源码里可以做到,也是个好办法,这样就不需要再麻烦的引用过程了
TO:scutLiu
一会试试您的代码,希望是通用的:)
procedure TfrmChoiceGoods.YourDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState); 在使用过程中,把grid的DrawColumnCell事件指向YourDrawColumnCell
procedure DrawColorRow(Sender: TObject; const Rect: TRect; DataCol: Integer;
Column: TColumnEh; State: TGridDrawState);
begin
//colorful dbgrid
with (Sender As TDBgridEh) do
begin
if DataSource.DataSet.IsEmpty then
exit;
if (state=[gdselected]) or (state=[gdselected,gdfocused]) then
begin
canvas.font.color:=clRed;//clyellow;
canvas.brush.color:=ClWhite;
end
else
begin
if datasource.dataset.RecNo mod 2 <>0 then
Canvas.brush.Color:=clInactiveCaptionText//ClWhite
else
Canvas.brush.Color :=CLAqua;
end;
DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
end;但是编译的时候提示:
[DCC Error] KernelUnit.pas(1582): E2010 Incompatible types: 'DBGridEh.TGridDrawState' and 'KernelUnit.TGridDrawState'这个如何解决呢?
procedure GridDrawColumnCell(Canvas: TCanvas;RecNo:integer;Selected:boolean);
begin
if Selectedin then //如果是被选中的
begin
Canvas.Brush.Color:= $00FFBB95 ;//被选中的背景色
Canvas.Font.Color:= clRed; //被选中的字体色
end
else
if RecNo mod 2=1 then
begin
Canvas.Brush.Color:= $00D8E9D8 ; //单数行颜色 $00FFFFCC
end
else
begin
CanVas.Brush.Color:= $00FFFBF7 ; //双数行颜色
end;
end; procedure TfrmChoiceGoods.dbgrdhGoodsDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
begin GridDrawColumnCell(dbgrdhGoods.canvas,dbgrdhGoods.DataSource.DataSet.RecNo,(gdSelected in State));
dbgrdhGoods.DefaultDrawcolumnCell(rect,DataCol,Column,State);end;
这样?
这个代码是可以编译了,但是在Delphi2009编译的程序运行后错出现网格描绘错误的问题
procedure TfrmChoiceGoods.dbgrdhGoodsDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
var
OldCol:TColor;
begin
OldCol:=dbgrdhGoods.CanVas.Brush.Color;
GridDrawColumnCell(dbgrdhGoods.canvas,dbgrdhGoods.DataSource.DataSet.RecNo,(gdSelected in State));
dbgrdhGoods.DefaultDrawcolumnCell(rect,DataCol,Column,State);
dbgrdhGoods.CanVas.Brush.Color:=OldCol;
end;
//在Unit中
procedure GridDrawColumnCell(Canvas: TCanvas;RecNo:integer;Selected:boolean);
begin
if Selected then //如果是被选中的
begin
Canvas.Brush.Color:= $00FFBB95 ;//被选中的背景色
Canvas.Font.Color:= clRed; //被选中的字体色
end
else
if RecNo mod 2=1 then
begin
Canvas.Brush.Color:= $00D8E9D8 ; //单数行颜色 $00FFFFCC
end
else
begin
CanVas.Brush.Color:= $00FFFBF7 ; //双数行颜色
end;
end;
//在Form中
procedure TfrmNavigation.dbgrdhBillDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumnEh;
State: TGridDrawState);
begin
with (Sender As TDBgridEh) do
begin
GridDrawColumnCell(canvas,DataSource.DataSet.RecNo,(gdSelected in State));
DefaultDrawcolumnCell(rect,DataCol,Column,State);
end;
end;////////////////
这是目前为止做到最简单的了,大家有比这个更简单的办法不?