DELPHI - How to change a TButton background color in a VCL Form Application?
Asked Answered
C

3

5

In my Delphi VCL Form Application, I have to change the background color of a TButton. Is there a way to apply this change without using third-party components?

Thanks.

Cavour answered 27/3, 2013 at 16:45 Comment(3)
an example component inherited from TButton with sourcecan be found here delphi.about.com/od/vclwriteenhance/l/aa061104a.htmLarock
@Larock there is no source code at the linkSiccative
@MehmetFide: Try here, instead: gist.github.com/turric4n/3abea102e562c20cac306f2461b4a9d1Planet
B
7

You can't change the color of a TButton component directly, instead you must use a third-party component or owner draw the button (BS_OWNERDRAW).

Beanstalk answered 27/3, 2013 at 16:57 Comment(2)
How can I implement the drawing of the Button?Cavour
The link in the answer covers that. You'll need to respond to WM_DRAWITEM.Regulus
P
2
  1. Create a panel.
  2. Change panel's color to any color.
  3. Empty panel's caption.
  4. Create a SpeedButton inside the panel.
  5. Set SpeedButton's Flat property True.
  6. Set SpeedButton's Align propert alClient.
  7. Voila! Here is your button with color!

Original source: https://engineertips.wordpress.com/2020/07/21/delphi-button-background-color/

Preparedness answered 21/7, 2020 at 10:28 Comment(1)
10.2 Tokyo. This works only in the design mode. When I try this in source code, the color goes away.Fax
B
0

Nice tip, thank you, Xel Naga!

You can also place an image on the panel, to make your button even nicer. Here I’ve added an image of a Yellow- Glass- button. To get the rounded corners, you shape the panel like this >>

SetWindowRgn(Panel1.Handle, CreateRoundRectRgn(0, 0, Panel1.Width, Panel1.Height, 65, 40), True);

enter image description here enter image description here enter image description here enter image description here

Next level after that, you can add 3 more images (on top of each-other) for button states: Mouse Over (OnMouseEnter), Button Pressed (OnMouseDown) and Button Disabled. Just change the Visible state of the images to reveal the one you want to see...

Broadloom answered 14/12, 2021 at 3:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.