2008-10-24 50 views
1

我需要將ToolStrip中的標準溢出函數替換爲「More ...」按鈕,然後彈出帶有溢出項目的菜單。有沒有人有關於如何做到這一點的任何想法?ToolStrip溢出

回答

2

我之前寫過一些與此類似的東西。我使用的代碼粘貼在下面,您可以自由修改它以滿足您的需求。

ToolStripCustomiseMenuItem基本上是您單擊時填充DropDown上下文菜單的「更多」按鈕。希望這可以幫助你,至少這應該是一個很好的起點...

public class ToolStripCustomiseMenuItem : ToolStripDropDownButton { 
     public ToolStripCustomiseMenuItem() 
      : base("Add Remove Buttons") { 
      this.Overflow = ToolStripItemOverflow.Always; 
      DropDown = CreateCheckImageContextMenuStrip(); 
     } 

    ContextMenuStrip checkImageContextMenuStrip = new ContextMenuStrip(); 
    internal ContextMenuStrip CreateCheckImageContextMenuStrip() { 
     ContextMenuStrip checkImageContextMenuStrip = new ContextMenuStrip(); 
     checkImageContextMenuStrip.ShowCheckMargin = true; 
     checkImageContextMenuStrip.ShowImageMargin = true; 
     checkImageContextMenuStrip.Closing += new ToolStripDropDownClosingEventHandler(checkImageContextMenuStrip_Closing); 
     checkImageContextMenuStrip.Opening += new CancelEventHandler(checkImageContextMenuStrip_Opening); 
     DropDownOpening += new EventHandler(ToolStripAddRemoveMenuItem_DropDownOpening); 
     return checkImageContextMenuStrip; 
    } 

    void checkImageContextMenuStrip_Opening(object sender, CancelEventArgs e) { 

    } 

    void ToolStripAddRemoveMenuItem_DropDownOpening(object sender, EventArgs e) { 
     DropDownItems.Clear(); 
     if (this.Owner == null) return; 
     foreach (ToolStripItem ti in Owner.Items) { 
      if (ti is ToolStripSeparator) continue; 
      if (ti == this) continue; 
      MyToolStripCheckedMenuItem itm = new MyToolStripCheckedMenuItem(ti); 
      itm.Checked = ti.Visible; 
      DropDownItems.Add(itm); 
     } 
    } 

    void checkImageContextMenuStrip_Closing(object sender, ToolStripDropDownClosingEventArgs e) { 
     if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked) { 
      e.Cancel = true; 
     } 
    } 
} 

internal class MyToolStripCheckedMenuItem : ToolStripMenuItem { 
    ToolStripItem tsi; 
    public MyToolStripCheckedMenuItem(ToolStripItem tsi) 
     : base(tsi.Text) { 
     this.tsi = tsi; 
     this.Image = tsi.Image; 
     this.CheckOnClick = true; 
     this.CheckState = CheckState.Checked; 
     CheckedChanged += new EventHandler(MyToolStripCheckedMenuItem_CheckedChanged); 
    } 

    void MyToolStripCheckedMenuItem_CheckedChanged(object sender, EventArgs e) { 
     tsi.Visible = this.Checked; 
    } 

} 
0

您可以捕獲按鈕上的漆事件致電

toolStrip1.OverflowButton.Paint += new PaintEventHandler(OverflowButton_Paint); 

這在理論上應該讓你做它說「更多...」,但我無法設置溢出的寬度按鈕只是(窄)默認寬度。

此外,另一個想法是,您可以在OverflowButton上捕獲VisibleChanged,然後在工具欄中手動插入拆分按鈕。棘手的部分是弄清楚該放哪個按鈕。