delphi透明背景组件 [英] delphi transparent background component

查看:54
本文介绍了delphi透明背景组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关Delphi XE的简短问题.

Quick question in regard to Delphi XE.

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

I'm trying to make a customized circle-shape component that has transparent background, so that when added on a form, the component can overlap other components. I've tried Brush.Style:=bsTransparent; or ellipse() and more on... but still couldn't find a way to make the edge area transparent.

无论如何,我是否可以使组件的边缘区域透明而无需使用其他lib或api?

Is there anyway I can make the edge area of the component transparent without using other lib or api?

推荐答案

这是一个快速的答案,应该可以助您一臂之力.

Well here's a quick answer, that should get you going.

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函数.

The key is the Windows CreateEllipticRgn and the GDI SetWindowRgn functions.

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

For more information about windows regions see Regions.

这篇关于delphi透明背景组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆