代码之家  ›  专栏  ›  技术社区  ›  harper

在tooltipstatusLabel中自动指示截断

  •  8
  • harper  · 技术社区  · 15 年前

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

    当我调整窗口的大小时,当tooltipstatusLabels无法适应状态跳闸时,它们将被隐藏。当标签无法适应状态时,我希望截短文本。隐藏标签的默认行为使区分空文本或隐藏标签变得困难。

    若要指示文本自动截断,应使用省略号(…)指示。怎么能做到?

    1 回复  |  直到 15 年前
        1
  •  14
  •   Hans Passant    15 年前

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

    如果使用“图像”或“文本对齐”属性,则需要做更多的工作。