⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faq12.htm

📁 C++builder学习资料C++builder
💻 HTM
字号:


<HTML>

<HEAD>

   <TITLE>Change a font's face and size from code</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 a font's face and size from code

</H3>

<P>

You can change a font's face by assigning a new value to it's <TT>Name</TT> property. You can change the size of the

font by assigning a value to either the font's <TT>Size</TT> property, or its <TT>Height</TT> property. The following

code changes the font of a label control.

</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>Size <b>=</b> <font color="blue">14</font><b>;</b>

        Label1<b>-></b>Font<b>-></b>Name <b>=</b> <font color="blue">"Courier New"</font><b>;</b>

    <b>}</b>

</pre>

<P>

<B>Note:</B> Before you change the <TT>Name</TT> property of a font, it is wise to make sure that the font is installed on

the system. The global <TT>Screen</TT> object and the object returned by the global <TT>Printers</TT> function both

contain a <TT>Fonts</TT> property that lists the fonts installed on the system. The next code example demonstrates how

you can determine if a font exists before making a change to the <TT>Name</TT> property.

</P>

<pre>

    <b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button2Click<b>(</b>TObject <b>*</b>Sender<b>)</b>

    <b>{</b>

        <b>if</b><b>(</b>Screen<b>-></b>Fonts<b>-></b>IndexOf<b>(</b><font color="blue">"Courier New"</font><b>)</b> <b>>=</b> <font color="blue">0</font><b>)</b>

            Label1<b>-></b>Font<b>-></b>Name <b>=</b> <font color="blue">"Courier New"</font><b>;</b>

        <b>else</b>

            ShowMessage<b>(</b><font color="blue">"Courier New not found"</font><b>)</b><b>;</b>

    <b>}</b>

</pre>

<P>

<B>Note:</B> The <TT>Size</TT> property of <TT>TFont</TT> specifies the height of the font in points. There are

approximately 72 points per inch. This means that if you set the <TT>Size</TT> property of a font to 72, the characters

will be 1 inch tall. If you specify a size of 12, the characters will be 1/6 of an inch tall.

</P>

<P>

<B>Note:</B> The <TT>Height</TT> property of <TT>TFont</TT> specifies the height of the font in pixels. When you use

the small fonts control panel setting, there are 96 pixels per inch. Since there are 72 points per inch, and 96 pixels

per inch, if you set the <TT>Size</TT> property to 72, then the <TT>Height</TT> of the font will be 96 pixels. You can

test this theory by setting the <TT>Size</TT> of font to 72 using the object inspector. The <TT>Height</TT> of a font

can be calculated from its size by the following equation

</P>

<pre>

    Height <b>=</b> <b>-</b> Size <b>*</b> PixelsPerInch <b>/</b> <font color="blue">72</font>

</pre>

<P>

<B>Note:</B> When the <TT>Size</TT> property is a positive value, the <TT>Height</TT> property will be negative. When

<TT>Size</TT> is negative, <TT>Height</TT> is positve. The two will never be the same sign. In most cases, the

<TT>Size</TT> property will be positive. When you launch the common font dialog box, the size listbox contains the

value that is assigned to the <TT>Size</TT> property of <TT>TFont</TT>. Since the size listbox always displays

positive values, <TT>Size</TT> ends up being positive, and <TT>Height</TT> ends up negative.

</P>

<P>

<B>Note:</B> Because the <TT>Size</TT> and <TT>Height</TT> properties can be negative, be careful not to write code

that malfunctions if the value is less than zero. For example, the following code appears to increase the size of a font by

2 pixels every time a button is clicked. Because the <TT>Height</TT> property usually starts out negative, the font

actually decreases in size instead of increasing. The label does start to grow when <TT>Height</TT> passes through 0

and becomes positive.

</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>Height <b>=</b> Label1<b>-></b>Font<b>-></b>Height <b>+</b> <font color="blue">2</font><b>;</b>

    <b>}</b>

</pre>

                                                

</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -