在不使用 Graphics 对象的情况下测量字符串? [英] Measure a String without using a Graphics object?

查看:24
本文介绍了在不使用 Graphics 对象的情况下测量字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用像素作为字体单位.在一个地方,我正在执行命中测试以检查用户是否在屏幕上某些文本的边界矩形内进行了单击.我需要为此使用 MeasureString 之类的东西.不幸的是,执行命中测试的代码位于库的深处,该库无法访问 Graphics 对象,甚至无法访问 Control.

I am using pixels as the unit for my font. In one place, I am performing a hit test to check if the user has clicked within the bounding rectangle of some text on screen. I need to use something like MeasureString for this. Unfortunately, the code doing the hit test is deep within a library which does not have access to a Graphics object or even a Control.

如何在不使用 Graphics 类的情况下获取给定字体的字符串的边界框?当我的字体以像素为单位时,为什么我什至需要一个 Graphics 对象?

How do I get the bounding box of a string given the font without using the Graphics class? Why do I even need a Graphics object when my font is in pixels?

推荐答案

如果您有对 System.Windows.Forms 的引用,请尝试使用 TextRenderer 类.有一个静态方法 (MeasureText),它接受字符串和字体并返回大小.MSDN 链接

If you have a reference to System.Windows.Forms, try using the TextRenderer class. There is a static method (MeasureText) which takes the string and font and returns the size. MSDN Link

这篇关于在不使用 Graphics 对象的情况下测量字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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