unit TransparentPanel; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls; type TTransparentPanelClass = class(TPanel) private procedure SetParent(AParent: TWinControl); override; procedure WMEraseBkGnd(var Message: TWMEraseBkGnd); message WM_EraseBkGnd; protected procedure CreateParams(var Params: TCreateParams); override; procedure Paint; override; public constructor Create(AOwner: TComponent); override; procedure Invalidate; override; end; implementation constructor TTransparentPanelClass.Create(AOwner: TComponent); begin inherited Create(AOwner); ControlStyle := ControlStyle - [csOpaque]; end; procedure TTransparentPanelClass.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); Params.ExStyle := Params.ExStyle or WS_EX_TRANSPARENT; end; //进行绘制 procedure TTransparentPanelClass.Paint; begin Canvas.Brush.Style := bsClear; Canvas.Rectangle(0, 0, Width, Height); Canvas.TextOut(Width div 2, Height div 2, 'Transparent'); end; procedure TTransparentPanelClass.WMEraseBkGnd(var Message: TWMEraseBkGnd); begin Message.Result := 1; end; procedure TTransparentPanelClass.SetParent(AParent: TWinControl); begin inherited SetParent(AParent); if Parent <> nil then SetWindowLong(Parent.Handle, GWL_STYLE, GetWindowLong (Parent.Handle, GWL_STYLE) and not WS_ClipChildren); end; procedure TTransparentPanelClass.Invalidate; var oRect: TRect; begin //重画 oRect := BoundsRect; if (Parent <> nil) and Parent.HandleAllocated then InvalidateRect(Parent.Handle, @oRect, True) else inherited Invalidate; end; end.