自动指示截断在ToolTipStatusLabel指示、ToolTipStatusLabel

2023-09-02 10:32:57 作者:痛深入骨髓

我有一个.NET应用程序与StatusStrip中拿着三ToolTipStatusLabels。标签的文本被从应用填充,因为它们显示状态。对于一些情况下,他们可以持有空文。

I have a .NET application with a StatusStrip holding three ToolTipStatusLabels. The Text of the labels are filled from the application as they show the status. For some circumstances they can hold an empty text.

当我调整窗口,在ToolTipStatusLabels是隐藏的,当他们不能适合在StatusStrip中。我想有文字截断当标签不能适合在StatusStrip中。默认的行为隐藏标签,使得它很难空的文本或隐藏的标签来区分。

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?

推荐答案

标签的春天属性设置为True得到的是自动调整其大小。为了得到省略号,你需要重写绘画。添加一个新类到您的项目并粘贴下面所示的code。编译。你会得到在状态条设计下拉列表中选择新的SpringLabel控制。

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);
    }
}

您需要,如果你使用图片或textAlign设置的属性做更多的工作。

You'll need to do more work if you use the Image or TextAlign properties.