2008-10-24 53 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,然后在工具栏中手动插入拆分按钮。棘手的部分是弄清楚该放哪个按钮。