faq49.htm
来自「C++builder学习资料C++builder」· HTM 代码 · 共 163 行
HTM
163 行
<HTML>
<HEAD>
<TITLE>Set or remove the bold and italic settings of Font->Style</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Set or remove the bold and italic settings of <TT>Font->Style</TT>
</H3>
<P>
The <TT>Style</TT> property of <TT>TFont</TT> is a set object. If you
look in GRAPHICS.HPP, you will see something that looks like this:
</P>
<pre>
<b>enum</b> TFontStyle <b>{</b> fsBold<b>,</b> fsItalic<b>,</b> fsUnderline<b>,</b> fsStrikeOut <b>}</b><b>;</b>
<b>typedef</b> Set < TFontStyle<b>,</b> fsBold<b>,</b> fsStrikeOut <b>></b> TFontStyles<b>;</b>
<font color="navy">//...</font>
<font color="navy">// skip some stuff</font>
<font color="navy">// ...</font>
<b>class</b> <b>__declspec</b><b>(</b>pascalimplementation<b>)</b> TFont <b>:</b> <b>public</b> TGraphicsObject
<b>{</b>
<font color="navy">// ...</font>
<b>__published</b><b>:</b>
<font color="navy">// ...</font>
<b>__property</b> TFontStyles Style<b>=</b><b>{</b>read<b>=</b>GetStyle<b>,</b> write<b>=</b>SetStyle<b>,</b> nodefault<b>}</b><b>;</b>
<b>}</b><b>;</b>
</pre>
<P>
Notice that the <TT>Style</TT> property of <TT>TFont</TT> is a
<TT>TFontStyles</TT> set. To manipulate the <TT>Style</TT> of a font object you
must modify the <TT>TFontStyles</TT> set. You can use any of the set operations
to do your work. Here are some code examples.
</P>
<pre>
<font color="navy">// Add the bold style to the font of a label</font>
<font color="navy">// Any other Style settings remain intact</font>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> Label1<b>-></b>Font<b>-></b>Style <b><<</b> fsBold<b>;</b>
<font color="navy">// Remove bold style</font>
<font color="navy">// Any other Style settings remain intact</font>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> Label1<b>-></b>Font<b>-></b>Style <b>>></b> fsBold<b>;</b>
<font color="navy">// Add bold, italic, and underline in one shot</font>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> Label1<b>-></b>Font<b>-></b>Style<b><<</b>fsBold<b><<</b>fsItalic<b><<</b>fsUnderline<b>;</b>
<font color="navy">// Add bold and italic, but remove fsUnderline</font>
Label1<b>-></b>Font<b>-></b>Style<b>=</b><b>(</b>Label1<b>-></b>Font<b>-></b>Style<b><<</b>fsBold<b><<</b>fsItalic<b>)</b> <b>>></b>fsUnderline<b>;</b>
<font color="navy">// Clear all styles and return to normal</font>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> Label1<b>-></b>Font<b>-></b>Style<b>.</b>Clear<b>(</b><b>)</b><b>;</b>
<font color="navy">// Another way to clear all styles</font>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> TFontStyles<b>(</b><b>)</b><b>;</b>
<font color="navy">// Set the Style to bold, and nothing else.</font>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> TFontStyles<b>(</b><b>)</b> <b><<</b> fsBold<b>;</b>
<font color="navy">// Test to see if the Style contains fsBold.</font>
<font color="navy">// Note that Contains is called using a dot and not -></font>
<b>if</b><b>(</b> Label1<b>-></b>Font<b>-></b>Style<b>.</b>Contains<b>(</b>fsBold<b>)</b><b>)</b>
Application<b>-></b>Terminate<b>(</b><b>)</b><b>;</b>
<font color="navy">// Toggle the fsBold property</font>
<b>if</b><b>(</b> Label1<b>-></b>Font<b>-></b>Style<b>.</b>Contains<b>(</b>fsBold<b>)</b><b>)</b>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> Label1<b>-></b>Font<b>-></b>Style <b>>></b> fsBold<b>;</b>
<b>else</b>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> Label1<b>-></b>Font<b>-></b>Style <b><<</b> fsBold<b>;</b>
<font color="navy">// mask out the fsUnderline and fsStrikeThrough properties.</font>
<font color="navy">// leave the other two alone.</font>
TFontStyles style<b>;</b>
style <b><<</b> fsBold <b><<</b> fsItalic<b>;</b>
style <b>*</b><b>=</b> Label1<b>-></b>Font<b>-></b>Style<b>;</b> <font color="navy">// set intersection, similar to bitwise and</font>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> style<b>;</b>
</pre>
<P>
<B>Note:</B> Changing the font property of a control does not immediately redraw the
control with it's new font. The control is invalidated, and will be redrawn when the
application gets a chance to process the messages in its queue. To see this concept in
action, use the <TT>Sleep</TT> command to halt the process directly after a change
to the font of a <TT>TLabel</TT> control. You should see that the control is not
repainted until after the delay (see code below). To force a repaint immediately,
call the <TT>Update</TT> method of <TT>TLabel</TT>.
</P>
<pre>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> Label1<b>-></b>Font<b>-></b>Style <b><<</b> fsBold<b>;</b>
Sleep<b>(</b><font color="blue">4000</font><b>)</b><b>;</b>
<font color="navy">// Label does not redraw until after the delay, when</font>
<font color="navy">// the function returns.</font>
<b>}</b>
</pre>
<P>
<B>Note:</B> The insertion and extraction operators<TT> << </TT> and<TT> >> </TT>
are members of the set class. You can insert items into a sets using code
like this (note the lack of an equal sign) because<TT> << </TT> is a member
function of <TT>TFontStyles</TT>:
</P>
<pre>
TFontStyles style<b>;</b>
style <b><<</b> fsBold<b>;</b>
</pre>
<P>
However, you cannot insert items directly into properties of a control. The
code below does not work. It compiles, but the <TT>Style</TT> property of the
control is not modifed. This is due to the way that properties work. The read
method of the property returns the <TT>TFontStyles</TT> value, but the write
method for the property is not called after you modify the property.
</P>
<pre>
<font color="navy">// This code does not work.</font>
Label1<b>-></b>Font<b>-></b>Style <b><<</b> fsBold<b>;</b>
</pre>
<P>
<B>Note:</B> The read and write methods for the <TT>Style</TT> property must
call private <TT>GetData</TT> and <TT>SetData</TT> functions within the
<TT>TFont</TT> class. Since every read and write of a control's font <TT>Style</TT>
will call these functions, you may want to minimize the number of times you
access the <TT>Style</TT> property directly if you are doing a lot of
manipulation with the font. The code below shows two ways of doing the same
thing, but the second strategy is a little more efficient.
</P>
<pre>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> TFontStyles<b>(</b><b>)</b><b>;</b>
<b>if</b> <b>(</b>CheckBox1<b>-></b>Checked<b>)</b>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> Label1<b>-></b>Font<b>-></b>Style <b><<</b> fsBold<b>;</b>
<b>if</b> <b>(</b>CheckBox2<b>-></b>Checked<b>)</b>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> Label1<b>-></b>Font<b>-></b>Style <b><<</b> fsItalic<b>;</b>
<b>if</b> <b>(</b>CheckBox3<b>-></b>Checked<b>)</b>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> Label1<b>-></b>Font<b>-></b>Style <b><<</b> fsUnderline<b>;</b>
<b>if</b> <b>(</b>CheckBox4<b>-></b>Checked<b>)</b>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> Label1<b>-></b>Font<b>-></b>Style <b><<</b> fsStrikeOut<b>;</b>
<font color="navy">// a better way to do the same thing</font>
TFontStyles style<b>;</b>
<b>if</b> <b>(</b>CheckBox1<b>-></b>Checked<b>)</b>
style <b><<</b> fsBold<b>;</b>
<b>if</b> <b>(</b>CheckBox2<b>-></b>Checked<b>)</b>
style <b><<</b> fsItalic<b>;</b>
<b>if</b> <b>(</b>CheckBox3<b>-></b>Checked<b>)</b>
style <b><<</b> fsUnderline<b>;</b>
<b>if</b> <b>(</b>CheckBox4<b>-></b>Checked<b>)</b>
style <b><<</b> fsStrikeOut<b>;</b>
Label1<b>-></b>Font<b>-></b>Style <b>=</b> style<b>;</b>
</pre>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?