My EllipseLabel - wrapping text question
-
I wrote my first own control, it derives from Windows.Forms.Control, and it's a simple ellipse label. The code for drawing:
protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; Brush foreBrush = new SolidBrush(ForeColor); Brush backBrush = new SolidBrush(BackColor); g.FillEllipse(backBrush, ClientRectangle); StringFormat fmt = new StringFormat(); fmt.Alignment = StringAlignment.Center; fmt.LineAlignment = StringAlignment.Center; g.DrawString(Text, Font, foreBrush, ClientRectangle, fmt); base.OnPaint(pe); } }
The question - how to implement so that the label wraps text if it is to long to fit in a single line? -
I wrote my first own control, it derives from Windows.Forms.Control, and it's a simple ellipse label. The code for drawing:
protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; Brush foreBrush = new SolidBrush(ForeColor); Brush backBrush = new SolidBrush(BackColor); g.FillEllipse(backBrush, ClientRectangle); StringFormat fmt = new StringFormat(); fmt.Alignment = StringAlignment.Center; fmt.LineAlignment = StringAlignment.Center; g.DrawString(Text, Font, foreBrush, ClientRectangle, fmt); base.OnPaint(pe); } }
The question - how to implement so that the label wraps text if it is to long to fit in a single line?Hi, you can use MeasureString to give you the length of the string in drawing units (pixels) and compare the width of your control - border (or just client width if there is not an NC portion). Then of course, just extend the height of your control by the height of the current font, split, and wrap the line. You can get the font height with the Font property and call its Height property but make sure you use your graphics object's MeasureString method to get the length of the string in question. hope that is enough detail for you, if not let me know and i'll try to help further.
-
Hi, you can use MeasureString to give you the length of the string in drawing units (pixels) and compare the width of your control - border (or just client width if there is not an NC portion). Then of course, just extend the height of your control by the height of the current font, split, and wrap the line. You can get the font height with the Font property and call its Height property but make sure you use your graphics object's MeasureString method to get the length of the string in question. hope that is enough detail for you, if not let me know and i'll try to help further.
That'll do, thanks a lot man!