• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

Urgent: Using DrawText() to send text to printer. Font size appears strange.

I am using Win 95, and I have set font size to very large (200%, using Display Properties dialog and choose Customize). With size 8 specify for text to draw on screen, the text appears large, which is expected. However, when printing to the printer (I assume that printer knows nothing about how the font size is set for the screen), the text also appears large, which I think is not correct.

The details following will explain what I have done to print the text to printer. I appreciate any help.

After specifying size 8 for hPrinterDC, as in following:
HGDIOBJ saveFont = SelectObject(hPrinterDC,
                   //font contains the font size
and looking at the return value from DrawText, I realize that textheight is 13, as in the following code:
int textheight = DrawText(hPrinterDC, str, length, &lpDCRect, just);

Please explain to me what is the idea behind this? In other words, how is this return value related to the value that we specify (size 8). Thanks.

  • 3
  • 3
  • 2
1 Solution
Please give the DPI of printerDC
ThuyAuthor Commented:
Thanks for your response. I will go get the DPI for the printer, but could you please explain to me how this helps since the problem occurs despite of any printer I am using? Thanks.

The return value "textheight" depend on DC's DPI and the font size.So you should tell me how you create your font?
"size 8" is logical size or physical size?
Get me the snippet.
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

what is "font" ?
Assuming this is MFC and a CFont instance.
How did you create the font ?
Could you post the code?
ThuyAuthor Commented:
BeyondWu and Pacman,
Thanks for your responses.

font is created using CreateFont as in following:
          0,//Best fit width
          0,//No escapement
          0,//No Orientation
          weight ,                                    italicStyle,
          FALSE, //No underline
          FALSE, //No strikeout
          OUT_DEFAULT_PRECIS,                         CLIP_DEFAULT_PRECIS,

So, 8 is point size (I would say logical).

Also, GetDeviceCaps(hDC, LOGPIXELSX) and GetDeviceCaps(hDC, LOGPIXELSY) both return 600 pixels per (logical) inch.

I appreciate any explanation, or correction, etc.

First of all, maybe you make some mistake in your code.
Because for a 8 point size font and a 600 DPI DC, you can't get the textheight 13. It's must be 96 DPI. It's means maybe you confuse the printerDC with ScreenDC.

Here is the formula:
FontHeight = (Point size of font)*DPI/72;
StringHeight = FontHeight + tmInternalLeading;
you can use,
TEXTMETRIC txtmtric;
GetTextMetrics(pDC->m_hDC, &txtmtric);
to get the txtmtric.tmInternalLeading;

Search "String Widths and Heights" in MSDN.

So for your case:
the textheight wich drawtext will return is:
textheight = 8*600/72+txtmtric.tmInternalLeading;
           = 67 + 9 = 76;
If for 96 DPI;
textheight = 8*96/72+txtmtric.tmInternalLeading;
           = 11 + 2 = 13;
Good Luck.
instead of CreateFont() you can use CreatePointFont().
Then you don't have to care about formulas.
You've only to pass the size of the font in 1/10 points.
for example 240 means 24 point. Further you pass the DC and let the function do the math.
ThuyAuthor Commented:
BeyondWu and Pacman,
Thanks for all of your answers. They all clarify the questions I have.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now