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;