Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Font size to pixels

This thread is locked; no one can reply to it. rss feed Print
Font size to pixels
Billybob
Member #3,136
January 2003

This one should be easy, but of course it isn't :(
I need to convert from font size to pixels. I'm making an MFC application.
I got this information from Googling:
There are 72 points in an inch. GetDeviceCaps(dc, LOGPIXELSY) will tell me the number of pixel in a logical inch. Combining the two I should be able to take the fontsize, multiply by GetDeviceCaps(dc, LOGPIXELSY) and divide by 72 to get the pixel height.
Doesn't work :(
The font size on my app is 18, that should be 1/4 of an inch. It's more like half an inch or more.
So, I'm assuming font size != points here.
Any help?

ReyBrujo
Moderator
January 2001
avatar

Quote:

There are 72 points in an inch.

The problem is here I think. Are you sure about this?

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

DmAndrew12
Member #1,950
February 2002
avatar

If I recall, font size measures the height of the font, not the maximum width of a character in the font. I don't think there really is a way to get pixel size from the font size, as TrueType fonts are comprised of vectors.

Gideon Weems
Member #3,925
October 2003

72 pixels/inch is a common resolution for bitmaps, is it not? I think your info equates "point" with "pixel," and assuming that 72 pixels equal an inch is obviously not wise.

So, why not...

// pixel_height_of_font / pixels_per_inch
int nHeight = text_height(font) / GetDeviceCaps(dc, LOGPIXELSY);

Billybob
Member #3,136
January 2003

Quote:

If I recall, font size measures the height of the font, not the maximum width of a character in the font.

I need height, not width.

Gideon: MFC.

And on another note, does anyone know how to set the color of a dialog in MFC?

X-G
Member #856
December 2000
avatar

Quote:

The font size on my app is 18, that should be 1/4 of an inch. It's more like half an inch or more.

Are you measuring this on a monitor? If so, don't even expect it to be appropriate. You can't just say that "1 unit = 72 pixels" when monitors are concerned; resolution and monitor size completely destroy that. As for LOGPIXELSY... I'm not sure. There might be more shenanigans involved if you want to do that conversion, and I'm not even sure you CAN get it accurate. Monitors just weren't designed to work that way. As for printing, it should be accurate if your printer is printing at 72 dpi (that is, draw the font 72 points high and they should come out one inch high).

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Oscar Giner
Member #2,207
April 2002
avatar

Quote:

And on another note, does anyone know how to set the color of a dialog in MFC?

It's certainly [edit]not[/edit] a very easy thing, but here is it.

Using the class wizard add a handler to the message WM_CTLCOLOR for your dialog. The definition of the handler will be HBRUSH CYouDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor).

This function is not only called for the dialog, but for all objects it contains, so if you want to only set the color of the dialog, you have to check the nCtlColor parameter (it's value is CTLCOLOR_DLG when it's a dialog box), or just pWnd that contains the window class.

Now, to the code:

HBRUSH CYourDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

  if (nCtlColor == CTLCOLOR_DLG)  // or  if (pWnd->GetDlgCtrlID() == IDD_DIALOG_ID)  // (IDD_DIALOG_ID is the id of your dialog)
  {
    return (HBRUSH)m_pen_dlg.GetSafeHandle();
  }

  return hbr;
}

m_pen_dlg is a member variable with type CBrush. You initialize it in the OnInitDialog, for example:

COLORREF color;
color = RGB(190, 210, 210);
m_pen_dlg.CreateSolidBrush(color);

[edit]
This is for the case where the dialog will be always of the same color. You can obviouslly have CBrush uninitialized but it must be a member variable (if it's a local variable it'd get destroyed after the return ;)) and do the creation each time in OnCtlColor.
[/edit]

Suggestion: buy a book about MFC. It's not the type of library you can learn by yourself :) It has lots of little things you'll never know about / take you too much time to get to work :)

[edit]
Warning: in case you want to change the background color of other objects. The CDC pointer you get (pDC) has a member function SetBkColor(COLORREF). For some controls you must also call this function (for example, for listboxes, IIRC, the brush you return only affects the background of the list, but not the background of the elements in the list (which are affected by this SetBkColor)). SetBkColor doesn't affect dialog boxes. IIRC (again :P) SetBkColor affect the background of text.

Matt Smith
Member #783
November 2000

The point sizes in fonts are in 1/72". This is an old printer's term from the letterpress days. The actual height of a capital letter is often much smaller than the pt size would suggest, so you can't predict exactly, unless you specify the font and make adjustments.

Go to: