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

时间:2012-07-19 08:25:31

标签: c# winforms autosize statusstrip

我有一个包含许多项目的statusstrip。其中一个是ToolStripStatusLabel Spring = True。 当标签的文字太长时,人们看不到它。

是否可以使statusstrip变得更高并在多行显示整个文本?

1 个答案:

答案 0 :(得分:3)

这是一个有趣的问题....我尝试了几件事但没有成功......基本上,ToolStripStatusLabel的功能非常有限。

我最后尝试了一个能够得到你想要的结果的黑客,但我不确定我会推荐这个,除非这当然是绝对必要的......

这是我得到的......

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

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

  TextBox txtDummy = new TextBox();

在Form Load中设置一些属性:

  txtDummy.Multiline = true;
  txtDummy.WordWrap = true;
  txtDummy.Font = toolStripStatusLabel1.Font;//Same font as Label

处理toolStripStatusLabel1的绘制事件

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

IMP:不要分配标签的文字属性,而是将其放在Tag中,我们会在Tag

中使用它
 toolStripStatusLabel1.Tag = "My very long String";

Screenshot