Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing this application and in order to ensure content is adjusted to page size, a the application uses the current font size and the value that GetTextExtentPoint32 returned to adjust the page size by feeding the out come into a while loop. I was rather schocked to discover that GetExtentPoint32 keep returning the same value inspite of decrrase of font-size. Why is that so, how can I solve this challenge.

The relevant codes are shown below:

GetTextExtentPoint32(hdc, wstSchoolName11.c_str(), lstrlen(wstSchoolName11.c_str()), &size);
if ((iTextLeft + > (iPaperWidth - iPictureWidth - iOffsetX))

		hNewFont = CreateAppFont(iFontSize, FW_BOLD);
		hOldFont = (HFONT)SelectObject(hdc, hNewFont);
		GetTextMetrics(hdc, &tm);
		//int iTextTop11 = iTop + iOffsetY;
		//int iTextLeft11 = iImageLeft + iPictureWidth + 5 * dOneMMX;
		wstring wstSchoolName11 = utf8_decode(stSchoolName);
		//SIZE size11;
		GetTextExtentPoint32(hdc, wstSchoolName11.c_str(), lstrlen(wstSchoolName11.c_str()), &size);
		bMaximumWith = true;

int iWindowExtX = 1000 * GetDeviceCaps(hDC,HORZSIZE);
    int iWindowExtY = 1000 * GetDeviceCaps(hDC, VERTSIZE);

    int iViewPortX = GetDeviceCaps(hDC, HORZRES);
    int iViewPortY = GetDeviceCaps(hDC, VERTRES);

    SetMapMode(hDC, MM_ISOTROPIC);
    SetWindowExtEx(hDC, iWindowExtX, iWindowExtY, nullptr);
    SetViewportExtEx(hDC, iViewPortX, iViewPortY, nullptr);

The definition of CreateFont app is as shown below. Is there anything wrong with it?

HFONT CreateAppFont(int cHeight, int cWeight, DWORD bItalic = 0, int cEscapement = 0, DWORD bUnderline = 0, LPCWSTR pszFaceName = L"Times New Roman");

HFONT CreateAppFont(int cHeight, int cWeight, DWORD bItalic, int cEscapement, DWORD bUnderline, LPCWSTR pszFaceName)
	return CreateFont(cHeight, 0, cEscapement, cEscapement, cWeight, bItalic, bUnderline, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,


What I have tried:

I have done have spent reasonable time debugging. The outcome neccitated this post.
Updated 17-Jan-24 10:43am

1 solution

The GetTextExtentPoint32A function (wingdi.h) - Win32 apps | Microsoft Learn[^] returns the size of the text in logical units. The text display functions use that number multiplied by the font size to calculate the actual space required for the display.

Well I have just tested this and the size values do reflect the font in use. I think you need to look more closely at what is going on in your code, with specific reference to the CreateAppFont function.
Share this answer
Gbenbam 17-Jan-24 16:46pm    
I have added the definition of CreateAppFont. Do you mind looking at it? Can, I see youre version of the code that worked?
Richard MacCutchan 18-Jan-24 4:51am    
I have used your code in my test and it works the same. The size values increase as the size of the selected font increases. You need to show the actual values that are returned in the size varaible when you run your code.
Gbenbam 18-Jan-24 11:08am    
The value returned by size regardless of how many times iFontSize was decremented was cx = 5988 and
cy = 479. What do you think?
Richard MacCutchan 18-Jan-24 12:04pm    
I think there is something in your code that you have not shown that is affecting what is happening. Or possibly the if statement in the second line of your first code sample never evaluates to TRUE.
Gbenbam 18-Jan-24 12:06pm    
You said that the size value increases as the font increases. By that i you mean he fontHeight parameter use to create the font? If , yes, what happens if the fontHeight paramter decreases which is the obective in my own peculiar situation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900