using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace Common { public class SplitButton : Button { [DefaultValue(null)] public ContextMenuStrip Menu { get; set; } [DefaultValue(false)] public bool ShowMenuUnderCursor { get; set; } private bool menuShown = false; protected override void OnMouseDown(MouseEventArgs mouseEvent) { if (mouseEvent.X < ClientRectangle.Width - (int) DpiScaling.Scale(20)) { base.OnMouseDown(mouseEvent); } else if (Menu != null && mouseEvent.Button == MouseButtons.Left) { Point menuLocation; if (ShowMenuUnderCursor) { menuLocation = mouseEvent.Location; } else { menuLocation = new Point(0, Height); } if (!menuShown) { menuShown = true; Menu.Show(this, menuLocation); } else { menuShown = false; Menu.Hide(); } } } protected override void OnPaint(PaintEventArgs pevent) { base.OnPaint(pevent); if (Menu == null) return; int arrowX = ClientRectangle.Width - (int)DpiScaling.Scale(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 + (int)DpiScaling.Scale(8), arrowY), new Point(arrowX + (int)DpiScaling.Scale(4), arrowY + (int)DpiScaling.Scale(4)) }; pevent.Graphics.FillPolygon(brush, arrows); } } }