Results 1 to 5 of 5

Thread: [Resolved] GDI and strings

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    [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.

  2. #2
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    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.
    Using VS 2010 on Fw4.0

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: GDI and strings

    Thanks

  4. #4
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    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:
    1. Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
    2.         ' stringformat lets you set the alignment:
    3.         Dim sf As StringFormat = StringFormat.GenericDefault.Clone
    4.         sf.Alignment = StringAlignment.Center ' horiz alignment
    5.         sf.LineAlignment = StringAlignment.Center ' vert alignment
    6.         ' look at overload list for drawstring to see one that uses stringformat:
    7.         ' Overloads Public Sub DrawString(String, Font, Brush, RectangleF, StringFormat)
    8.         ' make a rectangleF that is the size of the label
    9.         Dim rec As New RectangleF(0, 0, Label1.Width, Label1.Height)
    10.         ' draw string:
    11.         e.Graphics.DrawString("test", Me.Font, Brushes.Black, rec, sf)
    12.     End Sub

  5. #5
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: [Resolved] GDI and strings

    Quote 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:
    1. Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
    2.         ' stringformat lets you set the alignment:
    3.         Dim sf As StringFormat = StringFormat.GenericDefault.Clone
    4.         sf.Alignment = StringAlignment.Center ' horiz alignment
    5.         sf.LineAlignment = StringAlignment.Center ' vert alignment
    6.         ' look at overload list for drawstring to see one that uses stringformat:
    7.         ' Overloads Public Sub DrawString(String, Font, Brush, RectangleF, StringFormat)
    8.         ' make a rectangleF that is the size of the label
    9.         Dim rec As New RectangleF(0, 0, Label1.Width, Label1.Height)
    10.         ' draw string:
    11.         e.Graphics.DrawString("test", Me.Font, Brushes.Black, rec, sf)
    12.     End Sub
    Good thing to know !!
    Using VS 2010 on Fw4.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width