Results 1 to 5 of 5

Thread: Help with drawing a string

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    73

    Help with drawing a string

    I have a problem hopefully someone can help me out with. I am trying to draw a string in a rectange (not inheriting from any control). The rectangle can only be so big. I need to figure out if the string will not fit in the rectangle, then add a "..." to the end of the string right before it gets cut off.
    I know how to the draw the string, and I can figure out how measure it with the font and string formating. But finding that cut-off point is where I am having much difficulty.

    Thanks!

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Help with drawing a string

    There is a Graphics.MeasureString method you can use to measure the length of the string...

  3. #3
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Help with drawing a string

    Like this?
    VB Code:
    1. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    2.         Dim txt As String
    3.         Dim the_Font As New Font("Times New Roman", 30, FontStyle.Bold, GraphicsUnit.Pixel)
    4.         Dim layout_rect As RectangleF
    5.         Dim string_format As New StringFormat
    6.  
    7.         e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit
    8.         layout_rect = New RectangleF(100, 0, 180, 70)
    9.         txt = "Hello this is the text that is going to be trimmed"
    10.         string_format.Trimming = StringTrimming.EllipsisCharacter
    11.         e.Graphics.DrawString(txt, the_Font, Brushes.Black, layout_rect, string_format)
    12.         e.Graphics.DrawRectangle(Pens.Red, Rectangle.Round(layout_rect))
    13.  
    14.     End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    73

    Re: Help with drawing a string

    I can already get the length of the string. What I need to find is the length of the "fitting" string and cut it off at the proper length. For example, a string that measures an area of 200,400. However the actually area displayed is 200, 200. I need to find that point in the string and add a "..." to indicate there is more text.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    73

    Re: Help with drawing a string

    Thanks moeur!

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