|
-
Nov 21st, 2003, 01:37 AM
#1
Thread Starter
New Member
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?
-
Nov 21st, 2003, 02:40 AM
#2
Frenzied Member
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
-
Dec 4th, 2003, 02:39 AM
#3
Frenzied Member
This is what i came across:
VB Code:
Dim sformat As New StringFormat
Dim s As String = "Test String"
Dim cr(0) As CharacterRange
Dim reg(0) As Drawing.Region
Dim stringwidth, stringheight As Single
Dim gr As Graphics
Dim fnt As Font = Me.Font
gr = Me.CreateGraphics
cr(0) = New CharacterRange(0, s.Length)
sformat.SetMeasurableCharacterRanges(cr)
reg = gr.MeasureCharacterRanges(s, fnt, New RectangleF(0, 0, Single.MaxValue, Single.MaxValue), sformat)
stringwidth = reg(0).GetBounds(gr).Right()
stringheight = reg(0).GetBounds(gr).Bottom
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
-
Dec 4th, 2003, 03:11 AM
#4
Frenzied Member
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:
Public Sub MeasureStringSizeFFormatInts(ByVal e As PaintEventArgs)
' Set up string.
Dim measureString As String = "Measure String"
Dim stringFont As New Font("Arial", 16)
' Set maximum layout size.
Dim layoutSize As New SizeF(100.0F, 200.0F)
' Set string format.
Dim newStringFormat As New StringFormat
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical
' Measure string.
Dim charactersFitted As Integer
Dim linesFilled As Integer
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(measureString, stringFont, _
layoutSize, newStringFormat, charactersFitted, linesFilled)
' Draw rectangle representing size of string.
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
stringSize.Width, stringSize.Height)
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
New PointF(0, 0), newStringFormat)
' Draw output parameters to screen.
Dim outString As String = "chars " & charactersFitted & _
", lines " & linesFilled
e.Graphics.DrawString(outString, stringFont, Brushes.Black, _
New PointF(100, 0))
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
-
Dec 4th, 2003, 04:06 AM
#5
Member
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.
-
Dec 4th, 2003, 05:03 AM
#6
Frenzied Member
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:
Public Sub MeasureStringSizeFFormatInts(ByVal e As PaintEventArgs)
Dim sformat As StringFormat = StringFormat.GenericTypographic
Dim s As String = "Measure String"
Dim cr(0) As CharacterRange
Dim reg(0) As Drawing.Region
Dim gr As Graphics
Dim fnt As New Font("Times", 20)
Dim charactersFitted As Integer
Dim linesFilled As Integer
Dim layoutSize As New SizeF(Single.MaxValue, Single.MaxValue)
Dim stringSize1 As New SizeF
Dim stringSize2 As New SizeF
gr = e.Graphics
cr(0) = New CharacterRange(0, s.Length)
sformat.FormatFlags = StringFormatFlags.NoClip Or StringFormatFlags.NoWrap
sformat.Trimming = StringTrimming.None
sformat.SetMeasurableCharacterRanges(cr)
reg = gr.MeasureCharacterRanges(s, fnt, New RectangleF(New PointF(0.0F, 0.0F), layoutSize), sformat)
stringSize1 = New SizeF(reg(0).GetBounds(gr).Width, reg(0).GetBounds(gr).Height)
gr.FillRegion(Brushes.Gray, reg(0))
stringSize2 = gr.MeasureString(s, fnt, layoutSize, sformat, charactersFitted, linesFilled)
gr.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, stringSize2.Width, stringSize2.Height)
gr.DrawString(s, fnt, Brushes.Blue, New PointF(0, 0), sformat)
MessageBox.Show("MeasureCharacterRanges Width=" & stringSize1.Width.ToString & Environment.NewLine & _
"MeasureString Wdith=" & stringSize2.Width.ToString & Environment.NewLine & "MeasureCharacterRanges Height=" & _
stringSize1.Height.ToString & Environment.NewLine & "MeasureString Height=" & stringSize2.Height.ToString)
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
-
Dec 5th, 2003, 04:43 AM
#7
Member
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.
-
Dec 6th, 2003, 06:28 AM
#8
Frenzied Member
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
-
Dec 7th, 2003, 09:09 PM
#9
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|