📄 faq58.htm
字号:
<HTML>
<HEAD>
<TITLE>Change the text color on a push button</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Change the text color on a push button
</H3>
<P>
A standard windows push button does not allow you to easily change the color of its text.
This explains why changing the font properties of a standard <TT>TButton</TT> doesn't have
any impact. In order to change the text color of a button, you must either subclass the
control or owner draw the control. Fortunately, C++Builder provides a simple workaround.
The <TT>BitBtn</TT> control already owner draws itself. In fact, it owner draws itself
even if you don't display a glyph on the button.
</P>
<P>
To change the color of the text on a button, simply replace it with a <TT>BitBtn</TT>
component. Set the text color using the <TT>Font</TT> property of the button.
</P>
<P>
<B>Note:</B> You can prove that <TT>TBitBtn</TT> is owner drawing itself by looking at the
<TT>CreateParams</TT> member function. It looks like this (converted to C++):
</P>
<pre>
<b>void</b> <b>__fastcall</b> TBitBtn<b>:</b><b>:</b>CreateParams<b>(</b>TCreateParams <b>&</b>Params<b>)</b>
<b>{</b>
TButton<b>:</b><b>:</b>CreateParams<b>(</b>Params<b>)</b><b>;</b>
Params<b>.</b>Style <b>|=</b> BS_OWNERDRAW<b>;</b>
<b>}</b>
</pre>
<P>
<B>Note:</B> After proving that <TT>TBitBtn</TT> is using old-fashioned owner drawing, you
can then see for yourself how <TT>TBitBtn</TT> uses the <TT>Font</TT> property. Look
inside the <TT>TBitBtn::DrawItem</TT> function. You should see something like this inside
the function (once again, I converted the code to C++ and added comments):
</P>
<pre>
<font color="navy">// FCanvas is the canvas that the button is painted with.</font>
<font color="navy">// this->Font is the Font property of the control</font>
FCanvas<b>-></b>Font <b>=</b> <b>this</b><b>-></b>Font<b>;</b>
<font color="navy">// after setting the font, a function is called the draws the glyph,</font>
<font color="navy">// the text, and the background of the button using FCanvas</font>
</pre>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -