Delphi – How to you change the font color of a theme-enabled control

delphithemes

Yes, this is again this question:

How can I change the font color of a TCheckBox (or any handled control) with Delphi7->Delphi2007 on a themes enabled application?

After reading a lot on the internet and on this site, I found 4 kinds of answer:

  1. and Most populare (even from QC): You can't, it's designed like that by Microsoft.
  2. Create a component that let you draw it like you want.
  3. Buy expensive component set that draws like you want.
  4. Do not use themes.

OK, but I am still unhappy with that.

Giving a user colored feedback for the status of a property/data he has on a form, seems legitimate to me.

Then I just installed the MSVC# 2008 Express edition, and what a surprise, they can change the color of the font (property ForeColor of the check box) Then what?

It does not seem to be a "it's designed like that, by Microsoft." then now the question again:

How can I change the font color of a TCheckBox (or any handled control) with Delphi 7 through Delphi 2007 on a theme-enabled application?

Best Answer

This needs some tweak to be perfect solution but worked for me:

Add 2 method to your checkbox component

    FOriginalCaption: string;
    _MySetCap: Boolean;
    procedure WMPaint(var msg: TWMPaint); message WM_PAINT;
    procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;

and implement this way:

procedure TMyCheckbox.CMTextChanged(var Message: TMessage);
begin
  inherited;
  if _MySetCap then Exit;
  FOriginalCaption := Caption;
end;

procedure TMyCheckbox.WMPaint(var msg: TWMPaint);
var
  BtnWidth: Integer;
  canv: TControlCanvas;
begin
  BtnWidth := GetSystemMetrics(SM_CXMENUCHECK);

  _MySetCap := True;
  if not (csDesigning in ComponentState) then
    Caption := '';
  _MySetCap := False;
  inherited;
  canv := TControlCanvas.Create;
  try
    canv.Control := Self;
    canv.Font := Font;
    SetBkMode(canv.Handle, Ord(TRANSPARENT));
    canv.TextOut(BtnWidth + 1, 2, FOriginalCaption);
  finally
    canv.Free;
  end;
end;
Related Topic