Jump to content
davornik

TButton: change font color

Recommended Posts

I am trying to use TButton, but with changed caption Font.Color. Remy suggested to use BS_OWNERDRAW and intercept WM_DRAWITEM.

and here also to subclass:

 

https://stackoverflow.com/a/23125580

 

I tried it to use like this but no success, font color does not change.

...
type
  TMyButton = class(TButton)
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  end;

function ButtonSubclassProc(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM; uIdSubclass: UINT_PTR; dwRefData: DWORD_PTR): LRESULT; stdcall;

var
  btnFntClr: TMyButton;
...

function ButtonSubclassProc(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM; uIdSubclass: UINT_PTR; dwRefData: DWORD_PTR): LRESULT; stdcall;
begin
  case uMsg of
    WM_DRAWITEM: TMyButton(dwRefData).Font.Color := clRed;
    WM_NCDESTROY: RemoveWindowSubclass(hWnd, @ButtonSubclassProc, uIdSubclass);
  end;
  Result := DefSubclassProc(hWnd, uMsg, wParam, lParam);
end;

procedure TMyButton.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  with Params do Style := Style or BS_OWNERDRAW;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  btnFntClr.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  btnFntClr:=TMyButton.Create(Form1);
  btnFntClr.Parent:=Form1;
  btnFntClr.Style:=TCustomButton.TButtonStyle.bsSplitButton;
  btnFntClr.Caption:='Options';
  with btnFntClr do begin
    Left:=10;
    Top:=10;
    Width:=120;
  end;
  SetWindowSubclass(btnFntClr.Handle, @ButtonSubclassProc, 1, DWORD_PTR(btnFntClr));
end;

How to properly change font color in TButton?

Share this post


Link to post
On 2/21/2025 at 11:19 AM, dummzeuch said:

You could use TBitBtn instead, if that's an option.

Unfortunatelly, TButton is exactly what I need because TCustomButton.TButtonStyle.bsSplitButton State.

Share this post


Link to post
On 2/21/2025 at 11:27 AM, PeaShooter_OMO said:

@davornik

 

BS_OWNERDRAW means you must do the drawing of the button yourself. You must also detect user interactions and state changes and respond to them visually.

 

Here is an exmple of such a button (as @Remy Lebeau pointed out on Stack Overflow) :  Delphi - Pascal Windows Color Button

I have seen this example.

Is there any way to just change font color without drawing a complete button?

Share this post


Link to post
1 hour ago, davornik said:

I have seen this example.

Is there any way to just change font color without drawing a complete button?

Not if you use the standard Windows style. If you configure the app to use one of the custom styles (like Windows 10) you can set the StyleElements.seFont property element to false and it will then use the font.color you set and not the Windows default.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×