indicate truncation in ToolTipStatusLabel automatically
I have a .NET application with a StatusStrip holding three Too开发者_开发知识库lTipStatusLabels. The Text of the labels are filled from the application as they show the status. For some circumstances they can hold an empty text.
When I resize the window, the ToolTipStatusLabels are hidden when they cannot be fit in the StatusStrip. I would like to have the text truncated when the label cannot be fit in the StatusStrip. The default behavior to hide the label makes it difficult to distinguish between empty text or hidden label.
To indicate that the text is truncated automatically, this should be indicated with an ellipsis (...). How can this be done?
Set the label's Spring property to True to get is to adjust its size automatically. To get ellipses you'll need to override the painting. Add a new class to your project and paste the code shown below. Compile. You'll get the new SpringLabel control in the status strip designer dropdown list.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.StatusStrip)]
public class SpringLabel : ToolStripStatusLabel {
public SpringLabel() {
this.Spring = true;
}
protected override void OnPaint(PaintEventArgs e) {
var flags = TextFormatFlags.Left | TextFormatFlags.EndEllipsis;
var bounds = new Rectangle(0, 0, this.Bounds.Width, this.Bounds.Height);
TextRenderer.DrawText(e.Graphics, this.Text, this.Font, bounds, this.ForeColor, flags);
}
}
You'll need to do more work if you use the Image or TextAlign properties.
精彩评论