Need to clip strings when drawing on a panel

  • Thread starter Thread starter Academia
  • Start date Start date
A

Academia

As a simple example suppose I want to draw two columns of strings.

I know where each column starts and the widths.

So before I print a string I measure it and if it is longer than the column
width I remove a character at the end and measure it again. I repeat this
until the string is not longer than the desired width.

I'm drawing the strings on panel and wonder if there is a faster way.



Thanks
 
Academia said:
As a simple example suppose I want to draw two columns of strings.

I know where each column starts and the widths.

So before I print a string I measure it and if it is longer than
the column width I remove a character at the end and measure it
again. I repeat this until the string is not longer than the desired
width.

I'm drawing the strings on panel and wonder if there is a faster
way.


You can use an overloaded DrawString version specifiying the
"LayoutRectangle" and formatting options:

Dim rect As RectangleF
Dim sf As StringFormat

'--- 50 is the column width ---
rect = New RectangleF(30, 40, 50, 0)
sf = New StringFormat(StringFormatFlags.NoWrap)

'optional:
'sf.Trimming = StringTrimming.None

e.Graphics.DrawRectangle(Pens.Blue, Rectangle.Round(rect))
e.Graphics.DrawString( _
"very long text that does not fit into the column", _
Panel1.Font, Brushes.Black, rect, sf _
)


Armin
 
Thanks I'll try it now.

BTW - does Rectangle.Round(rect)) draw the rectangle inside the space
defined?

for example, suppose the pen width is 3 pixels wide, what gets drawn where?


I'm assuming the width can be in pixels. I've just looked at the Pen doc and
it doesn't give a clue as to the units for the width.

Thanks a lot


Armin Zingler said:
Leave out this line. Was for testing only.

thanks for going to all that trouble!
 
Academia said:
Thanks I'll try it now.

BTW - does Rectangle.Round(rect)) draw the rectangle inside the
space defined?

Rectangle.Round converts a RectangleF to a Rectangle, rounding the
left/top/width/height values.
for example, suppose the pen width is 3 pixels wide, what gets drawn
where?


I'm assuming the width can be in pixels. I've just looked at the Pen
doc and it doesn't give a clue as to the units for the width.


I don't know.


Armin
 
Back
Top