Text drawing problems

B

BluDog

Hi

I am trying to draw text in a panel. I want the width of the panel to
be fixed but the height to be determined by the number of lines the
text needs to cover in the panel. I also want to be able to specify
the linespacing for the text.

I have looked at the StringFormat class, MSDN stating:

"Encapsulates text layout information (such as alignment and line
spacing)..."

However there appears to be no property for setting the Line Spacing
of the text, and I cannot work out how to wrap it...

Private Sub DrawText(ByVal e As PaintEventArgs)

Dim Text As String
Dim I As Integer
For I = 1 To 20
Text &= "Sample Text"
If I < 20 Then Text &= " "
Next

Dim TextSize As PointF
Dim StringFormat As New StringFormat

e.Graphics.DrawString(Text, Font, New SolidBrush(Color.Black),
0, 0, StringFormat.GenericTypographic)

End Sub

Any help would be appreciated!

Cheers

Blu.
 
S

Supra

u need this:
dim g as graphics
dim path as ne graphicpath
path.AddString()
set world transform
dim rectBound as rectanglaF= path.GetBound()
dim aptDist() as PointF= new point(0,0), .....}
g.transform= new Matrix(rectbound,aptDist))
then fill path
g.Fillapth(new solidbrush(colour,path)


or u can do path.AddString.

u can get help from msdn from microsoft's website.
regards
 
K

Ken Tucker [MVP]

Hi,

Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

Dim strOut As String = "This is my really really long line of
text to print on the screen"
Dim sf As New StringFormat

e.Graphics.DrawString(strOut, Me.Font, Brushes.Black, _
RectangleF.op_Implicit(Panel1.ClientRectangle), sf)

End Sub

Ken
-----------------
 
B

BluDog

Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

Dim strOut As String = "This is my really really long line of
text to print on the screen"
Dim sf As New StringFormat

e.Graphics.DrawString(strOut, Me.Font, Brushes.Black, _
RectangleF.op_Implicit(Panel1.ClientRectangle), sf)

End Sub

Ken, thanks for this, it is pretty much where I am, however it is not
a solution to the line spacing problem.

Cheers

Tom
 
H

Hilbert A.

BluDog said:
However there appears to be no property for setting the Line Spacing
of the text, and I cannot work out how to wrap it...
e.Graphics.DrawString(Text, Font, New SolidBrush(Color.Black),
0, 0, StringFormat.GenericTypographic)

dim ls as integer = LineHeight ' define the value as you wish

dim Lines() as string = split(Text,vbcrlf)

dim Y as integer = StartTop ' define the value as you wish

for i as integer = 0 to ubound(Lines)

e.Graphics.DrawString(Lines(i), Font, New SolidBrush(Color.Black),
0,Y, StringFormat.GenericTypographic)

y+=LineHeight

next
 
B

BluDog

Hilbert

Thanks for your suggestion, the problem is there are no Line Feeds in
the string necessarily, therefore you cannot split it up in that
way... however i have tweeked it to find sensible line breaks and wrap
it manually:

Private LineHeight As Integer = 21
Private Format As StringFormat = StringFormat.GenericTypographic
Private SampleFont As Font = Font

Private Sub DrawText(SampleText as string, e As PaintEventArgs)

'Get the size of the panel the text is to be printed into
Dim PanelSize As New SizeF(SamplePanel.Width,
SamplePanel.Height)

'Split the text into an array of string
Dim Words() As String = SampleText.Split(" ")

'Loop through lines
Dim LineCount As Integer
Dim Line, TestLine As String
For Word As Integer = 0 To Words.Length - 1
TestLine = (Line & " " & Words(Word)).Trim
If e.Graphics.MeasureString(TestLine, SampleFont, New
PointF(3, 0), Format).Width > PanelSize.Width Then
e.Graphics.DrawString(Line, SampleFont, Brushes.Black,
3, LineCount * LineHeight, Format)
Line = ""
LineCount += 1
Word -= 1
ElseIf Word = Words.Length - 1 Then
e.Graphics.DrawString(TestLine, SampleFont,
Brushes.Black, 3, LineCount * LineHeight, Format)
LineCount += 1
Else
Line = TestLine
End If
Next

End Sub


Cheers

Blu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top