"MFC" MFC changes the font size of static text in dialog boxes

Source: Internet
Author: User

MFC changes the font size of static text in a dialog box 2010/08/09 11:30

VC dialog box font settings are valid for all controls, and you cannot change the font of a static text individually. For your problem, you need to first create a Font object with CreateFont, and then call the control's setfont.
Example:
1, change the ID of the static style, such as: Idc_static1
2. Add an edit control to create an associated control M_editcontrol.
3. Add the following code to the OnInitDialog:
CFont * F;
f = new CFont;
F->createfont (+,//nheight
0,//nwidth
0,//nescapement
0,//norientation
Fw_bold,//Nweight
TRUE,//Bitalic
FALSE,//Bunderline
0,//cstrikeout
Ansi_charset,//Ncharset
Out_default_precis,//Noutprecision
Clip_default_precis,//Nclipprecision
Default_quality,//Nquality
Default_pitch | Ff_swiss,//npitchandfamily
_t ("Arial")); Lpszfac

M_editcontrol.setfont (f);
M_editcontrol.setwindowtext ("Ask the Experts");
GetDlgItem (IDC_STATIC1)->setfont (f);

Run the results as shown, download the complete example Ctrlfont.zip.

It is important to note that here we are using the CFont pointer instead of the normal CFont local variable, if the program is changed to the following:
CFont F;
F.createfont (+,//nheight
0,//nwidth
0,//nescapement
0,//norientation
Fw_bold,//Nweight
TRUE,//Bitalic
FALSE,//Bunderline
0,//cstrikeout
Ansi_charset,//Ncharset
Out_default_precis,//Noutprecision
Clip_default_precis,//Nclipprecision
Default_quality,//Nquality
Default_pitch | Ff_swiss,//npitchandfamily
_t ("Arial"));

M_editcontrol.setfont (&F);
M_editcontrol.setwindowtext ("Ask the Experts");
GetDlgItem (IDC_STATIC1)->setfont (&f);

After running it will not get the desired effect, because the OnInitDialog function is completed, CFont F will be cleared. You must ensure that the Font object set by SetFont is valid until the dialog box is closed. Therefore, it is common to use pointers or class members to save the created Font object.
Non-MFC program, first use CreateFont to create a font handle, and then SendMessage to the control Wm_setfont message, the established font handle is assigned to the past, it is possible.

CheightIs the height of the font.CwidthIs the width of the font.cescapementis the tilt angle of the font.corientationis the tilt angle of the font.CweightIs the weight of the font.BitalicIs whether the font is italic.BunderlineIs whether the font has an underscore.BstrikeoutIs whether the font has strikethrough.IcharsetIs the character set used by the font.ioutprecisionis to specify how to select the appropriate font.iclipprecisionis used to determine the precision of the cropping.iqualityHow to match the selected font.

ipitchandfamily is the spacing flag and attribute flags.

Pszfacename is the name of the font.

MSDN Translation of the CreateFont () function

function function: This function creates a special logical font that can be selected by any device at a later time.

Function prototypes: Hfont createfont (int nheight,
int nwidth,
int Nescapement,
int Norientation,
int Fnweight,
DWORD Fdwltalic,
DWORD Fdwunderline,
DWORD Fdwstrikeout,
DWORD Fdwcharset,
DWORD Fdwoutputprecision,
DWORD Fdwclipprecision,
DWORD Fdwquality,
DWORD fdwpitchandfamily,
LPCTSTR lpszface);

Nheight: Specifies the logical unit height of the character cell or character of the font, the height value of the character (also called Em height), which is the character cell height minus the internal header value. The font mapper interprets the values specified by the nheight as follows, meaning the values are:

>0: The font mapper converts this value in device units and matches the cell height of the existing font.

0: The font mapper conversion uses a default height value when selecting a match.

<0: The font mapper converts this value to device units and matches its absolute value to the character height of the existing font.

Comparing all the heights, the font mapper chooses a maximum font size that does not exceed the required sizes.

This mapping occurs when the font is used for the first time.

For mm_text mapping, you can use the following formula to determine the height of a font that specifies a point size:

Nheight=-muldiv (Pointsize, GetDeviceCaps (HDC, Logpixelsy), 72)

Nwidth: Specifies the average width of the logical units of characters for the requested font. If this value is 0, the font mapper selects a closest match value, which is determined by comparing the absolute value of the current device's feature factor to the difference between the digitized feature factor of the font that can be used.

Nescapement: Specifies an angle between the shift vector and the x-axis of the device, in One-tenth-degree units. The shift vector is parallel to the baseline of the body line.

Windows NT: When the graphics device is set to gm_advanced, you can specify the shift angle of the string without relying on the position angle of the character string.

When the graphics mode is set to Gm_compatible, nescapement specify both the shift angle and the location angle, and you can set nescapement and norientation to the same value.

Windows 95:nescapement also specifies the shift angle and the location angle, which can be set nescapement and norientation to the same value.

Norientation: Specifies the angle between the baseline of each character and the x-axis of the device.

Fnweight: Specifies the weight of the font between 0 and 1000, such as 400 for the standard body, 700 for the black (thick) body, and if this value is 0, the default weight value is used.

For ease of definition, the following values can be used:

fw_dontcare:0;fw_thin;100;fw_extralight;200;fw_ultralight;200;fw_light;300;

fw_normal:400;fw_regular;400;fw_medium;500;fw_semibold;600;fw_demibold;600;

fw_bold:700;fw_extrabold;800;fw_ultrabold;800;fw_heavy;900;fw_black;900.

Fdwitalic: Specifies italic if set to true.

Fdwunderline: If set to True, specifies that the underlined word is full.

Fdwstrikeout: If set to True, strikeout specifies the font.

Fdwcharset: Specifies the character set, the following values are predefined:

Ansi_charset; Baltic_charset; Chinesebig5_charset; Default_charset;

Easteurope_charset; Gb2312_charset; Greek_charset; Hangul_charset; Mac_charset; Oem_charset; Russian_charset; Shiftjis_charset;

Symbol_charset; Turkish_charset.

Korea Windows:johab_charset;

Middle East Windows:hebrew_charsset; Arabic_charset

Windows:thai_charset Thailand

OEM_CHARSET Specifies the character set associated with the operating system.

You can use the Default_charset value to allow the name and size of the font to adequately describe the logical font. If the specified font name does not exist, the font for any character set can be substituted for the specified font, so be careful to use default_charset to avoid undesirable results.

There are other character set fonts in the operating system. If an application uses a font of an unknown character set, the application does not attempt to translate or interpret a string written in that font.

This parameter is important in the font mapping process. To ensure consistent results, specify a special character set. If a font name is specified in the Lpszface parameter, determine whether the Fdwcharset value matches the font character set specified by Lpszface.

Fdwoutputprecision: Specifies the output precision, the output precision of the output with the required font height, width, character positioning, shift, character spacing and character type matching program, it is preferable to one of the following values:

Out_character_precis; not used.

Out_default_precis: Specifies the default font mapper state.

Out_device_precis: Indicates that the font mapper selects a device font when there are multiple fonts in the system that use the same name in the same font.

Out_outline_prcis: This value in Windows NT indicates that the font mapper is selected from TrueType and other border-based fonts.

Out_raster_precis: Indicates that the font mapper chooses a raster font when there are multiple fonts in the system that use the same name.

Out_string_precis: This value is not used by the word full mapper, but is returned as a value when the scanned font is enumerated.

Out_stroke_precis: This value is not used by the font mapper in Windows NT, but as a return value when TrueType fonts, other border-based fonts, and vector fonts are enumerated.

Windows 95: This value is not used by the font mapper, but is returned as a value when TrueType fonts or vector fonts are enumerated.

Out_tt_only_precis: Indicates that the font mapper is selected only from a TrueType font, and the font mapping returns the default state if no TrueType fonts are installed on the system. 、

Out_tt_precis: Indicates that the font mapper chooses a TrueType font when there are multiple fonts with the same name in the system.

When the operating system contains multiple fonts with the same name as the specified names, the application can use the Out_device_precis,out_raster_precis and Out_tt_precis values to control how the font mapper chooses a font, for example, If the operating system contains a raster of name symbol and TrueType two fonts, specify OUT_TT_PRECIS to make the font mapper select TrueType mode. Specify OUT_TT_ONLY_PRECIS to have the font mapper select a TrueType font, although this will give the TrueType font a name.

fdwclipprecision; Specifies the cropping precision, the cropping precision defines how to crop a portion of a character that is out of the clipping area, and it may have one or more of the following values:

Clip_default_precis: Specifies the default cropping state. Clip_character_precis: Not used.

Clip_stroke_precis: Not used by font mapper, but as a return value when raster fonts, vector fonts, or TrueType fonts are enumerated. In a Windows environment, for compatibility, this value is always returned when the font is enumerated.

Clip_mask: Not used. Clip_embedded: This flag must be used to use embedded read-only fonts.

Clip_lh_angles: When this value is used, the rotation of all fonts depends on whether the coordinate system is positioned in the left or right direction.

If this value is not used, the device font is always rotated counterclockwise, but the rotation of the other fonts depends on the orientation of the coordinate system. For more information on the orientation of the coordinate system, see parameter orientation.

Clip_tt_always: Not used.

Fdwquality: Point to output quality, output quality defines how GDI carefully matches logical font properties to actual physical font properties. It is preferable to one of the following values:

Default_quality: The appearance of the font is not important.

Draft_quality: The importance of font appearance is greater than when using proof_quality, the scaling is active for GDI raster fonts, which means that a variety of font sizes are available, but the quality may not be high, if necessary, bold, italic, underline, Strikeout fonts can be used in combination.

Proof_quality: Character quality is more important than exactly matching logical font font attributes. For GDI scan fonts, the scale is active and the closest size is selected. Although the font size does not match exactly when using proof_quality, the font quality is high and there is no appearance distortion. Bold, italic, underline, strikeout fonts can be used in combination if necessary.

Fdwpitchandfamily: Specifies the font spacing and font family, low-end two-bit character spacing for the specified font, which is preferable to one of the following values:

Default_pitch;fixed_pitch; Variable_pitch

High-end four-bit specified font family, one of the following values is desirable:

Ff_decorative: Novel fonts, such as vintage English. Ff_dontcare: Do not care or do not know.

Ff_mdern: Stroke width fixed font, with or without liner. such as Pica, Elite and Courier New.

Ff_roman: The font of the stroke width change, with a lined line. such as Ms Serif.

Ff_script: Designed to look like a handwriting font. such as script and cursive.

Ff_swiss: Stroke width change font, no slash. such as Ms Sans Serif.

An application can combine character spacing and font families with operator or to assign a value to fdwpitchandfamily.

The font family describes the normal appearance of a font that can be used to specify fonts when all the exact words are not available.

Lpszface: A string pointer ending with/0 that points to the typeface name of the specified font, the length of the string cannot exceed 32 characters (including the character/0), and the function enumfontfamilies can be used to enumerate the typeface names of all currently available fonts.

If lpszface is null or points to an empty string, GDI uses the first font that can match other properties.

Return value: If the function call succeeds, the return value is a logical font handle, and if the function call fails, the return value is null.

Windows NT: To get more error messages, call the GetLastError function.

Note: When a font is no longer in use, DeleteObject can be used to remove it.

To protect the copyrights of vendors who provide fonts to Windows and Windows NT, WIN32-based applications always list the exact names of the selected fonts. Because different systems use different fonts, don't assume that the font you choose is the one you want. For example, if a font called Palatino is required, but the system does not provide a font, the font mapper will replace it with a font that does not have the same name but has similar properties. The system always reports the font name selected by the user.

Quick check: Windows nt:3.1 and later; windows

"MFC" MFC changes the font size of static text in dialog boxes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.