VCL控件透明

代码
  TestPanel = class(Tpanel)
  
public
    procedure Paint; 
override;
  end;
  
 procedure TestPanel.Paint;
var
  R: TRect;
begin
  inherited;
  R :
= ClientRect;
  DrawParentBackground(Self, Canvas.Handle, @R, True);  
//透明 不透明则  DrawParentBackground(Self, Canvas.Handle)
end;

procedure DrawParentBackground(Control: TControl; DC: HDC; R: PRect 
= nil;
  bDrawErasebkgnd: Boolean 
= False);
var
  LastOrigin: TPoint;
begin
  GetWindowOrgEx(DC, LastOrigin);
  Control.Parent.Perform(WM_ERASEBKGND, Integer(DC), Integer(DC));
  Control.Parent.Perform(WM_PAINT, Integer(DC), Integer(DC));
  SetWindowOrgEx(DC, LastOrigin.X, LastOrigin.Y, nil);
end;


// 测试DEMO
procedure TForm1.Button1Click(Sender: TObject);
var
  T1: TestPanel;
begin
  T1 :
= TestPanel.Create(Self);
  T1.Left :
= 100;
  T1.Caption :
= 'dd';
  T1.Parent :
= Self;
end;
原文地址:https://www.cnblogs.com/chengxin1982/p/1648675.html