创建基于TDBGrid控件,如何写这一点点?不懂!(50分)(50分)

  • 主题发起人 主题发起人 jrq
  • 开始时间 开始时间
J

jrq

Unregistered / Unconfirmed
GUEST, unregistred user!
目的:创建基于TDBGrid的TColorDBGrid控件
操作:在Delphi组件菜单中,选择New Component,在弹出对话框中作以下设置:
Ancestor Type = TDBGrid
Class Name = TColorDBGrid
其它默认,然后单击OK按钮,这样自动完成组件基本框架的定义。
我想为这个控件增加名称 OnDRawColorDBGrid 的事件,
并使它出现在Object Inspector的Events中,需要重载DrawCell方法。

问题:我怎样才能增加这个OnDRawColorDBGrid 事件?
需要写代码吗? 怎样写? 如何能够重载DrawCell方法。
还是需要其它 / 怎样的设置?

小弟一前从没有捣鼓过这样的问题,今天是开天辟地,请各位高手赐教!
谢谢!
 

没人光顾啊? 各位高手进来啊!
 

我在等啊~
 
看看下面的:
TColorGrid = class(TDBGrid)
private
FOnDRawColorDBGrid: TNotifyEvent;
protected
public
procedure DrawCell; override;
published
property OnDRawColorDBGrid: TNotifyEvent read FOnDRawColorDBGrid write FOnDRawColorDBGrid;
end;

建议你找本组件的书看看,那样会系统些
 
to 0738:多谢了~

我现在手头上没有书,能否推荐?
 
《Delphi高级开发指南》这本书有详细的介绍,看了后应该可以解决这个问题了。
 
to 0738:
我按照你写了~
可当我安装控件时提示:
[Error] ColoredDBGrid.pas(16): Declaration of 'DrawCell' differs from previous declaration

重载时发生了问题!
怎么回事啊? 我是不是还需要写其它的代码?

-------我手上没有书 拜托各位了!
 
是不是你的参数不对啊
protected
procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
 
to 0738:
我重写了参数,安装有错误~

下面的提示:
[Error] ColoredDBGrid.pas(15): Unsatisfied forward or external declaration: 'TColoredDBGrid.DrawCell'
外部声明是什么意思?

下面是全部代码:
---------------------------
unit ColoredDBGrid;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Grids, DBGrids;
type
TColoredDBGrid = class(TDBGrid)
private
FOnDRawColorDBGrid: TNotifyEvent;
{ Private declarations }
protected
procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
{ Protected declarations }
public
{ Public declarations }
published
property OnDRawColorDBGrid: TNotifyEvent read FOnDRawColorDBGrid write FOnDRawColorDBGrid;
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TColoredDBGrid]);
end;
end.

请帮忙检查一下 是不是缺少其它代码?
小弟对这些东东是初次接触
 
你没有重载过程的具体实现啊,试试下面的代码
interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, DBGrids;

type
TColorDBGrid = class(TDBGrid)

private
FOnDRawColorDBGrid: TNotifyEvent;
{ Private declarations }
protected
procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
{ Protected declarations }
public
{ Public declarations }
published
property OnDRawColorDBGrid: TNotifyEvent read FOnDRawColorDBGrid write FOnDRawColorDBGrid;
{ Published declarations }
end;

procedure Register;

implementation
procedure TColorDBGrid.DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
begin
inherited;
if Assigned(FOnDrawColorDBGrid) then FOnDrawColorDBGrid(Self);
end;

procedure Register;
begin
RegisterComponents('Samples', [TColoredDBGrid]);
end;
end.
 
上面的代码我已经帮你试过了,安装没有问题的,不过要将
RegisterComponents('Samples', [TColoredDBGrid]);
改成
RegisterComponents('Samples', [TColorDBGrid]);
 
to 0738:
多谢! 我去试试!

 
得到正确结果
结束~ 已经送分!

to 0738 : 谢谢!
to e_hua : 谢谢!
 
后退
顶部