Results 1 to 9 of 9

Thread: Getting the Length of string

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    9

    Getting the Length of string

    I'm using Mobile VB to develop an application for my P800. How can I get the width (in pixels) of a string using a specific font, size and style?

    This can only be done to develop for the PALM device.
    Code:
    AFTextBox1.Text = afExtLib.StringWidth("AFSE UIQ Standard", 15, 0, "1234hello")
    How can I do something similar for my P800?

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    One quick way is to assign the font and text property to a hidden label whose autosize is set to true and text is aligned middle left. Then getting label size will give you a rough estimate of the size of text. I think there is other way using GDI + . I will look at it.
    Last edited by Lunatic3; Nov 21st, 2003 at 03:02 AM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    This is what i came across:
    VB Code:
    1. Dim sformat As New StringFormat
    2. Dim s As String = "Test String"
    3. Dim cr(0) As CharacterRange
    4. Dim reg(0) As Drawing.Region
    5. Dim stringwidth, stringheight As Single
    6. Dim gr As Graphics
    7. Dim fnt As Font = Me.Font
    8. gr = Me.CreateGraphics
    9. cr(0) = New CharacterRange(0, s.Length)
    10. sformat.SetMeasurableCharacterRanges(cr)
    11. reg = gr.MeasureCharacterRanges(s, fnt, New RectangleF(0, 0, Single.MaxValue, Single.MaxValue), sformat)
    12. stringwidth = reg(0).GetBounds(gr).Right()
    13. stringheight = reg(0).GetBounds(gr).Bottom
    14. MessageBox.Show(stringwidth.ToString & ":" & stringheight.ToString)
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    For unknown reasons the previous code does not work correctly and i dont know why , but this is from MSDN and is ok:
    VB Code:
    1. Public Sub MeasureStringSizeFFormatInts(ByVal e As PaintEventArgs)
    2.         ' Set up string.
    3.         Dim measureString As String = "Measure String"
    4.         Dim stringFont As New Font("Arial", 16)
    5.         ' Set maximum layout size.
    6.         Dim layoutSize As New SizeF(100.0F, 200.0F)
    7.         ' Set string format.
    8.         Dim newStringFormat As New StringFormat
    9.         newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical
    10.         ' Measure string.
    11.         Dim charactersFitted As Integer
    12.         Dim linesFilled As Integer
    13.         Dim stringSize As New SizeF
    14.         stringSize = e.Graphics.MeasureString(measureString, stringFont, _
    15.         layoutSize, newStringFormat, charactersFitted, linesFilled)
    16.         ' Draw rectangle representing size of string.
    17.         e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
    18.         stringSize.Width, stringSize.Height)
    19.         ' Draw string to screen.
    20.         e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
    21.         New PointF(0, 0), newStringFormat)
    22.         ' Draw output parameters to screen.
    23.         Dim outString As String = "chars " & charactersFitted & _
    24.         ", lines " & linesFilled
    25.         e.Graphics.DrawString(outString, stringFont, Brushes.Black, _
    26.         New PointF(100, 0))
    27. End Sub
    Last edited by Lunatic3; Dec 6th, 2003 at 05:43 AM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  5. #5
    Member
    Join Date
    Sep 2003
    Posts
    47
    Why not use the MeasureString function of the graphics class?
    Code:
    Overloads Public Function MeasureString(String, Font) As SizeF
    
    Overloads Public Function MeasureString(String, Font, Integer) As SizeF
    
    Overloads Public Function MeasureString(String, Font, SizeF) As SizeF
    
    Overloads Public Function MeasureString(String, Font, Integer, StringFormat) As SizeF
    
    Overloads Public Function MeasureString(String, Font, PointF, StringFormat) As SizeF
    
    Overloads Public Function MeasureString(String, Font, SizeF, StringFormat) As SizeF
    
    Overloads Public Function MeasureString(String, Font, SizeF, StringFormat, ByRef Integer, ByRef Integer) As SizeF
    Now the only problem i have is that this function requires an e, PaintEvent argument. I need to call this function somewhere else that i can't get hold of the e and passing the e argument to that function is impossible.
    Creating a new graphics reference using the CreateGraphics function is wrong and the measured string will be different from what is drawn in the Paint Event.
    Last edited by Archaven; Dec 4th, 2003 at 04:10 AM.

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    In fact i was trying to answer your question. If you find the width and height of the string you want to draw, then you can find where to start to draw so its center is exactly the center of your ellipse or recatngle. However I fixed the first code and it just do what it should do. MeasureCharacterRanges seems to be more acurate. However you can compare these two using different font sizes cause at different font size like (18,18.25,18.5,18.75,19) the differences change.
    Try This code:
    VB Code:
    1. Public Sub MeasureStringSizeFFormatInts(ByVal e As PaintEventArgs)
    2.         Dim sformat As StringFormat = StringFormat.GenericTypographic
    3.         Dim s As String = "Measure String"
    4.         Dim cr(0) As CharacterRange
    5.         Dim reg(0) As Drawing.Region
    6.         Dim gr As Graphics
    7.         Dim fnt As New Font("Times", 20)
    8.         Dim charactersFitted As Integer
    9.         Dim linesFilled As Integer
    10.         Dim layoutSize As New SizeF(Single.MaxValue, Single.MaxValue)
    11.         Dim stringSize1 As New SizeF
    12.         Dim stringSize2 As New SizeF
    13.         gr = e.Graphics
    14.         cr(0) = New CharacterRange(0, s.Length)
    15.         sformat.FormatFlags = StringFormatFlags.NoClip Or StringFormatFlags.NoWrap
    16.         sformat.Trimming = StringTrimming.None
    17.         sformat.SetMeasurableCharacterRanges(cr)
    18.         reg = gr.MeasureCharacterRanges(s, fnt, New RectangleF(New PointF(0.0F, 0.0F), layoutSize), sformat)
    19.         stringSize1 = New SizeF(reg(0).GetBounds(gr).Width, reg(0).GetBounds(gr).Height)
    20.         gr.FillRegion(Brushes.Gray, reg(0))
    21.         stringSize2 = gr.MeasureString(s, fnt, layoutSize, sformat, charactersFitted, linesFilled)
    22.         gr.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, stringSize2.Width, stringSize2.Height)
    23.         gr.DrawString(s, fnt, Brushes.Blue, New PointF(0, 0), sformat)
    24.         MessageBox.Show("MeasureCharacterRanges Width=" & stringSize1.Width.ToString & Environment.NewLine & _
    25.         "MeasureString Wdith=" & stringSize2.Width.ToString & Environment.NewLine & "MeasureCharacterRanges Height=" & _
    26.         stringSize1.Height.ToString & Environment.NewLine & "MeasureString Height=" & stringSize2.Height.ToString)
    27. End Sub
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  7. #7
    Member
    Join Date
    Sep 2003
    Posts
    47
    Thanks Lunatic for the reply.
    I took your advice on using GraphicsPath to draw now instead of direct drawing as what you claimed that from your experience drawing with GraphicsPath seems to increase performance.

    Right now i need your advice on AddString of the GraphicsPath function. As my previous program, Text are consider objects as well. Hence, I need to draw graphics string to the canvas too. I noticed that if i were to use AddString of the GraphicsPath class, it seems very difficult to hit.
    Right now, I am using the AddRectangle function for the text string instead. Any comments?

    Also, i would like to know if you can help on my FileIO problem too. Thanks.

    Archaven.

  8. #8
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    What is your problem with addstring? and what problem with IO?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  9. #9
    Member
    Join Date
    Sep 2003
    Posts
    47
    The problem with AddString is that it's quite difficult to hit if im using the IsVisible method. Hence, im using AddRectangle instead.
    I can read/write into a textfile but i need it in binary format. I've checked BinaryReader and BinaryWriter but they don't seem to be able to read/write class objects. I think someone pointed out that i need to use serialization.

    Archaven

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