首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ToolStrip溢出

ToolStrip溢出
EN

Stack Overflow用户
提问于 2008-10-24 20:27:28
回答 2查看 1.1K关注 0票数 1

我需要将ToolStrip中的标准溢出函数替换为"More...“按钮,然后将弹出一个带有溢出项的菜单。有没有人对如何做到这一点有什么想法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2008-10-31 17:52:30

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

ToolStripCustomiseMenuItem基本上就是您的"More“按钮,当单击它时,它会填充DropDown上下文菜单。希望这对你有帮助,至少这应该是一个很好的起点…

代码语言:javascript
复制
  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;
    }

}
票数 2
EN

Stack Overflow用户

发布于 2008-10-24 21:12:23

通过调用,可以捕获按钮上的paint事件

代码语言:javascript
复制
toolStrip1.OverflowButton.Paint += new PaintEventHandler(OverflowButton_Paint);

这在理论上应该可以让它显示“更多...”,但我无法将Overflow Button的宽度设置为(窄的)默认宽度以外的任何值。

此外,另一个想法是,您可以在OverflowButton上捕获VisibleChanged,然后手动将拆分按钮插入到工具条中。棘手的部分是找出把按钮放在哪里。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/235146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档