Mark

Mark

  • NA
  • 4
  • 0

string width in pixels

Jun 29 2010 1:05 PM

Hi, y'all,
I am trying to determine the length of a string in pixels.
I created a little test program for myself and placed three labels on it, each with the same font in a different size. I set the width of the label to 1024, the screenwidth. I used the same test text for each label, cutting it off at the last character that shows, so there is not hidden text running off the label to be included in the string width count.
What I have found is that the width of the string is being reported as wider than the label, even though, as I just mentioned, there are no characters running off the edge of the label. So, something's awry. Does anyone know how to get an accurate width?
Here follows the whole test code. You can drop three labels on a form and run it to see what I mean.
 

Option Explicit On
Option Strict On
Public Class frmMain
Dim StringSize As New SizeF
Dim StringSizeForLargerFont As New SizeF
Dim StringSizeForLargestFont As New SizeF
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblMessage.Text = "1 2 3 4 5 6 7 8 9 0 A 1 2 3 4 5 6 7 8 9 0 B 1 2 3 4 5 6 7 8 9 0 C " & _
"1 2 3 4 5 6 7 8 9 0 D 1 2 3 4 5 6 7 8 9 0 E 1 2 3 4 5 6 7 8 9 0 F 1 2 3 4 5 6 7 8 9 0 G " & _
"1 2 3 4 5 6 7 8 9 0 H 1 2 3 4 5 6 7 8 9"
lblMessage.Width = 1280
lblMessage.Font = New System.Drawing.Font("Trebuchet MS", 12)
lblMessage.Font = New System.Drawing.Font(lblMessage.Font, FontStyle.Regular)
lblMessageInLargerFont.Text = "1 2 3 4 5 6 7 8 9 0 A 1 2 3 4 5 6 7 8 9 0 B 1 2 3 4 5 6 7 8 9 0 C " & _
"1 2 3 4 5 6 7 8 9 0 D 1 2 3 4 5 6 7 8 9 0 E 1 2 3 4 5 6 7 8 9 0 F"
lblMessageInLargerFont.Width = 1280
lblMessageInLargerFont.Font = New System.Drawing.Font("Trebuchet MS", 17.25)
lblMessageInLargerFont.Font = New System.Drawing.Font(lblMessageInLargerFont.Font, FontStyle.Regular)
lblMessageInLargestFont.Text = "1 2 3 4 5 6 7 8 9 0 A 1 2 3 4 5 6 7 8 9 0 B 1 2 3 4 5 6 7 8 9 0 C " & _
"1 2 3 4 5 6 7 8 9 0 D 1 2 3 4 5 6 7 8 9 0 E 1 2"
lblMessageInLargestFont.Width = 1280
lblMessageInLargestFont.Font = New System.Drawing.Font("Trebuchet MS", 20.25)
lblMessageInLargestFont.Font = New System.Drawing.Font(lblMessageInLargestFont.Font, FontStyle.Regular)
End Sub
Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.PageUnit = GraphicsUnit.Pixel
StringSize = e.Graphics.MeasureString(lblMessage.Text, lblMessage.Font)
StringSizeForLargerFont = e.Graphics.MeasureString(lblMessageInLargerFont.Text, lblMessageInLargerFont.Font)
StringSizeForLargestFont = e.Graphics.MeasureString(lblMessageInLargestFont.Text, lblMessageInLargestFont.Font)
lblInformation.Text = _
"Screen Dimensions: " & GetScreenWidth() & " X " & GetScreenHeight() & vbCrLf & _
"lblMessage Width: " & lblMessage.Width & vbCrLf & _
"Message String Width: " & StringSize.ToString & vbCrLf & _
"Larger Message String Width: " & StringSizeForLargerFont.ToString & vbCrLf & _
"Largest Message String Width: " & StringSizeForLargestFont.ToString
End Sub
Public Function GetScreenHeight() As Integer
Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height
Return intY
End Function
Public Function GetScreenWidth() As Integer
Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
Return intX
End Function
End Class

I appreciate input.