用下拉菜单按钮Windows.Forms的(Windows.Forms button with dr

2019-06-24 23:17发布

我正在开发使用.NET的Windows.Forms的简单的C#应用​​程序。 我需要一些按钮,将显示与子类别下拉菜单 - 就像ToolStripMenu,但按钮,你知道的。 我搜索了它,可能没有发现任何变种。

我的问题是:有没有办法做到这一点,也许有些秘密按钮属性,允许附加菜单呢?

任何帮助将不胜感激。

Answer 1:

您可以显示在单击事件的ContextMenuStrip:

private void button1_Click(object sender, EventArgs e) {
  contextMenuStrip1.Show(button1, new Point(0, button1.Height));
}

为了使自己的决定是否显示按钮的上方或下方的菜单,你可以尝试使用此代码,它测量菜单,并确定它是否会被部分离屏幕:

private void button1_Click(object sender, EventArgs e) {
  Point screenPoint = button1.PointToScreen(new Point(button1.Left, button1.Bottom));
  if (screenPoint.Y + contextMenuStrip1.Size.Height > Screen.PrimaryScreen.WorkingArea.Height) {
    contextMenuStrip1.Show(button1, new Point(0, -contextMenuStrip1.Size.Height));
  } else {
    contextMenuStrip1.Show(button1, new Point(0, button1.Height));
  }    
}


Answer 2:

巴顿有向下箭头的是右边,你可以从设计师的设置菜单它:

随着ShowMenuUnderCursor:

菜单按钮类:

public class MenuButton : Button
{
    [DefaultValue(null)]
    public ContextMenuStrip Menu { get; set; }

    [DefaultValue(false)]
    public bool ShowMenuUnderCursor { get; set; }

    protected override void OnMouseDown(MouseEventArgs mevent)
    {
        base.OnMouseDown(mevent);

        if (Menu != null && mevent.Button == MouseButtons.Left)
        {
            Point menuLocation;

            if (ShowMenuUnderCursor)
            {
                menuLocation = mevent.Location;
            }
            else
            {
                menuLocation = new Point(0, Height);
            }

            Menu.Show(this, menuLocation);
        }
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        base.OnPaint(pevent);

        if (Menu != null)
        {
            int arrowX = ClientRectangle.Width - 14;
            int arrowY = ClientRectangle.Height / 2 - 1;

            Brush brush = Enabled ? SystemBrushes.ControlText : SystemBrushes.ControlDark;
            Point[] arrows = new Point[] { new Point(arrowX, arrowY), new Point(arrowX + 7, arrowY), new Point(arrowX + 3, arrowY + 4) };
            pevent.Graphics.FillPolygon(brush, arrows);
        }
    }
}


Answer 3:

扩大@Jaex回答有点以允许分割线,如果配置没有箭头的条件绘制和主按键体单独click事件和菜单箭头。

应当注意的是,为了更好地对准你可以设置button.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

这里是我的轻微改善

public class SplitButton : Button
{
    [DefaultValue(null), Browsable(true),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public ContextMenuStrip Menu { get; set; }

    [DefaultValue(20), Browsable(true),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public int SplitWidth { get; set; }

    public SplitButton() 
    {
        SplitWidth = 20;
    }

    protected override void OnMouseDown(MouseEventArgs mevent)
    {
        var splitRect = new Rectangle(this.Width - this.SplitWidth, 0, this.SplitWidth, this.Height);

        // Figure out if the button click was on the button itself or the menu split
        if (Menu != null && 
            mevent.Button == MouseButtons.Left &&
            splitRect.Contains(mevent.Location) )
        {
            Menu.Show(this, 0, this.Height);    // Shows menu under button
            //Menu.Show(this, mevent.Location); // Shows menu at click location
        }
        else
        {
            base.OnMouseDown(mevent);
        }
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        base.OnPaint(pevent);

        if (this.Menu != null && this.SplitWidth > 0)
        { 
            // Draw the arrow glyph on the right side of the button
            int arrowX = ClientRectangle.Width - 14;
            int arrowY = ClientRectangle.Height / 2 - 1;

            var arrowBrush = Enabled ? SystemBrushes.ControlText : SystemBrushes.ButtonShadow;
            var arrows = new[] { new Point(arrowX, arrowY), new Point(arrowX + 7, arrowY), new Point(arrowX + 3, arrowY + 4) };
            pevent.Graphics.FillPolygon(arrowBrush, arrows);

            // Draw a dashed separator on the left of the arrow
            int lineX = ClientRectangle.Width - this.SplitWidth;
            int lineYFrom = arrowY - 4;
            int lineYTo = arrowY + 8;
            using( var separatorPen = new Pen(Brushes.DarkGray){DashStyle = DashStyle.Dot})
            {
                pevent.Graphics.DrawLine(separatorPen, lineX, lineYFrom, lineX, lineYTo);
            }
        }
    }
}


Answer 4:

易是我们可以做到这一点。 这可能有助于:)

ContextMenuStrip contextMenuStrip1 = new ContextMenuStrip();

        private void button1_Click(object sender, EventArgs e)
        {
            contextMenuStrip1.Items.Clear();
            contextMenuStrip1.Items.Add("item1");
            contextMenuStrip1.Items.Add("item2");

            contextMenuStrip1.Show(button1, new Point(0, button1.Height));
        }

        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "item1")
            {
                MessageBox.Show(e.ClickedItem.Text);
            }
        }


Answer 5:

最简单的办法是使用ToolStripDropDownButton在取消固定ToolStrip中,只显示了一个按钮。 然后你就可以分项添加到它,等等。要做到这一点: - 拖动工具条到你的控制/表单 - 使用的布局助手到DropDownButton添加 - 设置GripStyle为隐藏 - 设置码头,无

其结果是一个独立的工具栏样式的按钮,支持你所描述的下拉行为。



Answer 6:

显示下面的按钮上下文菜单中单击时。



Answer 7:

上述Jaex的菜单按钮类是适合我。 我没有添加逻辑下面进入onmousedown事件使上下文菜单中只会显示,如果我点击箭头。 如果我在较大的部分点击正常的单击事件将被触发。 允许的“默认”点击动作。

if (Menu != null && mevent.Button == MouseButtons.Left)
{
    if (mevent.Location.X >= this.Width - 14)
    {
        System.Drawing.Point menuLocation;

        if (ShowMenuUnderCursor)
        {
            menuLocation = mevent.Location; 
        }
        else
        {
            menuLocation = new System.Drawing.Point(0, Height);
        }

        Menu.Show(this, menuLocation);
    }
}

认为这可能是有用的人。 由于Jaex



Answer 8:

Infragistics的有WinDropDownButton: http://help.infragistics.com/Help/NetAdvantage/WinForms/2012.1/CLR2.0/html/WinDropDownButton_About_WinDropDownButton.html

所以它肯定存在,但你可能不找一个付费的第三方控制。



Answer 9:

我被这个问题摆弄以及发现一个极其简单的解决方案(虽然有点脏,哈克):将一个ComboBoxButton ,使得其显示旁边的按钮的下拉箭头。

然后使用SelectedIndexChanged的的ComboBox来改变Button的行为,或者你要立即做什么。



文章来源: Windows.Forms button with drop-down menu