delphi透明背景组件

ees*_*her 0 delphi components shape transparent

关于Delphi XE的快速问题.

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

反正我是否可以在不使用其他lib或api的情况下使组件的边缘区域透明?

Tom*_*erg 5

那么这是一个快速的答案,应该让你去.

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;
Run Code Online (Sandbox Code Playgroud)

关键是Windows CreateEllipticRgn和GDI SetWindowRgn函数.

有关Windows区域的更多信息,请参阅区域.

  • 根据[文档](http://msdn.microsoft.com/en-us/library/dd145102.aspx):"**成功调用SetWindowRgn后,系统拥有区域句柄指定的区域hRgn**系统不会复制该区域.因此,您不应该使用此区域句柄进行任何进一步的函数调用.**特别是,不要删除此区域句柄.系统在不再需要时删除区域句柄**" (3认同)