MyWinControl不能作为设计期间的容器

这个控件直接继承自TWinControl,因此不是改写Paint;函数,而是直接改写PaintWindow虚函数,它在VCL框架里被直接调用,直接就把自己画好了(不用走给控件Perform(WM_Paint)的路线了),很有意思。

------------------------------------------------------------------------------------------------

unit MyWinControl;

interface

uses
SysUtils, Classes, Controls, Windows;

type
TMyWinControl = class(TWinControl)
protected
procedure PaintWindow(DC: HDC); override;
public
constructor Create(AOwner: TComponent); override;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TMyWinControl]);
end;

constructor TMyWinControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlState := ControlState + [csCustomPaint];
// 必须通知 WMPaint 需要画自己
end;

procedure TMyWinControl.PaintWindow(DC: HDC);
var
Rect: TRect;
begin
Windows.GetClientRect(Handle, Rect);
FillRect(DC, Rect, COLOR_BTNSHADOW + 1);
SetBkMode(DC, TRANSPARENT);
DrawText(DC, 'Hello, TMyWinControl', -1, Rect, DT_SINGLELINE or DT_VCENTER or DT_CENTER);
end;

end.

------------------------------------------------------------------------------------------------

但代码指定则可以,而且还能跟随父控件一起销毁:

procedure TForm1.Button1Click(Sender: TObject);
begin
button2.Parent:=MyWinControl1;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
MyWinControl1.Destroy;
end;

 

你可能感兴趣的:(设计)