我已经开始在Delphi 6 Pro中构建一个新组件.目前它只有一个TFont发布的财产.但是,当我在设计时将组件放在Form上,并单击“textAttr_1”属性(省略号)的编辑按钮时,我得到一个异常,说“不能将NIL分配给TFont”.我做错了什么导致了这个错误?以下是组件的代码:
unit JvExtendedTextAttributes; interface uses Windows,Messages,SysUtils,Classes,JvRichEdit,Graphics; type TJvExtendedTextAttributes = class(TComponent) private { Private declarations } protected { Protected declarations } FTextAttr_1: TFont; public { Public declarations } constructor Create(AOwner: TComponent); published { Published declarations } property textAttr_1: TFont read FTextAttr_1 write FTextAttr_1; end; procedure Register; implementation procedure Register; begin RegisterComponents('FAVORITES',[TJvExtendedTextAttributes]); end; // --------------------------------------------------------------- constructor TJvExtendedTextAttributes.Create(AOwner: TComponent); begin inherited Create(AOwner); FTextAttr_1 := TFont.Create; end; // --------------------------------------------------------------- end.
解决方法
您的主要问题是您忘记向组件的构造函数添加覆盖.这意味着它没有被调用,因为VCL框架利用了TComponent的虚拟构造函数.这就解释了为什么你的字体实例是零.
您还需要一个调用Assign的set方法来复制字体的属性,而不是替换不可避免地导致内存损坏错误的实例.
VCL源有无数的这种模式的例子.它看起来像这样:
property Font: TFont read FFont write SetFont; ... procedure TMyComponent.SetFont(Value: TFont); begin FFont.Assign(Value); end;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。