将标签的Spring属性设置为“真”将自动调整其大小。要得到省略号,你需要覆盖这幅画。向项目中添加新类并粘贴下面显示的代码。编译。您将在状态条设计器下拉列表中获得新的SpringLabel控件。
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);
}
}
如果使用“图像”或“文本对齐”属性,则需要做更多的工作。