VB6 中的字体居中
如何确定 Arial Bold 字体文本字符串的长度,然后在 VB6 中将其居中?
我们没有使用“标签”或“图片框”将文本打印到屏幕上。 我们正在动态调整文本大小,并允许用户根据自己的喜好缩放应用程序的大小。 我们使用代码将文本写入屏幕。
How do you determine the length of a string of text in Arial Bold font, and then center it in VB6?
We are not using a "label" or "picture box" to print the text to the screen. We are sizing the text on the fly, and allowing the user to scale the size of our application to their liking. We write the text to screen using code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种方法是拥有一个隐藏的图片框,并按照您想要的方式设置该图片框的字体规格。
然后使用 PictureBox 的 TextHeight 和 TextWidth 方法进行测量。 单位将采用图片框设置的任何比例模式。
如果您直接打印到打印机或表格,则只需先设置字体,然后进行测量。
使其居中
将显示区域替换为您正在使用的任何对象。
根据您更新的答案,请注意隐藏的图片框建议不用于打印。 它只是获得文本测量。 但是,您直接打印到表单,因此您只需要使用上面的代码示例。
One way is to have a hidden picture box and setup the font specs of that picture box the way you want.
Then use the TextHeight and TextWidth methods of the PictureBox to take your measurements. The Units will be in whatever scalemode the Picture Box is set to.
If you are printing directly to the printer or form then just set your font FIRST then take your measurements.
To center it
Substitute displayarea with whatever object you are using.
Based on your updated answer note that the hidden picture box suggestion isn't used to print. It is only get text measurement. However you are printing directly to the form so you just need to use the code example above.
我不记得具体细节了(距离我上次使用 VB 6 已经过去了大约 3 年),但是 Form 上有一个名为“
MeasureString
”的方法。 它获取字符串,并根据表单的字体设置对其进行测量。另外,这是 Jason Lepack 发表的评论,以防我误解您的要求并使您的要求过于复杂:
“标签通常有一个对齐属性。如果将其设置为居中对齐,那么无论字体如何,它都应该在标签中居中”。
I can't remember the specifics (it's been about 3 years since I last used VB 6), but there's a method on Form called something like "
MeasureString
". It takes the string, and measures it according to the font settings of the form.Also, here's a comment posted by Jason Lepack in case I've misunderstood and over-complicated your requirements:
"Labels usually have an alignment property. If you set it to align to center then, regardless of the font face it should center in the label".
您可以调用 Win32 GDI 函数:例如,请参阅 http://msdn.microsoft.com/en-us/library/ms534223(VS.85).aspx
There are Win32 GDI functions you can invoke: see for example GetTextExtentPoint32 at http://msdn.microsoft.com/en-us/library/ms534223(VS.85).aspx
最好的选择可能是 Form.TextWidth,它似乎返回字符串的宽度(以缇为单位)。 我刚刚采用这种方法是为了根据需要出现在按钮内部的标签的长度来动态调整按钮的大小。
还有一个名为 Form.TextHeight 的相应函数,它允许您在垂直维度上执行相同的操作。
确保将窗体的 Font 属性设置为与要测量其文本的控件的 Font 属性相匹配,否则您将得到不正确的结果。
欲了解更多信息,请访问 http://msdn.microsoft.com/ en-us/library/aa267168(VS.60).aspx
Your best option may be Form.TextWidth, which appears to return the width of a string in twips. I've just taken this approach in order to dynamically size a button based on the length of the label that needs to appear inside it.
There is also a corresponding function called Form.TextHeight which would allow you to do the same thing in the vertical dimension.
Make sure that you set the Font property of the form to match the Font property of the control you're intending to measure the text for, otherwise you'll get incorrect results.
Read more at http://msdn.microsoft.com/en-us/library/aa267168(VS.60).aspx