-
Notifications
You must be signed in to change notification settings - Fork 14
/
SplitButton.cs
178 lines (150 loc) · 5.39 KB
/
SplitButton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*****************************************************
* WindowsFormsAero
* https://github.com/LorenzCK/WindowsFormsAero
* http://windowsformsaero.codeplex.com
*
* Author: Nicholas Kwan
*****************************************************/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using WindowsFormsAero.Native;
namespace WindowsFormsAero {
/// <summary>
/// A complex button provided with a secondary split push button
/// that open up a context menu.
/// </summary>
/// <remarks>
/// See: http://www.codeproject.com/KB/vista/themedvistacontrols.aspx
/// </remarks>
public class SplitButton : Button {
protected override CreateParams CreateParams {
get {
CreateParams p = base.CreateParams;
p.Style |= (int)(
(IsDefault) ? ButtonStyle.DefaultSplitButton : ButtonStyle.SplitButton
);
return p;
}
}
#region Split Context Menu
/// <summary>
/// Occurs when the split label is clicked.
/// </summary>
[
Description("Occurs when the split button is clicked."),
Category("Action")
]
public event EventHandler<SplitMenuEventArgs> SplitClick;
/// <summary>
/// Occurs when the split label is clicked, but before the associated
/// context menu is displayed by the control.
/// </summary>
[
Description("Occurs when the split label is clicked, but before the associated context menu is displayed."),
Category("Action")
]
public event EventHandler<SplitMenuEventArgs> SplitMenuOpening;
protected virtual void OnSplitClick(SplitMenuEventArgs e) {
SplitClick?.Invoke(this, e);
if (SplitMenu == null && SplitMenuStrip == null)
return;
SplitMenuOpening?.Invoke(this, e);
if (e.PreventOpening)
return;
var pBottomLeft = new System.Drawing.Point(e.DrawArea.Left, e.DrawArea.Bottom);
if (SplitMenu != null) {
SplitMenu.Show(this, pBottomLeft);
}
else if (SplitMenuStrip != null) {
SplitMenuStrip.Width = e.DrawArea.Width;
SplitMenuStrip.Show(this, pBottomLeft);
}
}
/// <summary>
/// Gets or sets the associated context menu that is displayed when the split
/// glyph of the button is clicked.
/// </summary>
[
Description("Sets the context menu that is displayed by clicking on the split button."),
Category("Behavior"),
DefaultValue(null)
]
public ContextMenuStrip SplitMenuStrip { get; set; }
/// <summary>
/// Gets or sets the associated context menu that is displayed when the split
/// glyph of the button is clicked.
/// </summary>
/// <remarks>
/// Exposed for backward compatibility with legacy context menu classes.
/// If both <see cref="SplitMenuStrip"/> and <see cref="SplitMenu"/> are
/// set, the first is preferred.
/// </remarks>
[
Description("Sets the context menu that is displayed by clicking on the split button."),
Category("Behavior"),
DefaultValue(null)
]
public ContextMenu SplitMenu { get; set; }
#endregion Split Context Menu
bool _alignLeft = false;
/// <summary>
/// Gets or sets whether the split button should be aligned on the left side of the button.
/// </summary>
[
Description("Align the split button on the left side of the button."),
Category("Appearance"),
DefaultValue(false)
]
public bool SplitButtonAlignLeft {
get {
return _alignLeft;
}
set {
_alignLeft = value;
UpdateStyle();
}
}
bool _noSplit = false;
/// <summary>
/// Gets or sets whether the split button should be shown or not.
/// </summary>
[
Description("Hide the split button."),
Category("Appearance"),
DefaultValue(false)
]
public bool SplitButtonNoSplit {
get {
return _noSplit;
}
set {
_noSplit = value;
UpdateStyle();
}
}
private void UpdateStyle() {
//TODO: add "no split" style
var info = new ButtonSplitInfo();
info.Mask = ButtonSplitInfo.MaskType.Style;
info.Style = (
(SplitButtonAlignLeft) ? ButtonSplitInfo.SplitStyle.AlignLeft : ButtonSplitInfo.SplitStyle.None
);
using(var hSplitInfo = new StructWrapper<ButtonSplitInfo>(info)) {
Methods.SendMessage(Handle,
(int)WindowMessage.BCM_SETSPLITINFO,
IntPtr.Zero,
hSplitInfo);
}
}
protected override void WndProc(ref Message m) {
if (m.Msg == (int)WindowMessage.BCM_SETDROPDOWNSTATE) {
if (m.WParam.ToInt32() == 1) {
OnSplitClick(new SplitMenuEventArgs(ClientRectangle));
}
}
base.WndProc(ref m);
}
}
}