使用下面的方法可以,不过我不想修改源代码,而是继承1个新控件,而且我是要用DBGridEh实现 如何做?????????????????????????????????首先,找到Grid.pas
打开后,找到TCustomGrid的定义部分,在private中加上两句
TCustomGrid = class(TCustomControl)
private
FLineColor: TColor; //第一句
...
procedure SetLineColor(Value: TColor); //第二句
...
然后在published中再加上一句:
published
property LineColor: TColor read FLineColor write SetLineColor default clSilver;接下来要写一个过程,就是上面定义过的SetLineColor。
procedure TCustomGrid.SetLineColor(Value: TColor);
begin
FLineColor:=Value;
Paint;
end;之后在TCustomGrid的构造函数中,也就是Create过程中加上一句:
constructor TCustomGrid.Create(AOwner: TComponent);
...
begin
...
FLineColor:=clSilver; //就是这一句了最后,最重要的是在TCustomGird.Paint中修改一句,大概在Grid.pas的第1930行左右。
你找找看,原语句如下:
LineColor := clSilver;
把它改为:LineColor:=FLineColor;
打开后,找到TCustomGrid的定义部分,在private中加上两句
TCustomGrid = class(TCustomControl)
private
FLineColor: TColor; //第一句
...
procedure SetLineColor(Value: TColor); //第二句
...
然后在published中再加上一句:
published
property LineColor: TColor read FLineColor write SetLineColor default clSilver;接下来要写一个过程,就是上面定义过的SetLineColor。
procedure TCustomGrid.SetLineColor(Value: TColor);
begin
FLineColor:=Value;
Paint;
end;之后在TCustomGrid的构造函数中,也就是Create过程中加上一句:
constructor TCustomGrid.Create(AOwner: TComponent);
...
begin
...
FLineColor:=clSilver; //就是这一句了最后,最重要的是在TCustomGird.Paint中修改一句,大概在Grid.pas的第1930行左右。
你找找看,原语句如下:
LineColor := clSilver;
把它改为:LineColor:=FLineColor;
解决方案 »
- 操作DBGrid时,如何让 ClientDataSet.RecNo 不动??
- 帮我解析一下 小程序
- 怎么得到控制台程序的参数啊,给个例子,谢谢!
- 请教如何知道函数的参数
- 急救:如果取得每个汉字的五笔打法的第一个字母?
- 注册表读写错误。
- delphi 关闭 windows
- 关于 自定义类型 中使用 string 的问题!
- 用TTable的AddIndex方法,给一个已建数据表建立索引,出现"Table is busy."异常,是什么原因?
- 为什么有时我用DELPHI打开别人的程序时,不能使用Ctrl+Click就定位到类的声明代码上(在自己的程序上是可以的)?
- 求助!!对从串口收到的二进制数进行反码变换
- 现在有这么个问题,谁有好的解决办法啊,我想应该很好弄,不过我不会啊
代码如下,如何修改这段代码才能更改DbgridEh的线颜色,请帮忙:unit DBGridEhPro;interfaceuses
SysUtils, Classes, Controls, Grids, DBGridEh,Graphics,dbgrids;type
TDBGridEhPro = class(TDBGridEh)
private
FLineColor:Tcolor;
Procedure SetLineColor(value:Tcolor);
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property LineColor:Tcolor read FLineColor Write setLineColor Default clSilver; end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Samples', [TDBGridEhPro]);
end;{ TDBGridEhPro }
constructor TDBGridEhPro.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLinecolor:=clred;
end;destructor TDBGridEhPro.Destroy;
begin inherited;
end;procedure TDBGridEhPro.SetLineColor(value: Tcolor);
begin
if FLineColor <> Value then
begin
FLineColor := Value;
invalidate;
end;end;
end.
以下的示例程序演示了对 DBGrid 各属性的设置,使 Delphi 显示的表格就像网页中的表格一样漂亮美观。
示例程序的运行:在 Form1 上放置 DBGrid1、Query1、DataSource1 三个数据库组件,设置相关的属性,使 DBGrid1 能显示表中的数据。然后,在 DBGrid1 的 onDrawColumnCell 事件中键入以下代码,然后运行程序,就可以看到神奇的结果了。本代码在 Windows98、Delphi5.0 环境下调试通过。
procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
var i :integer;
begin
if gdSelected in State then Exit;//定义表头的字体和背景颜色:
for i :=0 to (Sender as TDBGrid).Columns.Count-1 do
begin
(Sender as TDBGrid).Columns[i].Title.Font.Name :='宋体'; //字体
(Sender as TDBGrid).Columns[i].Title.Font.Size :=9; //字体大小
(Sender as TDBGrid).Columns[i].Title.Font.Color :=$000000ff; //字体颜色(红色)
(Sender as TDBGrid).Columns[i].Title.Color :=$0000ff00; //背景色(绿色)
end;//隔行改变网格背景色:
if Query1.RecNo mod 2 = 0 then
(Sender as TDBGrid).Canvas.Brush.Color := clInfoBk //定义背景颜色
else
(Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); //定义背景颜色//定义网格线的颜色:
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
with (Sender as TDBGrid).Canvas do //画 cell 的边框
begin
Pen.Color := $00ff0000; //定义画笔颜色(蓝色)
MoveTo(Rect.Left, Rect.Bottom); //画笔定位
LineTo(Rect.Right, Rect.Bottom); //画蓝色的横线
Pen.Color := $0000ff00; //定义画笔颜色(绿色)
MoveTo(Rect.Right, Rect.Top); //画笔定位
LineTo(Rect.Right, Rect.Bottom); //画绿色的竖线
end;
end;
运行效果:选中行变为蓝色
步骤1:设置dbgrid的options的dgrowselect为true.
步骤2:在dbgrid的ondrawcolumncell事件里面写上:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if gdSelected in state then
dbgrid1.Canvas.Brush.Color:=clblue;
dbgrid1.DefaultDrawColumnCell(rect,datacol,column,state);
end;
运行效果:选中行变为蓝色.