c# - 自动指示 ToolTipStatusLabel 中的截断

标签 c# .net winforms

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

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

要指示文本被自动截断,应使用省略号 (...) 表示。如何做到这一点?

最佳答案

将label的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);
    }
}

如果您使用 Image 或 TextAlign 属性,您将需要做更多的工作。

关于c# - 自动指示 ToolTipStatusLabel 中的截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2903172/

相关文章:

c# - 如何复数单词/字符串?

c# - 我应该只移动文件还是创建一个移动它的类?

c# - WP7 : TCP connection from phone to PC via USB

c# - 为什么 C# 预先绑定(bind)局部变量?

c# - 读取文本文件直到一个空格并存储多个值

C# 自动清除 Winform 文本框

c# - 在 WebResourceRequested 事件中为 WebView2 设置 cookie

c# - 通过 BitmapImage 加载某些目录中的图像会导致 E_NETWORK_ERROR

c# - WPF - 为什么 ValidationRule 不是 DependencyObject?

java - FTP 文件服务器上传警报