对于自定义的控件,使用方法有两种:
1. 通过控件注册,让你的控件可以出现在Delphi的控件面板上;
2. 不通过注册,直接把单元加入到项目当中使用。你的错误肯定是使用方法的问题。
我想你使用的是第二种方法,那么请把在interface部分的uses中的OcoolEdit单元引用去掉,改在implementation下面的uses中引用:
implementationuses
OcoolEdit;如果使用的是第一种方法,则你使用的引用方法是正确的。
1. 通过控件注册,让你的控件可以出现在Delphi的控件面板上;
2. 不通过注册,直接把单元加入到项目当中使用。你的错误肯定是使用方法的问题。
我想你使用的是第二种方法,那么请把在interface部分的uses中的OcoolEdit单元引用去掉,改在implementation下面的uses中引用:
implementationuses
OcoolEdit;如果使用的是第一种方法,则你使用的引用方法是正确的。
constructor TOcoolEdit.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
end;procedure TOcoolEdit.Destroy;
begin
inherited Destroy;
end;上面的代码去掉,多余了。procedure TOcoolEdit.Paint;
begin
Canvas.Rectangle(0,0,100,200);
end;
改成procedure TOcoolEdit.Paint;
begin
inherited;
Canvas.Rectangle(0,0,100,200);
end;否则就算你的控件使用起来了,也不能正常显示出来。
constructor TOcoolEdit.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
end;procedure TOcoolEdit.Destroy;
begin
inherited Destroy;
end;上面的代码去掉,多余了。procedure TOcoolEdit.Paint;
begin
Canvas.Rectangle(0,0,100,200);
end;
改成procedure TOcoolEdit.Paint;
begin
inherited;
Canvas.Rectangle(0,0,100,200);
end;否则就算你的控件使用起来了,也不能正常显示出来。
unit main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses
OcoolEdit; //注意我已经把引用写到了这里! {$R *.dfm}end.
另外,debussy(debussy) ,那个问题我依然没能完全解决,多谢您的指点,我已经把那个帖子接了,您的分数已经家上了!
单元怎么没有uses语句,你明明要用一些单元的,比如Canvas类要用Graphics
TCustomControl要用Controls单元啊
unit ocooledit;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms;type
TOcoolEdit=class(TCustomControl)
protected
procedure Paint; override;
property Canvas;
public
constructor Create(AOwner:TComponent); override;
destructor Destroy; override;
end;implementationconstructor TOcoolEdit.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
end;procedure TOcoolEdit.Paint;
begin
Canvas.Rectangle(0,0,100,200);
end;procedure TOcoolEdit.Destroy;
begin
inherited Destroy;
end;end.