|
-
Nov 1st, 2005, 12:42 PM
#1
Thread Starter
Frenzied Member
[Resolved] GDI and strings
I'm using GDI to write out a string on a custom control that I am creating. How can I get the width of that string so I can make it centered in the control?
Thanks
Last edited by mpdeglau; Nov 1st, 2005 at 01:17 PM.
-
Nov 1st, 2005, 12:52 PM
#2
Re: GDI and strings
There is a MeasureString function available in the Graphics Class that do so.
But you have to count manualy the cariage return in it.
-
Nov 1st, 2005, 01:17 PM
#3
Thread Starter
Frenzied Member
-
Nov 1st, 2005, 01:29 PM
#4
Hyperactive Member
Re: [Resolved] GDI and strings
You can also use a stringformat which will let you set the alignment - and will wrap the text for you:
VB Code:
Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
' stringformat lets you set the alignment:
Dim sf As StringFormat = StringFormat.GenericDefault.Clone
sf.Alignment = StringAlignment.Center ' horiz alignment
sf.LineAlignment = StringAlignment.Center ' vert alignment
' look at overload list for drawstring to see one that uses stringformat:
' Overloads Public Sub DrawString(String, Font, Brush, RectangleF, StringFormat)
' make a rectangleF that is the size of the label
Dim rec As New RectangleF(0, 0, Label1.Width, Label1.Height)
' draw string:
e.Graphics.DrawString("test", Me.Font, Brushes.Black, rec, sf)
End Sub
-
Nov 1st, 2005, 01:35 PM
#5
Re: [Resolved] GDI and strings
 Originally Posted by jo0ls
You can also use a stringformat which will let you set the alignment - and will wrap the text for you:
VB Code:
Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
' stringformat lets you set the alignment:
Dim sf As StringFormat = StringFormat.GenericDefault.Clone
sf.Alignment = StringAlignment.Center ' horiz alignment
sf.LineAlignment = StringAlignment.Center ' vert alignment
' look at overload list for drawstring to see one that uses stringformat:
' Overloads Public Sub DrawString(String, Font, Brush, RectangleF, StringFormat)
' make a rectangleF that is the size of the label
Dim rec As New RectangleF(0, 0, Label1.Width, Label1.Height)
' draw string:
e.Graphics.DrawString("test", Me.Font, Brushes.Black, rec, sf)
End Sub
Good thing to know !!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|