MeasureString()填充左侧和右侧的文本 [英] MeasureString() pads the text on the left and the right

查看:115
本文介绍了MeasureString()填充左侧和右侧的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中使用GDI +. (此问题也可能在C#中存在).

I'm using GDI+ in C++. (This issue might exist in C# too).

我注意到,每当我调用Graphics :: MeasureString()或Graphics :: DrawString()时,该字符串就会在左右两侧填充空白.

I notice that whenever I call Graphics::MeasureString() or Graphics::DrawString(), the string is padded with blank space on the left and right.

例如,如果我使用的是Courier字体(不是斜体!),并且我测得"P",我得到90,但是"PP"给我150.我希望等宽字体的宽度恰好是"PP".

For example, if I am using a Courier font, (not italic!) and I measure "P" I get 90, but "PP" gives me 150. I would expect a monospace font to give exactly double the width for "PP".

我的问题是:这是预期的还是已记录的行为,如何禁用此行为?

My question is: is this intended or documented behaviour, and how do I disable this?

RectF Rect(0,0,32767,32767);
RectF Bounds1, Bounds2;
graphics->MeasureString(L"PP", 1, font, Rect, &Bounds1);
graphics->MeasureString(L"PP", 2, font, Rect, &Bounds2);
margin = Bounds1.Width * 2 - Bounds2.Width;

推荐答案

这是设计使然,该方法不使用实际的字形来度量宽度,因此在悬垂的情况下增加了一些填充.

It's by design, that method doesn't use the actual glyphs to measure the width and so adds a little padding in the case of overhangs.

MSDN 建议使用其他方法,如果您需要更高的准确性:

MSDN suggests using a different method if you need more accuracy:

要获取适合布局中相邻字符串的度量标准(例如,在实现格式化文本时),请使用 GenericTypographic .另外,请确保 TextRenderingHint 用于图形

To obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.

这篇关于MeasureString()填充左侧和右侧的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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