Advertisement
jpfassis

TButtonColor Transparent Delphi

May 30th, 2022 (edited)
1,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.66 KB | None | 0 0
  1. //Atualizado em: 07/08/2022
  2. unit UButtonColor;
  3.  
  4. interface
  5.  
  6. uses
  7.   Windows,
  8.   Messages,
  9.   SysUtils,
  10.   Classes,
  11.   VCL.Graphics,
  12.   VCL.Controls,
  13.   VCL.StdCtrls,
  14.   VCL.Buttons,
  15.   VCL.ExtCtrls;
  16.  
  17. type
  18.   TColorButton = class(TButton)
  19.   private
  20.     FButtonColor : TColor;
  21.     FTransparentButton : Boolean;
  22.     FCanvas : TCanvas;
  23.     FStyle, FExStyle : DWORD;
  24.   protected
  25.     procedure CreateParams(var Params : TCreateParams); override;
  26.     procedure CNCtlcolorbtn(var Message: TMessage); message CN_CTLCOLORBTN;
  27.     procedure SetButtonStyle(Value : Boolean); override;
  28.     procedure SetColorBackGroundButton(const Value : TColor);
  29.     procedure SetTransparentButton(const Value : Boolean);
  30.     procedure WMEraseBkGnd(var Message : TWMEraseBkGnd); message WM_ERASEBKGND;
  31.   public
  32.     constructor Create(AOwner: TComponent); override;
  33.     destructor Destroy; override;
  34.   published
  35.     property ColorButton : TColor read FButtonColor write SetColorBackGroundButton;
  36.     property TransparentButton : Boolean read FTransparentButton write SetTransparentButton;
  37.   end;
  38.  
  39. procedure Register;
  40.  
  41. implementation
  42.  
  43. constructor TColorButton.Create(AOwner: TComponent);
  44. begin
  45.  inherited Create(AOwner);
  46.  
  47.  Width  := 150;
  48.  Height := 50;
  49.  Font.Color := clWhite;
  50.  DoubleBuffered:=False;
  51.  ParentDoubleBuffered:=False;
  52.  FButtonColor := clBlue;
  53.  FTransparentButton := False;
  54.  ControlStyle := ControlStyle + [csOpaque];
  55.  
  56. end;
  57.  
  58. procedure TColorButton.CreateParams(var Params: TCreateParams);
  59. begin
  60.   inherited CreateParams(Params);
  61.  
  62.   with Params do
  63.   begin
  64.     FStyle := Style or BS_OWNERDRAW;
  65.     Style := FStyle;
  66.     FExStyle := ExStyle or WS_EX_TRANSPARENT;
  67.     ExStyle := FExStyle;
  68.   end;
  69. end;
  70.  
  71. destructor TColorButton.Destroy;
  72. begin
  73.  inherited Destroy;
  74. end;
  75.  
  76.  
  77. procedure TColorButton.SetButtonStyle(Value: Boolean);
  78. begin
  79. //  deixar comentado para que não faça nada
  80. //  inherited;
  81. end;
  82.  
  83. procedure TColorButton.SetColorBackGroundButton(const Value: TColor);
  84. begin
  85.   FButtonColor := Value;
  86.   Repaint;
  87. end;
  88.  
  89. procedure TColorButton.SetTransparentButton(const Value: Boolean);
  90. begin
  91.   FTransparentButton := Value;
  92.   Repaint;
  93. end;
  94.  
  95. procedure TColorButton.CNCtlcolorbtn(var Message: TMessage);
  96. var
  97.   DC : HDC;
  98.   R : TRect;
  99. begin
  100.  
  101. if (FTransparentButton = True) then
  102. begin
  103.  
  104.   DoubleBuffered:=False;
  105.   ParentDoubleBuffered:=False;
  106.  
  107.   try
  108.    DC := GetDC(Handle);
  109.    R := ClientRect;
  110.    SetBkMode(DC, TRANSPARENT);
  111.    FCanvas := TCanvas.Create;
  112.    Brush.Style := bsClear;
  113.    FCanvas.Handle := DC;
  114.    FCanvas.Brush.Style:=bsClear;
  115.    FCanvas.Pen.Style:=psClear;
  116.    FCanvas.Rectangle(0, 0, Width, Height);
  117.    FCanvas.Font := Self.Font;
  118.    DrawText(FCanvas.Handle, Pchar(Caption), -1, R, DT_SINGLELINE OR DT_CENTER OR DT_VCENTER);
  119.   finally
  120.    ReleaseDC(DC, FCanvas.Handle);
  121.    FCanvas.Free;
  122.   end;
  123.  
  124. end
  125. else
  126. begin
  127.  
  128.   try
  129.    DC := GetDC(Handle);
  130.    R := ClientRect;
  131.    SetBkMode(Handle, OPAQUE);
  132.    FCanvas := TCanvas.Create;
  133.    FCanvas.Handle := DC;
  134.    FCanvas.Brush.Style:=bsSolid;
  135.    FCanvas.Brush.Color := FButtonColor; //cor do fundo
  136.    FCanvas.Pen.Style:=psSolid;
  137.    FCanvas.Pen.Color:= FButtonColor; //cor da borda
  138.    FCanvas.Rectangle(0, 0, Width, Height);
  139.    FCanvas.Font := Self.Font;
  140.    DrawText(FCanvas.Handle, Pchar(Caption), -1, R, DT_SINGLELINE OR DT_CENTER OR DT_VCENTER);
  141.   finally
  142.    ReleaseDC(DC, FCanvas.Handle);
  143.    FCanvas.Free;
  144.   end;
  145.  
  146.   Message.Result := 1;
  147. end;
  148.  
  149. end;
  150.  
  151. procedure TColorButton.WMEraseBkGnd(var Message: TWMEraseBkGnd);
  152. begin
  153.   Message.Result := 1;
  154. end;
  155.  
  156. procedure Register;
  157. begin
  158.   RegisterComponents('Standard', [TColorButton]);
  159. end;
  160.  
  161. initialization
  162.   RegisterClass(TColorButton);
  163.  
  164. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement