我正在尝试创建一组自定义组件,如 TEdit、TDBEdit、TComboBox 以及一种新的边框(圆角),我创建了以下代码:
unit RoundRectControls;
interface
uses
SysUtils, Classes, Controls, StdCtrls, Windows, Messages, Forms;
type
TRoundRectEdit = class(TEdit)
private
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
{ Public declarations }
published
property BorderStyle default bsNone;
property Ctl3D default False;
{ Published declarations }
end;
procedure Register;
procedure DrawRoundedRect(Control: TWinControl);
implementation
constructor TRoundRectEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
DrawRoundedRect(Self);
end;
procedure Register;
begin
RegisterComponents('Eduardo', [TRoundRectEdit]);
end;
procedure DrawRoundedRect(Control: TWinControl);
var
r: TRect;
Rgn: HRGN;
begin
with Control do
begin
r := ClientRect;
rgn := CreateRoundRectRgn(r.Left, r.Top, r.Right, r.Bottom, 30, 30) ;
Perform(EM_GETRECT, 0, lParam(@r)) ;
InflateRect(r, - 4, - 4) ;
Perform(EM_SETRECTNP, 0, lParam(@r)) ;
SetWindowRgn(Handle, rgn, True) ;
Invalidate;
end;
end;
end.
但是当我尝试将组件放入 Form 中后,出现了这个消息:
那么,我该如何解决呢?我是构建组件的新手,我需要一个很好的网络教程。有些东西告诉我,我需要在 Constructor 之外制作那个 DrawRoundedRect
...但是在哪里?
编辑 1 - 2012-07-27 14:50
Sertac Akyuz 的回答很好,解决了问题,但结果有点难看。我不知道我做错了什么。 EditBox 的文本太靠近左上角。有谁知道我该如何解决?
最佳答案
您正在请求“ClientRect”,但尚未创建编辑控件窗口(没有窗口,没有矩形)。您可以在创建后将您的区域修改代码移动到某个地方。示例:
type
TRoundRectEdit = class(TEdit)
private
{ Private declarations }
protected
procedure CreateWnd; override;
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
...
constructor TRoundRectEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// DrawRoundedRect(Self);
end;
procedure TRoundRectEdit.CreateWnd;
begin
inherited;
DrawRoundedRect(Self);
end;
错误消息本身反射(reflect)了 VCL 在请求窗口句柄后创建窗口的努力。它不能这样做,因为它无法确定要将控件放置在哪个窗口中。
关于delphi - 在 DT/RT 创建后如何在自定义组件上绘制额外的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11692094/