Joke Collection Website - Public benefit messages - How to change the font size of controls in mfc dialog box

How to change the font size of controls in mfc dialog box

The dialog font setting of VC is valid for all controls, and the font of static text cannot be changed separately. First, create a Font object with CreateFont, and then call the control's SetFont.

Example:

1. Change the ID of a static style, for example, IDC_STATIC 1.

2. Add an edit control and establish an associated control m_editControl.

3. Add the following code in the OnInitDialog:

CFont * f;

F = new CFont

f-& gt; CreateFont( 16, //n height

0, //n width

0,// nEscapement

0, //n direction

FW_BOLD,//NW weight

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_editControlset font(f);

M_editControlSetWindowText ("consult an expert");

GetDlgItem(IDC _ static 1)-& gt; set font(f);

The running results are shown in the following figure. Download the complete sample CtrlFont.zip

It should be noted that CFont pointers are used here, instead of ordinary CFont local variables. If the program changes to the following:

CFont f;

F.CreateFont( 16, //n height

0, //n width

0,// nEscapement

0, //n direction

FW_BOLD,//NW weight

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_editControlset font(& amp; f);

M_editControlSetWindowText ("consult an expert");

GetDlgItem(IDC _ static 1)-& gt; set font(& amp; f);

You can't get the desired effect after running, because CFont f will be cleared after the OnInitDialog function is executed. Before closing the dialog box, you must ensure that the font object set by SetFont is valid. Therefore, pointers or class members are usually used to save established font objects.

For non-MFC programs, first create a font handle with CreateFont, and then send a message to the control WM_SETFONT with SendMessage, and allocate the established font handle.

CHeight is the height of the font.

CWidth is the width of the font.

CEscapement is the inclination angle of the font.

COrientation is the inclination angle of the font.

CWeight is the thickness of the font.

BItalic is whether the font is italic.

BUnderline is whether the font is underlined or not.

BStrikeOut is whether the font has strikethrough.

ICharSet is the character set used by fonts.

IOutPrecision is to specify how to choose the appropriate font.

IClipPrecision is used to determine the accuracy of cutting.

How does iQuality match the selected font?

IPitchAndFamily is an interval symbol and an attribute symbol.

PszFaceName is the name of the font.

MSDN translation of CreateFont () function

Function: This function creates a special logical font that can be selected by any device in the future.

Function prototype: HFONT CreateFont(int nHeight,

int nWidth,

Interior landscape,

Nutrition,

int fnWeight,

DWORD fdwltalic,

DWORD fdwUnderline,

DWORD fdwStrikeOut,

DWORD fdwCharSet,

DWORD fdwOutputPrecision,

DWORD fdwClipPrecision,

DWORD fdwQuality,

DWORD fdwPitchAndFamily,

LPCTSTR lpsz face);

Parameters:

NHeight: Specifies the character unit or logical unit height of the font. The height value of a character (also called em height) refers to the unit height value of the character minus the internal header value. The font mapper interprets the values specified by nHeight in the following ways, and each value represents:

& gt0: Font Mapper converts this value to device units and matches the unit height of existing fonts.

0: The Font Mapper transformation uses the default height value when selecting matches.

& lt0: The font mapper converts this value into device units and matches its absolute value with the character height of the existing font.

Comparing all heights, the font mapper selects the largest font that does not exceed the required size.

This mapping will appear the first time you use this font.

For the MM_TEXT mapping mode, the following formula can be used to determine the height of a font with a specified point size:

nHeight=-MulDiv(PointSize,GetDeviceCaps(hDC,LOGPIXELSY),72)

NWidth: Specifies the average width of logical units of characters in the required font. If the value is 0, the font mapper selects the closest matching value, which is determined by comparing the absolute value of the difference between the feature coefficient of the current device and the digitized feature coefficient of the available font.

N escape: Specifies the angle between the displacement vector and the X-axis of the device, in tenths of a degree. The displacement vector is parallel to the baseline of the normal.

Windows NT: When the graphics device is set to GM_ADVANCED, you can specify the offset angle of the string, regardless of the positioning angle of the characters in the string.

When the drawing mode is set to GM_COMPATIBLE, nEscapement specifies the offset angle and positioning angle, and you can set nEscapement and nOrientation to the same value.

Windows 95: n escape specifies the offset angle and positioning angle, and you can set n escape and nOrientation to the same value.

NOrientation: Specifies the angle between the baseline of each character and the X axis of the device.

FnWeight: Specify the font thickness between 0 and 1000, such as 400 for standard font and 700 for black font. If the value is 0, the default weight will be used.

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

FW _ dont care:0; FW _ THIN 100; FW _ EXTRALIGHT200; FW _ ULTRALIGHT200; FW _ LIGHT300;

FW _ NORMAL:400; FW _ general; 400; FW _ MEDIUM500; FW _ SEMIBOLD600; FW _ DEMIBOLD600;

FW _ BOLD:700; FW _ EXTRABOLD800; FW _ ULTRABOLD800; FW _ HEAVY900; FW _ BLACK900。

FdwItalic: If set to TRUE, italics are specified.

FdwUnderline: If set to TRUE, the underlined word is specified.

FdwStrikeOut: If set to TRUE, strikethrough specifies the font.

FdwCharSet: Specifies the character set, and the following values are predefined:

ANSI _ CHARSET Baltic character set; CHINESEBIG5 _ CHARSET default _ character set;

Eastern Europe _ Character Set; GB 23 12 _ CHARSET; Greek _ charsethangul _ charsetmac _ charsetoem _ charsetrussian _ character set; SHIFTJIS _ CHARSET

SYMBOL _ CHARSET Turkish character set.

South Korea Windows:JOHAB _ charset;;

Windows in the Middle East: Hebrew _ charset; Arabic _ Character Set

Windows in Thailand: Thailand _ charset

The character set specified by OEM_CHARSET is related to the operating system.

You can use the DEFAULT_CHARSET value to allow the name and size of the font to fully describe the logical font. If the specified font name does not exist, any font of character set can replace the specified font, so you should use DEFAULT_CHARSET carefully to avoid unexpected results.

Fonts of other character sets exist in the operating system. If an application uses a font with an unknown character set, the application will not try to translate or interpret a string written in that font.

This parameter is very 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, that is, the matching program between the output and the required font height, width, character positioning, displacement, character spacing and character type. It can take one of the following values:

OUT _ CHARACTER _ PRECIS is useless.

OUT_DEFAULT_PRECIS: Specifies the default font mapping state.

OUT_DEVICE_PRECIS: instructs the font mapper to select a device font when there are multiple fonts with the same font and name in the system.

OUT_OUTLINE_PRCIS: In Windows NT, this value instructs the font mapper to choose from TrueType and other border-based fonts.

OUT_RASTER_PRECIS: instructs the font mapper to select a raster font when there are multiple fonts with the same name in the system.

OUT_STRING_PRECIS: This value is not used by the word mapper, but is used as a return value when enumerating scanned fonts.

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

Windows 95: The font mapper does not use this value, but it is used as a return value when enumerating TrueType fonts or vector fonts.

OUT_TT_ONLY_PRECIS: instructs the font mapper to select only TrueType fonts. If the TrueType font is not installed in the system, the font mapping will return to the default state. 、

OUT_TT_PRECIS: When there are multiple fonts with the same name in the system, instructs the font mapper to select a TrueType font.

When the operating system contains multiple fonts with the same name as the specified name, the application can use the values of out _ device _ precis, out _ raster _ precis and OUT_TT_PRECIS to control how the font mapper selects fonts. For example, if the operating system contains two fonts, namely raster and TrueType, specifying OUT_TT_PRECIS causes the font mapper to select TrueType. Specifying OUT_TT_ONLY_PRECIS will cause the font mapper to select a TrueType font, although this will change the name of the TrueType font.

FdwClipPrecision specifies the cropping precision, which defines how to crop some characters beyond the cropping area. It can take one or more of the following values:

CLIP_DEFAULT_PRECIS: Specifies the default clip state. CLIP_CHARACTER_PRECIS: not used.

CLIP_STROKE_PRECIS: Not used by font mapper, but as a return value when enumerating raster fonts, vector fonts or TrueType fonts. In Windows environment, to ensure compatibility, this value is always returned when listing fonts.

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 left or right.

If this value is not used, the device fonts always rotate counterclockwise, but the rotation of other fonts depends on the direction of the coordinate system. For more information about coordinate system orientation, see Parameter Orientation.

CLIP_TT_ALWAYS: not used.

FdwQuality: refers to the output quality, which defines how GDI carefully matches the logical font attributes with the actual physical font attributes. It can take one of the following values:

DEFAULT_QUALITY: The appearance of the font is not important.

DRAFT_QUALITY: Font appearance is second only to PROOF_QUALITY. For GDI raster fonts, the scaling is active, which means there are many font sizes to choose from, but the quality may not be high. If necessary, bold, italic, underline and strikethrough fonts can be used together.

PROOF_QUALITY: Character quality is more important than matching the font attributes of logical fonts exactly. For GDI scanning fonts, scaling is active and the closest size is selected. Although the selected font sizes are not completely matched when using PROOF_QUALITY, the quality of the font is high and there is no distortion in appearance. If necessary, bold, italic, underline and strikethrough fonts can be used together.

FdwPitchAndFamily: specifies the font spacing and font family. The following two digits specify the character spacing of the font, which can be one of the following values:

Default _ pitch; Fixed _ spacing; Variable pitch

The high-end four-digit number specifies the font family, which can take one of the following values:

FF_DECORATIVE: The font is novel, such as Old English. FF_DONTCARE: Don't care or don't know.

FF_MDERN: Font with fixed stroke width, with or without serif. Such as Pica, Elite and Courier New.

Variable stroke width and serif fonts. Such as MS Serif.

FF_SCRIPT: A font designed to look like handwriting. Such as handwriting and cursive script.

FF_SWISS: a font with variable stroke width and no diagonal lines. Such as MS Sans Serif.