NO

Author Topic: Font size - Newbie  (Read 2515 times)

Offline DonnyDave

  • Member
  • *
  • Posts: 8
Font size - Newbie
« on: March 18, 2021, 12:43:05 PM »
Hi
I've searched Google and Charles Petzold but still can't
find out how to print different size text.

example:-

  . . . . . . . . . .
       case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;

            TextOutA(hdc, 10, 100, TEXT("This is small text"), 18);      // Write a line of text to the client area.
      //  Do something - (Change font size).
            TextOutA(hdc, 10, 150, TEXT("This is medium text"), 19);  // Write a line of text to the client area.
      //  Do something - (Change font size).
            TextOutA(hdc, 10, 200, TEXT("This is large text"), 18);      // Write a line of text to the client area.

          EndPaint (hwnd, &ps) ;
       return 0 ;
  . . . . . . . . . .


I'm a newbie so am obviously missing something.
Any help would be much appreciated.

 Donny Dave
« Last Edit: March 18, 2021, 12:46:21 PM by DonnyDave »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Font size - Newbie
« Reply #1 on: March 18, 2021, 01:30:40 PM »
You should use CreateFont() to create font objects having the dimensions you want, then select the required one in the device context before to output text.
See the examples.
The Ms example is wrong. Doesn't delete the old font object returned by the SelectObject() call, and doesn't save and then restores the original font.
The code below is the correct one:
Code: [Select]
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{

case WM_PAINT:
{
RECT rect;
HBRUSH hBrush;
HFONT hFont, hFontOld;
hdc = BeginPaint(hWnd, &ps);

//Logical units are device dependent pixels, so this will create a handle to a logical font that is 48 pixels in height.
//The width, when set to 0, will cause the font mapper to choose the closest matching value.
//The font face name will be Impact.
hFont = CreateFont(48, 0, 0, 0, FW_DONTCARE, FALSE, TRUE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT("Impact"));
hFontOld = SelectObject(hdc, hFont);

//Sets the coordinates for the rectangle in which the text is to be formatted.
SetRect(&rect, 100, 100, 700, 200);
SetTextColor(hdc, RGB(255, 0, 0));
DrawText(hdc, TEXT("Drawing Text with Impact"), -1, &rect, DT_NOCLIP);

//Logical units are device dependent pixels, so this will create a handle to a logical font that is 36 pixels in height.
//The width, when set to 20, will cause the font mapper to choose a font which, in this case, is stretched.
//The font face name will be Times New Roman.  This time nEscapement is at -300 tenths of a degree (-30 degrees)
hFont = CreateFont(36, 20, -300, 0, FW_DONTCARE, FALSE, TRUE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT("Times New Roman"));
DeleteObject(SelectObject(hdc, hFont));

//Sets the coordinates for the rectangle in which the text is to be formatted.
SetRect(&rect, 100, 200, 900, 800);
SetTextColor(hdc, RGB(0, 128, 0));
DrawText(hdc, TEXT("Drawing Text with Times New Roman"), -1, &rect, DT_NOCLIP);

//Logical units are device dependent pixels, so this will create a handle to a logical font that is 36 pixels in height.
//The width, when set to 10, will cause the font mapper to choose a font which, in this case, is compressed.
//The font face name will be Arial. This time nEscapement is at 250 tenths of a degree (25 degrees)
hFont = CreateFont(36, 10, 250, 0, FW_DONTCARE, FALSE, TRUE, FALSE, DEFAULT_CHARSET,
OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, VARIABLE_PITCH, TEXT("Arial"));
DeleteObject(SelectObject(hdc, hFont));

//Sets the coordinates for the rectangle in which the text is to be formatted.
SetRect(&rect, 500, 200, 1400, 600);
SetTextColor(hdc, RGB(0, 0, 255));
DrawText(hdc, TEXT("Drawing Text with Arial"), -1, &rect, DT_NOCLIP);
DeleteObject(SelectObject(hdc, hFontOld));

EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
« Last Edit: March 18, 2021, 01:44:12 PM by frankie »
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Grincheux

  • Guest
Re: Font size - Newbie
« Reply #2 on: March 18, 2021, 08:55:44 PM »

Offline DonnyDave

  • Member
  • *
  • Posts: 8
Re: Font size - Newbie
« Reply #3 on: March 19, 2021, 05:10:01 PM »
Thanks both,
 That does exactly what I wanted.
 I'll also probably use GetTextExtentPoint32A in a later project

Dave

Offline John Z

  • Member
  • *
  • Posts: 790
Re: Font size - Newbie
« Reply #4 on: March 20, 2021, 10:16:39 AM »
Hi Dave,

I'll also probably use GetTextExtentPoint32A in a later project

You'll probably need to use GetTextExtentPoint32A sooner rather than later if you are printing multiple lines.  If you want to output text on several iines with the correct spacing between lines, and maybe accommodate different font sizes.  After setting the font to the printer use something like:
Code: [Select]
// need to get vertical sizing after setting font
SIZE size;
  GetTextExtentPoint32(gHDCPrinter, L"Qwerty", 7, &size);
Height = size.cy;
Then by counting lines you can set the printer Y position easily as
Code: [Select]
TextOut(gHDCPrinter, x, y+abs(Height)*k,p_ltemp,wcslen(p_ltemp)); Where X=right page margin, Y=top page margin, abs(Height) is the vertical spacing needed for the selected font and k is the line number.
You would also need to use this number to determine when to start a new page  :)

Further, if you would do multiple prints on the same line you would use GetTextExtentPoint32 to calculate where the next print would start based on what has already been printed on the line.

Regards,
John

Grincheux

  • Guest
Re: Font size - Newbie
« Reply #5 on: March 20, 2021, 06:06:04 PM »
Thank You John.
I was not brave enough to give examples.  >:(