德尔福透明背景组件



关于Delphi XE的快速问题。

我正在尝试制作一个具有透明背景的自定义圆形组件,以便在添加到窗体上时,该组件可以与其他组件重叠。我已经尝试了Brush.Style:=bsTransparent;ellipse()以及更多...但仍然找不到使边缘区域透明的方法。

无论如何,我可以在不使用其他库或 API 的情况下使组件的边缘区域透明吗?

好吧,这里有一个快速的答案,应该让你开始。

type
  TEllipticPanel = class(Vcl.ExtCtrls.TPanel)
    procedure CreateWnd; override;
    procedure Paint; override;
    procedure Resize; override;
    procedure RecreateHRGN;
 end;
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    panl: TEllipticPanel;
  public
    { Public declarations }
  end;
implementation
procedure TForm1.FormCreate(Sender: TObject);
begin
  panl := TEllipticPanel.Create(self);
  panl.Left := 10;
  panl.Top := 10;
  panl.Width := 100;
  panl.Height := 50;
  panl.ParentBackground := False;
  panl.ParentColor := False;
  panl.Color := clYellow;
  panl.Parent := self;
end;
{ TEllipticPanel }
procedure TEllipticPanel.RecreateHRGN;
var
  hr: hRgn;
begin
  inherited;
  hr := CreateEllipticRgn(0,0,Width,Height);
  SetWindowRgn(Handle, hr, True);
end;
procedure TEllipticPanel.CreateWnd;
begin
  inherited;
  RecreateHRGN;
end;
procedure TEllipticPanel.Paint;
begin
  inherited;
  Canvas.Brush.Style := bsClear;
  Canvas.Pen.Style := TPenStyle(psSolid);
  Canvas.Pen.Width := 1;
  Canvas.Pen.Color := clGray;
  Canvas.Ellipse(1,1,Width-2,Height-2);
end;
procedure TEllipticPanel.Resize;
begin
  inherited;
  RecreateHRGN;
end;

关键是Windows CreateEllipticRgn和GDI SetWindowRgn函数。

有关窗口区域的详细信息,请参阅区域。

最新更新