测量不使用图形对象的String? [英] Measure a String without using a Graphics object?
问题描述
我使用像素为单位对我的字体。在一个地方,我执行的命中测试,以检查用户是否点击屏幕上的一些文字的矩形边框中。我需要使用类似 MeasureString
这一点。不幸的是,code做点击测试是一个图书馆深处不具有访问图形
对象甚至是控制
。
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
.
我如何获得给定的字体,而无需使用图形
类的字符串的边框?为什么我甚至需要一个图形
对象时,我的字体是像素?
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
这篇关于测量不使用图形对象的String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!