상세 컨텐츠

본문 제목

[일반/컴포넌트] 투명한(Transparent) TPanel 콤포넌트

카테고리 없음

by [롯벨] 2023. 9. 6. 08:26

본문

Type
  TTransparentPanel = 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;

Constructor TTransparentPanel.Create(AOwner:TComponent);
Begin
  Inherited Create(AOwner);
  ControlStyle:= ControlStyle - [csOpaque];
End;

Procedure TTransparentPanel.CreateParams(Var Params:TCreateParams);
Begin
  Inherited CreateParams(Params);
  Params.ExStyle:= Params.ExStyle or WS_EX_TRANSPARENT;
End;

Procedure TTransparentPanel.Paint;
Begin
  Canvas.Brush.Style:= bsClear;
  Canvas.Rectangle(0, 0, Width, Height);
  Canvas.TextOut(Width div 2, Height div 2, 'Transparent');
End;

Procedure TTransparentPanel.WMEraseBkGnd(Var Message:TWMEraseBkGnd);
Begin
  {Do Nothing}
  Message.Result:= 1;
End;

Procedure TTransparentPanel.SetParent(AParent:TWinControl);
Begin
  Inherited SetParent(AParent);
  {
    The trick needed to make it all work!
    I don't know if changing the parent's style is a good idea, but it only
    removes the WS_CLIPCHILDREN style which shouldn't cause
    any problems.
  }
  If Parent <> Nil then
    SetWindowLong(Parent.Handle, GWL_STYLE,
       GetWindowLong(Parent.Handle, GWL_STYLE) And Not WS_ClipChildren);
End;

Procedure TTransparentPanel.Invalidate;
Var
  Rect :TRect;
Begin
  Rect:= BoundsRect;
  If (Parent <> Nil) and Parent.HandleAllocated then
    InvalidateRect(Parent.Handle, @Rect, True)
  Else
    Inherited Invalidate;
End;