在 DT/RT 创建后如何在自定义组件上绘制额外的东西?

Ple*_*rds 2 delphi user-interface

我正在尝试使用一种新的边框(圆角)创建一组自定义组件,如 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.
Run Code Online (Sandbox Code Playgroud)

但是在我尝试将组件放入 Form 后,出现了以下消息:

控制 '' 没有父级

那么,我该如何解决?我是构建组件的新手,我需要一个很好的网络教程。有些东西告诉我我需要DrawRoundedRect在构造函数之外制作它......但是在哪里?

编辑 1 - 2012-07-27 14:50

结果

Sertac Akyuz 的回答很棒并解决了问题,但结果有点难看。我不知道我做错了什么。EditBox 的文本太靠近左上角。有谁知道我该如何解决?

Ser*_*yuz 5

您正在请求“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;
Run Code Online (Sandbox Code Playgroud)


错误消息本身反映了 VCL 在请求其句柄后创建窗口的努力。它不能这样做,因为它无法解析控件将放置在哪个窗口中。