Resolved VS2008 DrawString within bounds of Rectangle

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I have this code where I'm wanting to draw text inside a defined rectangle and if it's too wide I want it to be slit onto multiple lines and if it's too tall, I was it to only paint what it can which right now it's not cutting it off it continues on below the bottom border. Here's my code:
VB.NET:
Dim NotesRect As New RectangleF(mMargins.Left + LeftIndent, m_PagePositionSingle, mMargins.Right, mMargins.Bottom)
e.Graphics.DrawString(NotesString, m_ItemFont, Brushes.Black, NotesRect, New StringFormat(StringFormatFlags.FitBlackBox Or StringFormatFlags.LineLimit))
Anyone know of an easy way to fix this?
 
Actually, I don't get text outside the rectangle with that code, but FitBlackBox should allow part of chars to overhang rectangle, while LineLimit should ensure only whole lines (in relation to a drawn lines height) are visible and drawn within rectangle.
LineLimit: Only entire lines are laid out in the formatting rectangle.
FitBlackBox: Parts of characters are allowed to overhang the string's layout rectangle.
Seems mutually exclusive to me, but LineLimit took effect when I tested. Also without StringFormat text is limited to rectangle, but then allow half visible lines.

Also, StringFormat implements IDisposable for a reason, you need to call Dispose after use.
 
Turns out I was defining the rectangle wrong, I also added the Using sf As StringFormat around it too since I hadn't done that before posting:
VB.NET:
Dim NotesRect As New RectangleF(mMargins.Left + LeftIndent, m_PagePositionSingle, mMargins.Right - (mMargins.Left + LeftIndent), mMargins.Bottom - m_PagePositionSingle)
Using sf As New StringFormat(StringFormatFlags.LineLimit)
    .DrawString(CCard.Notes, m_PrintItemFont, Brushes.Black, NotesRect, sf)
End Using
 
Back
Top