StatusStrip 可以根据其项目的大小自动更改其高度吗?

hor*_*rgh 5 c# statusstrip autosize winforms

我有一个包含许多项目的状态条。其中之一是ToolStripStatusLabelwith Spring = True。当标签的文字太长时,人们就看不清它。

是否可以使状态条变高并以多行显示整个文本?

Ari*_*bal 6

这是一个有趣的问题...我尝试了一些方法但没有成功...基本上 ToolStripStatusLabel 的功能非常有限。

我最终尝试了一种破解方法,可以给出您想要的结果,但我什至不确定我会推荐这个,除非这当然是绝对必要的......

这就是我所拥有的...

在 StatusStrip 的属性中设置 AutoSize = false,这是为了允许调整 StatusStrip 的大小以容纳多行。我假设名为 ststusStrip1 的 statusStrip 包含名为 toolStripStatusLabel1 的标签。

在表单级别声明一个 TextBox 类型的变量:

  TextBox txtDummy = new TextBox();
Run Code Online (Sandbox Code Playgroud)

在表单加载时设置其一些属性:

  txtDummy.Multiline = true;
  txtDummy.WordWrap = true;
  txtDummy.Font = toolStripStatusLabel1.Font;//Same font as Label
Run Code Online (Sandbox Code Playgroud)

处理toolStripStatusLabel1的paint事件

 private void toolStripStatusLabel1_Paint(object sender, PaintEventArgs e)
 {        

    String textToPaint = toolStripStatusLabel1.Tag.ToString(); //We take the string to print from Tag
    SizeF stringSize = e.Graphics.MeasureString(textToPaint, toolStripStatusLabel1.Font);
    if (stringSize.Width > toolStripStatusLabel1.Width)//If the size is large we need to find out how many lines it will take
    {
        //We use a textBox to find out the number of lines this text should be broken into
        txtDummy.Width = toolStripStatusLabel1.Width - 10;
        txtDummy.Text = textToPaint;
        int linesRequired = txtDummy.GetLineFromCharIndex(textToPaint.Length - 1) + 1;
        statusStrip1.Height =((int)stringSize.Height * linesRequired) + 5;
        toolStripStatusLabel1.Text = "";
        e.Graphics.DrawString(textToPaint, toolStripStatusLabel1.Font, new SolidBrush( toolStripStatusLabel1.ForeColor), new RectangleF( new PointF(0, 0), new SizeF(toolStripStatusLabel1.Width, toolStripStatusLabel1.Height)));
    }
    else
    {
        toolStripStatusLabel1.Text = textToPaint;
    }
} 
Run Code Online (Sandbox Code Playgroud)

IMP:不要分配标签的文本属性,而是将其放入标签中,我们将从标签中使用它

 toolStripStatusLabel1.Tag = "My very long String";
Run Code Online (Sandbox Code Playgroud)

截屏