2012-07-19 64 views
3

我有一個statusstrip與一些項目。其中之一是ToolStripStatusLabelSpring = True。 當標籤的文字太長時,人們看不到它。StatusStrip可以根據物品的尺寸自動改變其高度嗎?

是否有可能使statusstrip變得更高並以多行顯示整個文本?

+1

ToolStripStatusLabel不支持多行文本渲染。你必須創建你自己的派生類。這是不容易得到的,ToolStripItem很難處理,自動調整大小非常複雜。 – 2012-07-19 11:59:56

+0

您可以在尺寸上測量標籤的字符串(其文本),如果它大於控件的寬度,則根據需要增加高度。 [檢查這裏](http://msdn.microsoft.com/en-us/library/aa327655%28v=vs.71%29.aspx) – 2012-07-19 10:08:59

+0

我認爲在這個解決方案的問題將使ToolStripStatusLabel多行...可能是我們需要自己畫上標籤,然後用兩行畫出文字...... – 2012-07-19 10:59:28

回答

3

這是一個有趣的問題....我嘗試了幾件事情,但沒有成功...... basicall ToolStripStatusLabel的能力非常有限。

最後我想一個黑客,讓你想,但我不知道,即使我會推薦這當然除非這是絕對必要的,結果...

這裏是我有...

在您的StatusStrip的屬性中設置AutoSize = false,這將允許調整StatusStrip的大小以容納多行。我假設statusStrip調用ststusStrip1包含名爲toolStripStatusLabel1的標籤。

在形式等級聲明文本框類型的變量:

TextBox txtDummy = new TextBox(); 

在窗體加載設置它的一些屬性:

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:不要指定你的標籤的文本屬性,而是把它放在標籤我們wou LD用它從標籤

toolStripStatusLabel1.Tag = "My very long String"; 

Screenshot

+0

非常感謝!我甚至不希望得到那種答案! – horgh 2012-07-20 02:56:50

+0

我的代碼中只有一件事需要改變。正如我想縮回ToolStrip,如果一些新文本比前者小,我刪除了字符串寬度的檢查,並檢查Tag屬性是否設置爲空(如果是的話)。所以現在ToolStrip會隨時改變它的高度!再次感謝你! – horgh 2012-07-20 05:32:43