使用下面的方法可以,不过我不想修改源代码,而是继承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;

解决方案 »

  1.   

    我本来是想作一个TDBGridEhPro控件继承自TDBGridEh [ TDBGridEhPro=class(TDBGridEh) ],
    代码如下,如何修改这段代码才能更改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.
      

  2.   

    >>转贴在 Delphi 语言的数据库编程中,DBGrid 是显示数据的主要手段之一。但是 DBGrid 缺省的外观未免显得单调和缺乏创意。其实,我们完全可以在我们的程序中通过编程来达到美化 DBGrid 外观的目的。通过编程,我们可以改变 DBGrid 的表头、网格、网格线的前景色和背景色,以及相关的字体的大小和风格。
    以下的示例程序演示了对 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;
    运行效果:选中行变为蓝色.