vb.net 检测vb.net中文本的宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5245845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect width of text in vb.net
提问by Urbycoz
Is there a way to detect the actual width of text in a vb.net web app? It needs to be dependant upon its font-style and size.
有没有办法检测 vb.net 网络应用程序中文本的实际宽度?它需要依赖于它的字体样式和大小。
In vb6 you could copy the text into a label and make it expand to fit then measure its width, but this won't work in vb.net.
在 vb6 中,您可以将文本复制到标签中并使其展开以适应然后测量其宽度,但这在 vb.net 中不起作用。
回答by Mitch Wheat
Update: On further inspection, TextRenderer.MeasureText
seems a better option:
更新:进一步检查, TextRenderer.MeasureText
似乎是一个更好的选择:
Dim text1 As String = "Measure this text"
Dim arialBold As New Font("Arial", 12.0F)
Dim textSize As Size = TextRenderer.MeasureText(text1, arialBold)
Measures the specified string when drawn with the specified Font.
使用指定的 Font 绘制时测量指定的字符串。
Dim myFontBold As New Font("Microsoft Sans Serif", 10, FontStyle.Bold)
Dim StringSize As New SizeF
StringSize = e.Graphics.MeasureString("How wide is this string?", myFontBold)
回答by Urbycoz
I wrote this low-end function to do just that without higher-level API's.
我编写了这个低端函数来做到这一点,而无需更高级别的 API。
It creates a bitmap and graphics object, writes the string to the bitmap, scans backwards for the font edge and then returns the width in pixels
它创建一个位图和图形对象,将字符串写入位图中,向后扫描字体边缘,然后返回宽度(以像素为单位)
Private Function FontLengthInPixels(inputString As String, FontStyle As Font) As Integer
' Pick a large, arbitrary number for the width (500) in my case
Dim bmap As New Bitmap(500, 100)
Dim g As Graphics = Graphics.FromImage(bmap)
g.FillRectangle(Brushes.Black, bmap.GetBounds(GraphicsUnit.Pixel))
g.DrawString(inputString, FontStyle, Brushes.White, New Point(0, 0))
' Scan backwards to forwards, since we know the first pixel location is 0,0; we need to find the LAST and subtract
' the bitmap width from it to find the width.
For x = -(bmap.Width - 1) To -1
' Scan from the 5th pixel to the 10th, we'll find something within that range!
For y = 5 To 10
Dim col As Color = bmap.GetPixel(Math.Abs(x), y)
' Look for white (ignore alpha)
If col.R = 255 And col.G = 255 And col.B = 255 Then
Return Math.Abs(x) ' We got it!
End If
Next
Next
' Lets do this approx
Return 0
End Function
回答by Hyman
i have just recently done this in one of my projects here is how i did it
我最近刚刚在我的一个项目中完成了这就是我是如何做到的
Dim textsize As Size = TextRenderer.MeasureText(cbx_Email.Text, cbx_Email.Font)
cbx_Email.Width = textsize.Width + 17
this is in a combobox.SelectedIndex changed sub.
这是在一个组合框。SelectedIndex 更改子。
The +17 is for the pixels that the dropdown arrow takes up in a combobox so it doesntcover text.
+17 表示下拉箭头在组合框中占据的像素,因此它不会覆盖文本。
by using control.font it allows the code to dynamically change no matter what font is being used. Using Control.Text means you can use this on anything and wont have to change the code when changing the text of the control or page.
通过使用 control.font,无论使用什么字体,它都允许代码动态更改。使用 Control.Text 意味着您可以在任何东西上使用它,并且在更改控件或页面的文本时不必更改代码。