Click here to Skip to main content
14,454,504 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello, guys
When selecting a font in applications such as notepad, we select fontsize. However When we use chossefont to select a font, we select only a font width and height. My question is this: How to transform font size to font width and font size as notepad does.
Rate this:
Please Sign up or sign in to vote.

Solution 1

I never really "care" about the font width in the application I've been working on, most of the time the user will only see the font point size (e.g. CFontDialog).

When working with the LOGFONT structure, you need to convert the point size to the font height (Calculating The Logical Height and Point Size of a Font[^] )

With something like (from point size to font height):

CDC* cdc = GetDC();
HDC hdc = cdc->GetSafeHdc();
int pointPerInch = 72;
int deviceCaps = GetDeviceCaps( hdc, LOGPIXELSY );
// Generate a LOGFONT containing all the font properties.
LOGFONT logFont;
strcpy( logFont.lfFaceName, fontProperties.m_FontName );
logFont.lfHeight = -MulDiv(fontProperties.m_FontSize, deviceCaps, pointPerInch);
logFont.lfWidth = 0; // keep zero.
Rate this:
Please Sign up or sign in to vote.

Solution 2

They are all part of the same metrics. Font size is given as either the height (in points) of the bounding rectangle of each character in the font, or the maximum height of each character when parts of the character can extend above or below this rectangle; or the width, or some combination of features. The width of each character in the font will be proportional to the height as set by the font designer. In fixed width fonts each character has the same width, in proportional fonts the width is as much as necessary to display the character (cf. 'i' and 'w'). Once you have selected a font into a device context you can get the text metrics which includes height, width etc.

The following code gives a sample:
HFONT		hFont, hOldFont;

// get a 10-point font and select it into the DC
currentY = MulDiv(10, GetDeviceCaps(hDC, LOGPIXELSY), 72);
hFont = CreateFont(-currentY, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0, L"Courier New");
hOldFont = SelectFont(hDC, hFont);

GetTextMetrics(hDC, &textMetric);

Take a look at the MSDN pages[^] for each of the above functions for more detail.
Rate this:
Please Sign up or sign in to vote.

Solution 3

"Size" is commonly referred to the height (or to the "cell height", see here[^]), letting the width chosen proportionally.

It essentially derives from the fact that words are written horizontally, so in a line "same height" (or different height) is a more important concept than "same width".

In don't know if the same applies (with the due "transforantions") to vertical scripting (like some asian alphabets) as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100