GetTextMetrics returning different maximum and average character width for Courier New

橙三吉。 提交于 2020-12-26 10:25:42

问题


I'm maintaining an application using Borland C++ Builder 6 running on Windows 7.

The application is incorrectly drawing text using font Courier New because each letter is being slighty cut off. The issue is when calling the method GetTextMetrics because it is filling the TEXTMETICS struct with differing tmAveCharWidth and tmMaxCharWidth values. The application then uses tmAveCharWidth to calculate character width with is wrong because that value it can be less than tmMaxCharWidth. That issue I will be fixing.

I courious why GetTextMetrics is returning differing tmAveCharWidth and tmMaxCharWidth values for Courier New? My understanding was that Courier New is a monospaced font and that tmAveCharWidth and tmMaxCharWidth should be the same. I tested with other monospaced fonts that that assumption is correct.

This is the section of code with the issue:

hFont = CreateFontIndirect(&lpInstData->lf);

hDC = GetDC(hWnd);
hFontOld = SelectObject(hDC, hFont);


GetTextMetrics(hDC, &tm);
lpInstData->nCharHeight = tm.tmHeight;
lpInstData->nCharWidth = tm.tmAveCharWidth; <--- Should be using tmMaxCharWidth 

Here is the code running when I selected size 12 Courier New.

Parameter passed to CreateFontIndirect

TEXTMETRICS structure returned from GetTextMetrics


回答1:


I found this was indeed ClearType at work (thanks Deanna). Turning off ClearType corrects the display issue without changing any code, although I still need to correct how the application works with ClearType.

I also found the issue was not present on Windows XP because ClearType is turned off by default, whereas in Windows 7 (and Vista) it is turned on by default.



来源:https://stackoverflow.com/questions/10279688/gettextmetrics-returning-different-maximum-and-average-character-width-for-couri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!