.net 工具条溢出

.net 工具条溢出,.net,winforms,.net,Winforms,我需要将ToolStrip中的标准溢出功能替换为“更多…”按钮,然后弹出一个包含溢出项的菜单。有人知道如何完成此操作吗?您可以通过调用 toolStrip1.OverflowButton.Paint += new PaintEventHandler(OverflowButton_Paint); 理论上,这应该允许你让它说“更多…”,但我无法将溢出按钮的宽度设置为(窄)默认宽度以外的任何宽度 另外,另一个想法是,您可以在OverflowButton上捕获VisibleChanged,然后手动将拆

我需要将ToolStrip中的标准溢出功能替换为“更多…”按钮,然后弹出一个包含溢出项的菜单。有人知道如何完成此操作吗?

您可以通过调用

toolStrip1.OverflowButton.Paint += new PaintEventHandler(OverflowButton_Paint);
理论上,这应该允许你让它说“更多…”,但我无法将溢出按钮的宽度设置为(窄)默认宽度以外的任何宽度


另外,另一个想法是,您可以在OverflowButton上捕获
VisibleChanged
,然后手动将拆分按钮注入toolstrip。棘手的部分是找出把那个按钮放在哪里。

我不久前写了一些类似的东西。我使用的代码粘贴在下面,您可以自由修改它以满足您的需要

ToolStripCustomiseMenuItem基本上是您的“更多”按钮,单击时填充下拉上下文菜单。希望这对你有帮助,至少这应该是一个很好的起点

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

}