事实上针对控件的不规则设计和不规则的窗口原理是一样的。甚至更简单一些,建议你读一下
delphi中VCL目录里的源程序,了解一下控件的派生和继承关系。尝试从学做不规则的TLABEL开始。
它比较简单一些。
下面是bbkxjy写的一个控件,很简单清晰:
一个无用的椭圆 Panel:
unit Panel1;
interface
uses
Windows, Messages, Classes, Controls, ExtCtrls, Graphics;
type
TPanel1 = class(TPanel)
private
FHRgn: HRGN;
procedure WMSize(var Msg: TWMSize); message WM_SIZE;
protected
procedure CreateWnd; override;
procedure DestroyWnd; override;
procedure Paint; override;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TPanel1]);
end;
{ TPanel1 }
procedure TPanel1.CreateWnd;
begin
inherited;
FHRgn := CreateEllipticRgn(ClientRect.Left, ClientRect.Top,
ClientRect.Right, ClientRect.Bottom);
SetWindowRgn(Handle, FHRgn, True);
end;
procedure TPanel1.DestroyWnd;
begin
inherited;
DeleteObject(FHRgn);
end;
procedure TPanel1.Paint;
var
SavedBrushStyle : TBrushStyle;
SavedBrushColor: TColor;
begin
SavedBrushStyle := Canvas.Brush.Style;
SavedBrushColor := Canvas.Brush.Color;
try
Canvas.Brush.Style := bsCross;
Canvas.Brush.Color := clGreen;
Canvas.Ellipse(ClientRect);
finally
Canvas.Brush.Style := SavedBrushStyle;
Canvas.Brush.Color := SavedBrushColor;
end;
end;
procedure TPanel1.WMSize(var Msg: TWMSize);
begin
inherited;
DeleteObject(FHRgn);
FHRgn := CreateEllipticRgn(ClientRect.Left, ClientRect.Top,
ClientRect.Right, ClientRect.Bottom);
SetWindowRgn(Handle, FHRgn, True);
end;
end.