How to change the font color of a theme-enabled item?

Yes, this is again a question:

How to change TCheckBox font color (or any managed control) using Delphi7-> Delphi2007 in a theme-supported application?

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

  • and Most populare (even from QC): you cannot, it was developed by Microsoft.
  • Create a component that allows you to draw it as you want.
  • Buy an expensive kit of components that is drawn the way you want.
  • Do not use themes.

Good, but I'm still not happy with this.

Giving the user color feedback for the status of the property / data that he has in the form seems legal to me.

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

This is not like Microsoft designed it that way. now the question again:

How to change TCheckBox font color (or any managed control) using Delphi 7 through Delphi 2007 in a theme-enabled application?

+7
delphi themes
source share
4 answers

This requires some tweaking to be the perfect solution, but worked for me:

Add 2 methods 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 method:

 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; 
+4
source share

Oh, but you can!

Just put this before your form declaration:

 TCheckBox = class(StdCtrls.TCheckBox) public procedure CNCtlColorStatic(var Message: TWMCtlColorStatic); message CN_CTLCOLORSTATIC; end; 

This re-declaration of TCheckBox is now used at runtime as a type streaming from your DFM form. Now do the following:

 procedure TCheckBox.CNCtlColorStatic(var Message: TWMCtlColorStatic); begin SetTextColor(Message.ChildDC, ColorToRGB(clRed)); // or RGB(255,0,0)); SetBkMode(Message.ChildDC, TRANSPARENT); Message.Result := GetStockObject(NULL_BRUSH); end; 

This intercepts the WM_CTLCOLORSTATIC message and changes the text color to red. This works in non-thematic mode for me (using WinXP classic), but not in thematic mode.

You must be aware that in order to allow the theme controls to send you this message, the control must provide the DTPB_USECTLCOLORSTATIC flag to Theme-drawing API. Unfortunately, this is not the default behavior, and I do not know how to do this. Take a look at this question.

+2
source share

Here is how I solved it in my application:

  • Uncheck the box and make it smaller - simple enough to show the actual box without a box.
  • Place a TLabel next to this flag to act as a signature.
  • In the Label OnClick event, toggle the state of the checkbox.
  • If there is an Alt + letter keyboard accelerator on the label, make the form handle the CM_DIALOGCHAR message. Copy the message handler from the TLabel source code and add a line to toggle the state of the check box.

This is not a real flag, and it works a little more than we would like, but it works quite well in my application (which has only one flag that needs this treatment).

+1
source share

Option 5. Use the tyou control as a base parameter and override all the drawing messages in the control (yes, you can call it a component, but the control is the name for the visible components, so you should use it). Just catch WM_PAINT, maybe WM_NCPAINT, you can draw the control in your own style. At least you can reuse all the functionality from the control. Until you change the layout, only colors that you do not need to change the mittedowns canvases. moving etc. etc.

Note. I have experience redefining TCustomEdit, allowing all colors, background text, extra buttons, etc. to be used. It takes enough time to fix this and read all the documents from MSDn and KB to make sure that the control did what it wanted.

0
source share

All Articles