1 | using System.ComponentModel;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Windows.Forms;
|
---|
4 |
|
---|
5 | namespace Common
|
---|
6 | {
|
---|
7 | public class SplitButton : Button
|
---|
8 | {
|
---|
9 | [DefaultValue(null)]
|
---|
10 | public ContextMenuStrip Menu { get; set; }
|
---|
11 |
|
---|
12 | [DefaultValue(false)]
|
---|
13 | public bool ShowMenuUnderCursor { get; set; }
|
---|
14 |
|
---|
15 | private bool menuShown = false;
|
---|
16 |
|
---|
17 | protected override void OnMouseDown(MouseEventArgs mouseEvent)
|
---|
18 | {
|
---|
19 | if (mouseEvent.X < ClientRectangle.Width - (int) DpiScaling.Scale(20))
|
---|
20 | {
|
---|
21 | base.OnMouseDown(mouseEvent);
|
---|
22 | } else
|
---|
23 | if (Menu != null && mouseEvent.Button == MouseButtons.Left)
|
---|
24 | {
|
---|
25 | Point menuLocation;
|
---|
26 |
|
---|
27 | if (ShowMenuUnderCursor)
|
---|
28 | {
|
---|
29 | menuLocation = mouseEvent.Location;
|
---|
30 | }
|
---|
31 | else
|
---|
32 | {
|
---|
33 | menuLocation = new Point(0, Height);
|
---|
34 | }
|
---|
35 |
|
---|
36 | if (!menuShown)
|
---|
37 | {
|
---|
38 | menuShown = true;
|
---|
39 | Menu.Show(this, menuLocation);
|
---|
40 | }
|
---|
41 | else
|
---|
42 | {
|
---|
43 | menuShown = false;
|
---|
44 | Menu.Hide();
|
---|
45 | }
|
---|
46 |
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected override void OnPaint(PaintEventArgs pevent)
|
---|
51 | {
|
---|
52 | base.OnPaint(pevent);
|
---|
53 |
|
---|
54 | if (Menu == null) return;
|
---|
55 | int arrowX = ClientRectangle.Width - (int)DpiScaling.Scale(14);
|
---|
56 | int arrowY = ClientRectangle.Height / 2 - 1;
|
---|
57 |
|
---|
58 | Brush brush = Enabled ? SystemBrushes.ControlText : SystemBrushes.ControlDark;
|
---|
59 | Point[] arrows = new Point[] {
|
---|
60 | new Point(arrowX, arrowY),
|
---|
61 | new Point(arrowX + (int)DpiScaling.Scale(8), arrowY),
|
---|
62 | new Point(arrowX + (int)DpiScaling.Scale(4), arrowY + (int)DpiScaling.Scale(4))
|
---|
63 | };
|
---|
64 | pevent.Graphics.FillPolygon(brush, arrows);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | } |
---|