GetTextMetrics为Courier New返回不同的最大和平均字符宽度 [英] GetTextMetrics returning different maximum and average character width for Courier New

查看:94
本文介绍了GetTextMetrics为Courier New返回不同的最大和平均字符宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows 7上运行的Borland C ++ Builder 6维护应用程序.

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

应用程序错误地使用Courier New字体绘制了文本,因为每个字母都被略微切断了.问题在于调用方法GetTextMetrics时,因为它使用不同的tmAveCharWidth和tmMaxCharWidth值填充TEXTMETICS结构.然后,应用程序使用tmAveCharWidth来计算字符宽度,这是错误的,因为该值可能小于tmMaxCharWidth.我将解决的问题.

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.

我很好奇,为什么GetTextMetrics为Courier New返回不同的tmAveCharWidth和tmMaxCharWidth值?我的理解是,Courier New是等宽字体,并且tmAveCharWidth和tmMaxCharWidth应该相同.我用其他等宽字体测试了这个假设是正确的.

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 

这是我选择12号Courier New时正在运行的代码.

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

传递给CreateFontIndirect的参数

从GetTextMetrics返回的TEXTMETRICS结构

推荐答案

我发现这确实是ClearType在工作(感谢Deanna).关闭ClearType可以在不更改任何代码的情况下解决显示问题,尽管我仍然需要纠正应用程序与ClearType一起工作的方式.

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.

我还发现Windows XP上不存在该问题,因为ClearType默认情况下处于关闭状态,而在Windows 7(和Vista)中默认情况下处于打开状态.

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.

这篇关于GetTextMetrics为Courier New返回不同的最大和平均字符宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆