C#: Graphics DrawString to Exactly Place Text on a System.Label
Posted
by jp2code
on Stack Overflow
See other posts from Stack Overflow
or by jp2code
Published on 2010-04-07T15:20:37Z
Indexed on
2010/04/07
15:23 UTC
Read the original article
Hit count: 608
I have overridden the OnPaint method of my Label control in VS2008:
void Label_OnPaint(object sender, PaintEventArgs e) {
base.OnPaint(e);
Label lbl = sender as Label;
if (lbl != null) {
string Text = lbl.Text;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
if (myShowShadow) { // draw the shadow first!
e.Graphics.DrawString(Text, lbl.Font, new SolidBrush(myShadowColor), myShadowOffset, StringFormat.GenericDefault);
}
e.Graphics.DrawString(Text, lbl.Font, new SolidBrush(lbl.ForeColor), 0, 0, StringFormat.GenericDefault);
}
}
This works, but I really want to find out how to center the text both vertically and horizontally. I've heard of the MeasureString()
method, but my "Text" complicates matters because it could include page breaks.
Could someone guide me with how to do this?
© Stack Overflow or respective owner