自动指示ToolTipStatusLabel中的截断

har*_*per 8 .net c# winforms

我有一个.NET应用程序,其StatusStrip包含三个ToolTipStatusLabels.标签的文本在应用程序显示状态时填充.在某些情况下,他们可以保留一个空文本.

当我调整窗口大小时,ToolTipStatusLabel在无法放入StatusStrip时会被隐藏.我想在标签无法适应StatusStrip时截断文本.隐藏标签的默认行为使得难以区分空文本或隐藏标签.

为了表明文本被自动截断,应该用省略号(...)表示.如何才能做到这一点?

Han*_*ant 14

将标签的Spring属性设置为True,以便自动调整其大小.要获得省略号,您需要覆盖绘画.在项目中添加一个新类并粘贴下面显示的代码.编译.您将在状态条设计器下拉列表中获得新的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果使用Image或TextAlign属性,则需要做更多工作.