Font size in points..

Hi all,
I am a newbie and code in Borland C(++)..
When I want to set the font in an edit box I crate a font using CreateFont API.
But how can I exactly specify the size in points?(no user input)
The DOS fonts have different sizes in different versions of Windows..
regards
and thanks in advance.

Comments

  • Since I am in electronic forms industry and I had a lot of fonts to set...
    The font size units (and not only that) are chosen, because when we talk about text (drawn with a font, of course) we want to be able to read the text on screen AND on printer or any other device. That is why the 'points' - point is 1/72 of an inch, so it is independent ('paper') unit which allows to get on the printer what you can see on the screen. If the pixels were set as units here - then we say have a font with 20 pixels high - then on a printer with resolution 600 DPI (Dots (pixels) Per Inch) we would not be able to see that text as we see on screen. Anyhow, here is formula to create a font:
    [code]
    LOGFONT lf;
    int iptSize = 12; // 12 points height

    memset (&lf, 0, sizeof (lf)); // Set all fields to zero
    lstrcpy (lf.lfFaceName, "Tahoma");
    lf.lfHeight = -MulDiv (iptSize, GetDeviceCaps (hDC, LOGPIXELSY), 72);
    lf.lfWeight = FW_NORMAL; // Can be FW_BOLD
    hFont = CreateFontIndirect (&lf);
    [/code]
    The key point here is the 'hDC' - it supposed to be the HDC of a device where you are going to be drawing your text - screen DC or Printer DC. For your purpose now - the font is in edit box - you need screen DC:
    [code]
    HDC hScreenDC = GetDC (NULL);

    // ... create an HFONT for 'hScreenDC' as you see above...

    ReleaseDC (NULL, hScreenDC); // do not forget it!!!
    [/code]
    Now after you used the HFONT you need to call DeleteObject() for it at the end of the program or scope - the edit box will not do it by default.

    For more info on Win32 API go to:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/win32/catfunc_26m1.asp

    Cheers!

  • Tks a lot..
    AsmGuru...
    That was COOL :)

    : Since I am in electronic forms industry and I had a lot of fonts to set...
    : The font size units (and not only that) are chosen, because when we talk about text (drawn with a font, of course) we want to be able to read the text on screen AND on printer or any other device. That is why the 'points' - point is 1/72 of an inch, so it is independent ('paper') unit which allows to get on the printer what you can see on the screen. If the pixels were set as units here - then we say have a font with 20 pixels high - then on a printer with resolution 600 DPI (Dots (pixels) Per Inch) we would not be able to see that text as we see on screen. Anyhow, here is formula to create a font:
    : [code]
    : LOGFONT lf;
    : int iptSize = 12; // 12 points height
    :
    : memset (&lf, 0, sizeof (lf)); // Set all fields to zero
    : lstrcpy (lf.lfFaceName, "Tahoma");
    : lf.lfHeight = -MulDiv (iptSize, GetDeviceCaps (hDC, LOGPIXELSY), 72);
    : lf.lfWeight = FW_NORMAL; // Can be FW_BOLD
    : hFont = CreateFontIndirect (&lf);
    : [/code]
    : The key point here is the 'hDC' - it supposed to be the HDC of a device where you are going to be drawing your text - screen DC or Printer DC. For your purpose now - the font is in edit box - you need screen DC:
    : [code]
    : HDC hScreenDC = GetDC (NULL);
    :
    : // ... create an HFONT for 'hScreenDC' as you see above...
    :
    : ReleaseDC (NULL, hScreenDC); // do not forget it!!!
    : [/code]
    : Now after you used the HFONT you need to call DeleteObject() for it at the end of the program or scope - the edit box will not do it by default.
    :
    : For more info on Win32 API go to:
    :
    : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/win32/catfunc_26m1.asp
    :
    : Cheers!
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories