Recent

Author Topic: how to set a button color in IDE?  (Read 2913 times)

blackangel

  • Newbie
  • Posts: 2
how to set a button color in IDE?
« on: November 22, 2024, 08:28:44 am »
I have set a button color and font,and it's font is changed,but it's color is not changed.
who can tell me why?

Handoko

  • Hero Member
  • *****
  • Posts: 5386
  • My goal: build my own game engine using Lazarus
Re: how to set a button color in IDE?
« Reply #1 on: November 22, 2024, 08:50:39 am »
Hello blackangel,
Welcome to the forum.

It is OS dependent behavior. Windows does not allow changing button's color, on Linux and other OSes button's color can be changed.

There are several solutions if you want to have custom button's color on Windows. You can write your own button, for example using TPanel. You can search the forum, there were many discussions showing how to use TPanel to behave like a button. Alternatively you can use third party components for example ATButton:

https://wiki.lazarus.freepascal.org/ATButton

blackangel

  • Newbie
  • Posts: 2
Re: how to set a button color in IDE?
« Reply #2 on: November 24, 2024, 04:31:07 am »
thks,i think i know how to do it

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1295
Re: how to set a button color in IDE?
« Reply #3 on: November 27, 2024, 09:56:01 am »
Use tcdbutton
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Onur2x

  • New Member
  • *
  • Posts: 36
Re: how to set a button color in IDE?
« Reply #4 on: December 19, 2024, 11:57:23 am »
Code: Pascal  [Select][+][-]
  1. Type
  2.  
  3. TExButtonState = (obshover, obspressed, obsnormal);  
  4.  
  5. TExampleButton = class(TButton)
  6.    private
  7.     Fstate: TExButtonState;
  8.     FCanvas      : TCanvas;
  9.     Fresim,FBGDisable,FBGHover,FBGNormal,FBGPress:TBGRABitmap;
  10.     procedure WMEraseBkGnd(var Message:TWMEraseBkGnd);
  11.    protected  
  12.      procedure MouseEnter; override;
  13.     procedure MouseLeave; override;
  14.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  15.       X: integer; Y: integer); override;
  16.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  17.       X: integer; Y: integer); override;
  18.     procedure WMPaint(var Msg: TLMPaint); message LM_PAINT;
  19.     procedure Paint; virtual;
  20.     procedure PaintWindow(DC: HDC); override;
  21.     property Canvas: TCanvas read FCanvas;
  22.     public  
  23.     constructor Create(AOwner: TComponent); override;
  24.     destructor Destroy; override;
  25.    end;
  26.  
  27. implementation
  28.  
  29. procedure TExampleButton.MouseEnter;
  30. begin
  31.   if (csDesigning in ComponentState) then    exit;
  32.   if (Enabled=false) or (Fstate = obshover) then  Exit;
  33.   inherited MouseEnter;
  34.   Fstate := obshover;
  35.   Invalidate;
  36. end;
  37.  
  38. procedure TExampleButton.MouseLeave;
  39. begin
  40.    if (csDesigning in ComponentState) then
  41.   exit;
  42.   if (Enabled=false) or (Fstate = obsnormal) then
  43.   Exit;
  44.   inherited MouseLeave;
  45.   Fstate := obsnormal;
  46.   Invalidate;
  47. end;
  48. procedure TExampleButton.MouseDown(Button:TMouseButton;Shift:TShiftState;X:
  49.   integer;Y:integer);
  50. begin
  51.   if (csDesigning in ComponentState) then
  52.   exit;
  53.   if (Enabled=false) or (Fstate = obspressed) then
  54.   Exit;
  55.   inherited MouseDown(Button, Shift, X, Y);
  56.   if Button = mbLeft then
  57.   begin
  58.     Fstate := obspressed;
  59.     Invalidate;
  60.   end;
  61. end;
  62.  
  63. procedure TExampleButton.MouseUp(Button:TMouseButton;Shift:TShiftState;X:integer;
  64.   Y:integer);
  65. begin
  66.   inherited MouseUp(Button, Shift, X, Y);
  67.   Fstate := obsnormal;
  68.   Invalidate;
  69. end;
  70.  
  71. procedure TExampleButton.WMEraseBkGnd(var Message: TWMEraseBkGnd);
  72. begin
  73.   Message.Result := - 1;
  74. end;
  75.  
  76.  
  77. constructor TExampleButton.Create(AOwner:TComponent);
  78. begin
  79.   inherited Create(AOwner);
  80.   self.Width              := 100;
  81.   self.Height             := 30;
  82.   Fstate                   := obsNormal;
  83.   FCanvas                 := TControlCanvas.Create;
  84.   TControlCanvas(FCanvas).Control := Self;    
  85.   FResim                  := TBGRABitmap.Create(self.ClientWidth,self.ClientHeight);
  86.   FBGDisable            := TBGRABitmap.Create(self.ClientWidth,self.ClientHeight);
  87.   FBGHover              := TBGRABitmap.Create(self.ClientWidth,self.ClientHeight);
  88.   FBGNormal            := TBGRABitmap.Create(self.ClientWidth,self.ClientHeight);
  89.   FBGPress              := TBGRABitmap.Create(self.ClientWidth,self.ClientHeight);
  90.  
  91.   FBGDisable.LoadFromFile('disable.png');
  92.   FBGHover.LoadFromFile('Hover.png');
  93.   FBGNormal.LoadFromFile('Normal.png');
  94.   FBGPress.LoadFromFile('press.png');
  95.  
  96. end;
  97.  
  98. destructor TExampleButton.Destroy;
  99. begin
  100.   FreeAndNil(Fcanvas);
  101.   FreeAndNil(FBGDisable);
  102.   FreeAndNil(FBGHover);
  103.   FreeAndNil(FBGNormal);
  104.   FreeAndNil(FBGPress);
  105.   FreeAndNil(Fresim);
  106.   inherited Destroy;
  107.  
  108. end;
  109.  
  110.  
  111.  
  112. procedure TExampleButton.WMEraseBkGnd(var Message: TWMEraseBkGnd);
  113. begin
  114.   Message.Result := - 1;
  115. end;
  116.  
  117. procedure TExampleButton .WMPaint(var Msg:TLMPaint);
  118. begin
  119.  ControlState := ControlState+[csCustomPaint];
  120.  inherited;
  121.  ControlState := ControlState-[csCustomPaint];
  122. end;    
  123.  
  124.  
  125. procedure TExampleButton.Paint;
  126. var
  127.   rc:TRect;
  128.   x, y: integer;
  129.   hdc1, SpanRgn: hdc;
  130.   WindowRgn: HRGN;
  131.   p: PBGRAPixel;
  132. begin
  133.  if not Assigned(fresim) then exit;
  134.   if fresim.Width<0 then exit;
  135.   if not Visible then exit;
  136.  
  137.        
  138.           fresim.SetSize(0,0);
  139.           fresim.SetSize(clientWidth,clientHeight);
  140.           fresim.Fill(BGRAPixelTransparent);
  141.           if Enabled = True then
  142.           begin
  143.             case Fstate of
  144.                   obsNormal  : begin fresim.PutImage(0,0,FBGNormal,dmDrawWithTransparency); self.font.Color:=FNormalC.Fontcolor end;
  145.                   obshover   : begin fresim.PutImage(0,0,FBGHover,dmDrawWithTransparency); self.font.Color:=FHoverC.Fontcolor end;
  146.                   obspressed : begin fresim.PutImage(0,0,FBGPress,dmDrawWithTransparency); self.font.Color:=FPressC.Fontcolor end;
  147.                   else
  148.                   begin fresim.PutImage(0,0,FBGNormal,dmDrawWithTransparency); self.font.Color:=FNormalC.Fontcolor end;
  149.                  end;
  150.  
  151.           end else
  152.           begin
  153.             fresim.PutImage(0,0,FBGDisable,dmDrawWithTransparency); self.font.Color:=FDisableC.Fontcolor;
  154.           end;
  155.  
  156.        
  157.  
  158.         if fresim.Width>0 then
  159.         begin
  160.  
  161.  
  162. /// if crop to image
  163.  
  164. {           WindowRgn := CreateRectRgn(0, 0, fresim.Width, fresim.Height);
  165.  
  166.             for Y := 0 to fresim.Height - 1 do
  167.             begin
  168.               p := fresim.Scanline[Y];
  169.               for X := fresim.Width - 1 downto 0 do
  170.               begin
  171.  
  172.                 if p^.Alpha < 20 then//<255 then
  173.                 begin
  174.                   p^ := BGRAPixelTransparent;
  175.                   SpanRgn := CreateRectRgn(x, y, x + 1, y + 1);
  176.                   CombineRgn(WindowRgn, WindowRgn, SpanRgn, RGN_DIFF);
  177.                   DeleteObject(SpanRgn);
  178.                 end;
  179.                 Inc(p);
  180.               end;
  181.             end;
  182.  
  183.             fresim.InvalidateBitmap;
  184.  
  185.  
  186.             SetWindowRgn(self.Handle, WindowRgn, True);
  187.             hdc1 := GetDC(self.Handle);
  188.             ReleaseDC(self.Handle, hdc1);
  189.             DeleteObject(WindowRgn);
  190.             DeleteObject(hdc1);
  191.  
  192. } // Crop to image
  193.  
  194.             rc:=ClientRect;
  195.             fresim.Canvas.Brush.Style:=bsClear;
  196.             DrawText(fresim.Canvas.Handle,Pchar(Caption),Length(Caption),rc,DT_CENTER or DT_VCENTER or DT_SINGLELINE);
  197.  
  198.             fresim.Draw(fCanvas,0,0);
  199.         end;
  200.  
  201.  
  202. end;
  203.  
  204. procedure TExampleButton.PaintWindow(DC:HDC);
  205. begin
  206.  FCanvas.Lock;
  207.   try
  208.     FCanvas.Handle := DC;
  209.     try
  210.       Paint;
  211.     finally
  212.  
  213.       FCanvas.Handle := 0;
  214.     end;
  215.   finally
  216.     FCanvas.Unlock;
  217.   end;
  218. end;    
  219.  
« Last Edit: December 19, 2024, 05:51:29 pm by Onur2x »

lainz

  • Hero Member
  • *****
  • Posts: 4667
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: how to set a button color in IDE?
« Reply #5 on: December 19, 2024, 01:20:17 pm »
Use BGRAControls, we have a lot of different kind of buttons, even image button.

 

TinyPortal © 2005-2018