Создавал динамически? а про парент забыл My.Parent := Self;
| Код | unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;
type TMyComp = class (TGraphicControl) protected FBorderWidth: integer; procedure SetBorderWidth(const Value: integer); procedure Paint; override; public constructor Create(AOwner: TComponent); override; published property BorderWidth: integer read FBorderWidth write SetBorderWidth default 1; end;
var Form1: TForm1; My: TMyComp; A: Integer;
implementation
{$R *.dfm} { TMyComp }
constructor TMyComp .Create(AOwner: TComponent); begin inherited Create(AOwner); FBorderWidth := 1; Width := 100; Height := 100; end;
procedure TMyComp .Paint; var DrawRct: TRect; begin DrawRct := ClientRect; Canvas.Pen.Color := clBlack; Canvas.Pen.Width := FBorderWidth; Canvas.Brush.Color := clWhite; Canvas.Rectangle(Left, Top, Width, Height); end;
procedure TMyComp .SetBorderWidth(const Value: integer); begin if Value <> FBorderWidth then FBorderWidth := Value; Invalidate; end;
procedure TForm1.Button1Click(Sender: TObject); begin Inc(A); My.BorderWidth := A; end;
procedure TForm1.FormCreate(Sender: TObject); begin My := TMyComp.Create(Self); My.Parent := Self; My.Left := 0; My.Top := 0; My.Width := 100; My.Height := 100; end;
end.
|
|