I ran into this issue myself, and ended up creating my own simple Label control.
.Net's Label control is a surprisingly complicated beast, and is therefore slower than we'd like.
You can make a class that inherits Control
, call SetStyle
in the constructor to make it double-buffered and user-painted, then override the OnPaint
method to call e.Graphics.DrawString
and draw the Text
property.
Finally, override Text
or TextChanged
and call Invalidate
.
As long as you don't need AutoSize, this will be substantially faster than the standard Label control.
Here is my implementation: (Currently in use in production)
///<summary>A simple but extremely fast control.</summary>
///<remarks>Believe it or not, a regular label isn't fast enough, even double-buffered.</remarks>
class FastLabel : Control {
public FastLabel() {
SetStyle(ControlStyles.AllPaintingInWmPaint
| ControlStyles.CacheText
| ControlStyles.OptimizedDoubleBuffer
| ControlStyles.ResizeRedraw
| ControlStyles.UserPaint, true);
}
protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); Invalidate(); }
protected override void OnFontChanged(EventArgs e) { base.OnFontChanged(e); Invalidate(); }
static readonly StringFormat format = new StringFormat {
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.DrawString(Text, Font, SystemBrushes.ControlText, ClientRectangle, format);
}
}
If you don't want centering, you can get rid of, or change, the StringFormat
.