locked
About CreateFont RRS feed

  • Question

  • My question is about CreateFont first parameter "nHeight", how/what value should i set in here so that it will "scale" properly on various

    DPI settings and monitors?

    I've read MSDN about CreateFont and it states that:

    "For the MM_TEXT mapping mode, you can use the following formula to specify a height for a font with a specified point size:

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

    "

    But i don't understand, where this MM_TEXT value should be set? if this is related to what i want (about "proper scale").

    Thanks for your time.


    EDIT: and from where did they get this 72 "magic number"?
    Tuesday, March 22, 2011 6:40 PM

Answers

  • Your current DPI is mixed in through GetDeviceCaps(hDC, LOGPIXELSY).  This is the number of pixels per inch vertically.  You are scaling the point size as 72 up to your current DPI using this line.

    nHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    • Marked as answer by borko1980 Tuesday, March 22, 2011 8:30 PM
    Tuesday, March 22, 2011 8:20 PM
  • It would have been a bit clearer if they wrote

    const int POINTS_PER_INCH=72;

    then used POINTS_PER_INCH instead of the 72.

    However, 72 points per inch is a constant that will not change with DPI.  That is part of the definition of a point, that 72 point text should be one inch tall.  Now, of course you would need to factor in the DPI of your output device to determine how many PIXELS tall that text should be.  Which is what the formula is doing.

    • Marked as answer by borko1980 Tuesday, March 22, 2011 8:30 PM
    Tuesday, March 22, 2011 8:21 PM

All replies

  • MM_TEXT is the default mapping mode of a DC.  You can change It with SetMapMode.  MM_TEXT means essentially that the values you pass into DC functions correspond with pixels.  I am not a high-DPI expert, so I am not sure what effect this has.  I suspect you may not want to use MM_TEXT for a DPI aware app.

    The magic number 72 represents points.  There are 72 points per inch.  Basically the code is getting the number of pixels per inch and then dividing by 72 to get points.

    Tuesday, March 22, 2011 7:27 PM
  • I am confused as to why this value/constant (72) is set instead putting current value of dpi (ppi) from monitor.

    For example on my old monitor default value was 96 and on this is 120.

    Now i don't know what to put in first parameter of CreateFont function.


    Tuesday, March 22, 2011 7:54 PM
  • Your current DPI is mixed in through GetDeviceCaps(hDC, LOGPIXELSY).  This is the number of pixels per inch vertically.  You are scaling the point size as 72 up to your current DPI using this line.

    nHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    • Marked as answer by borko1980 Tuesday, March 22, 2011 8:30 PM
    Tuesday, March 22, 2011 8:20 PM
  • It would have been a bit clearer if they wrote

    const int POINTS_PER_INCH=72;

    then used POINTS_PER_INCH instead of the 72.

    However, 72 points per inch is a constant that will not change with DPI.  That is part of the definition of a point, that 72 point text should be one inch tall.  Now, of course you would need to factor in the DPI of your output device to determine how many PIXELS tall that text should be.  Which is what the formula is doing.

    • Marked as answer by borko1980 Tuesday, March 22, 2011 8:30 PM
    Tuesday, March 22, 2011 8:21 PM