如何为dbgrideh控件添加LineColor属性,可以在设计时任意改变dbgrideh的行、列线颜色?(50分)

H

hnzqw

Unregistered / Unconfirmed
GUEST, unregistred user!
如何为dbgrideh控件添加LineColor属性,可以在设计时任意改变dbgrideh的行、列线颜色?
请给代码?谢谢!
 
DELPHIBBS真的是不如以前了!版主呢?说句话啊!
 
{
在DBGridEh的OnGetCellParams事件中添加
MyGetBackground(DBGridEh1, Background, State);
MyGetBackground 实现代码如下
}
procedure MyGetBackground(DBGridEh1: TDBGridEh; var Background: TColor;
State: TGridDrawState);
begin
if (DBGridEh1.SumList.RecNo mod 2) = 0 then
Background := MyOrdColor //奇数行的颜色
else
Background := MyEvenColor; //偶数行的颜色
if ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
Background := MySelectColor; //选中行的颜色
end;
 
to daniel007:
谢谢你!不过我的意思是想为DBGRIDEH控件增加一个LineColor属性,可以在设计时任意
设置网格线的颜色,而不是改变行的颜色。
 
如:
Type
TMtEdit = class(TEdit)
private
FAbout: String;
procedure SetAbout(Value: String);
public
constructor Create(AOwner: TComponent); override;
published
property About: String read FAbout write SetAbout;

implementation

constructor TMyEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAbout := 'Contact me with my e-mail ...';
end;

procedure TMyEdit.SetAbout(Value: String);
begin
Value := 'Contact me with my e-mail ...';
if FAbout <> Value then
begin
FABout := Value;
invalidate;
end;
end;
 
关注
{
在DBGridEh的OnGetCellParams事件中添加
MyGetBackground(DBGridEh1, Background, State);
MyGetBackground 实现代码如下
}
procedure MyGetBackground(DBGridEh1: TDBGridEh; var Background: TColor;
State: TGridDrawState);
begin
if (DBGridEh1.SumList.RecNo mod 2) = 0 then
Background := MyOrdColor //奇数行的颜色
else
Background := MyEvenColor; //偶数行的颜色
if ((State = [gdSelected]) or (State=[gdSelected,gdFocused])) then
Background := MySelectColor; //选中行的颜色
end;
 
上面的方法可以实现班马效果,但是新增记录时有毛病!!
 
以上各位没有理解我的意思:DBGRIDEH是个很不错的控件,不过没有LineColor属性,无法
设置线的颜色。我想更改这个控件,增加这个属性,以便在设计时可以改变线的颜色。而
不是改变行的颜色。有人会吗?
 
to:Reesinx
要多少分,你说吧?先把问题给我解决了!谢谢!hnzqw@xinhuanet.com
 
[brown]OK了[/brown]
 
不好意思,手头上没有DBGridEh,又比较懒没有DownLoad。就拿D本身的Grid给你演示一个吧!
首先,找到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;
这样就OK了!!

因为TCustomGrid是TStringGrid和TDrawGird的祖先,所以就可以用TStringGird来实验。
StringGird1.LineColor:=clRed;
就可以了!

不过不能使用TDBGrid,因为它检查TCustomGrid的版本。

DBGridEh的修改方法可上述应该差不多,你自己试试吧!

要给分哦!:)
其实不是我不肯奉献,我刚来DFW,你看我的分就知道了,一共提了两三次问题,
现在就剩下几十分了。不好意思,帮一下穷苦的人吧!
 
>因为TCustomGrid是TStringGrid和TDrawGird的祖先,所以就可以用TStringGird来实验。
>StringGird1.LineColor:=clRed;
>就可以了!
是StringGird1还是StringGrid1,不过两个我都试了,不行呵!
 
TO Reesinx:
照你说的的确达到了目的!
不过如果不修改源代码,而是继承1个新控件,如何作,这就是我所想问也是我想知道的:
我本来是想作一个TDBGridEhPro控件继承自TDBGridEh [ TDBGridEhPro=class(TDBGridEh) ],
代码如下,如何修改这段代码才能更改DbgridEh的线颜色,请帮忙:

unit DBGridEhPro;

interface

uses
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;

implementation

procedure 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.
 
我想override DBGridEh的Paint过程就可以吧!
我手头上没有DBGridEh的源码!告诉我download地址,我下一个看一下,再给你确切的代码。
OK?
 
http://www.51delphi.com/delphi/download?num=1 可以下载EHLIB2.5
谢谢!QQ:26039871,你的QQ?
 
呵。老弟你是用的for delphi5的dbgrideh控件吧?的确,它没有这个属性。
但是,for delphi6版的就有了!你可以参考一下这个版本的代码呀!
 
我用的是最新的FOR D7版,D6版好象没有,我也用过。
 
顶部