-
Notifications
You must be signed in to change notification settings - Fork 19
/
VistaMenu.cs
341 lines (270 loc) · 10.7 KB
/
VistaMenu.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
//VistaMenu 1.1, created by Wyatt O'Day
//Visit: http://wyday.com/blog/2008/vista-menu-with-icons-in-c-sharp/
namespace CDBurnerXP.Controls
{
//Properties for the MenuItem
internal class Properties
{
public Image Image = null;
public IntPtr renderBmpHbitmap = IntPtr.Zero;
}
[ProvideProperty("Image", typeof(MenuItem))]
public partial class VistaMenu : System.ComponentModel.Component, IExtenderProvider, ISupportInitialize
{
private Container components = null;
private Hashtable properties = new Hashtable();
private Hashtable menuParents = new Hashtable();
private bool formHasBeenIntialized = false;
private bool isVistaOrLater = false;
#region Imports
public const int MIIM_BITMAP = 0x00000080;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetMenuItemInfo(HandleRef hMenu, int uItem, bool fByPosition, MENUITEMINFO_T_RW lpmii);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetMenuInfo(HandleRef hMenu, MENUINFO lpcmi);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
#endregion
public VistaMenu()
{
isVistaOrLater = Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6;
InitializeComponent();
}
public VistaMenu(System.ComponentModel.IContainer container)
: this()
{
container.Add(this);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
//release all the HBitmap handles created
foreach (DictionaryEntry de in properties)
{
if (((Properties)de.Value).renderBmpHbitmap != IntPtr.Zero)
DeleteObject(((Properties)de.Value).renderBmpHbitmap);
}
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
bool IExtenderProvider.CanExtend(object o)
{
if (o is MenuItem || o is Form)
return true;
else
return false;
}
private Properties EnsurePropertiesExists(MenuItem key)
{
Properties p = (Properties)properties[key];
if (p == null)
{
p = new Properties();
properties[key] = p;
}
return p;
}
#region MenuItem.Image
[Description("The Image for the MenuItem")]
[Category("Appearance")]
public Image GetImage(MenuItem mnuItem)
{
return EnsurePropertiesExists(mnuItem).Image;
}
public void SetImage(MenuItem mnuItem, Image value)
{
Properties prop = EnsurePropertiesExists(mnuItem);
prop.Image = value;
if (!DesignMode && isVistaOrLater)
{
//Destroy old bitmap object
if (prop.renderBmpHbitmap != IntPtr.Zero)
{
DeleteObject(prop.renderBmpHbitmap);
prop.renderBmpHbitmap = IntPtr.Zero;
}
//if there's no Image, then just bail out
if (value == null)
return;
//convert to 32bppPArgb (the 'P' means The red, green, and blue components are premultiplied, according to the alpha component.)
Bitmap renderBmp = new Bitmap(value.Width, value.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics g = Graphics.FromImage(renderBmp);
g.DrawImage(value, 0, 0, value.Width, value.Height);
g.Dispose();
prop.renderBmpHbitmap = renderBmp.GetHbitmap(Color.FromArgb(0, 0, 0, 0));
renderBmp.Dispose();
if (formHasBeenIntialized)
{
AddVistaMenuItem(mnuItem);
}
}
//for every Pre-Vista Windows, add the parent of the menu item to the list of parents
if (!DesignMode && !isVistaOrLater && formHasBeenIntialized)
{
AddPreVistaMenuItem(mnuItem);
}
}
private bool ShouldSerializeImage(MenuItem mnuItem)
{
if (GetImage(mnuItem) != null)
return true;
else
return false;
}
private void ResetImage(MenuItem mnuItem)
{
SetImage(mnuItem, null);
}
#endregion
void ISupportInitialize.BeginInit()
{
}
MENUINFO mnuInfo = new MENUINFO();
void AddVistaMenuItem(MenuItem mnuItem)
{
//get the bitmap children of the parent
List<MenuItem> mnuBitmapChildren = (List<MenuItem>)menuParents[mnuItem.Parent.Handle];
if (mnuBitmapChildren == null)
{
if (mnuItem.Parent.GetType() == typeof(ContextMenu))
((ContextMenu)mnuItem.Parent).Popup += new EventHandler(MenuItem_Popup);
else
((MenuItem)mnuItem.Parent).Popup += new EventHandler(MenuItem_Popup);
//intialize all the topmost menus to be of type "MNS_CHECKORBMP" (for Vista classic theme)
SetMenuInfo(new HandleRef(null, mnuItem.Parent.Handle), mnuInfo);
mnuBitmapChildren = new List<MenuItem>();
mnuBitmapChildren.Add(mnuItem);
//set the new children list to the corresponding parent
menuParents[mnuItem.Parent.Handle] = mnuBitmapChildren;
}
else
{
mnuBitmapChildren.Add(mnuItem);
}
}
void AddPreVistaMenuItem(MenuItem mnuItem)
{
if (menuParents[mnuItem.Parent] == null)
{
if (mnuItem.Parent.GetType() == typeof(ContextMenu))
((ContextMenu)mnuItem.Parent).Popup += new EventHandler(PreVistaMenuItem_Popup);
else
((MenuItem)mnuItem.Parent).Popup += new EventHandler(PreVistaMenuItem_Popup);
menuParents[mnuItem.Parent] = true;
if (formHasBeenIntialized)
{
//add all the menu items with custom paint events
foreach (MenuItem menu in mnuItem.Parent.MenuItems)
{
menu.DrawItem += new DrawItemEventHandler(MenuItem_DrawItem);
menu.MeasureItem += new MeasureItemEventHandler(MenuItem_MeasureItem);
menu.OwnerDraw = true;
}
}
}
}
void ISupportInitialize.EndInit()
{
if (!DesignMode)
{
if (isVistaOrLater)
{
foreach (DictionaryEntry de in properties)
{
AddVistaMenuItem((MenuItem)de.Key);
}
}
else // Pre-Vista menus
{
foreach (DictionaryEntry de in properties)
{
AddPreVistaMenuItem((MenuItem)de.Key);
}
//add event handle for each menu item's measure & draw routines
foreach (DictionaryEntry parent in menuParents)
{
foreach (MenuItem mnuItem in ((Menu)parent.Key).MenuItems)
{
mnuItem.DrawItem += new DrawItemEventHandler(MenuItem_DrawItem);
mnuItem.MeasureItem += new MeasureItemEventHandler(MenuItem_MeasureItem);
mnuItem.OwnerDraw = true;
}
}
}
formHasBeenIntialized = true;
}
}
void MenuItem_Popup(object sender, EventArgs e)
{
//get the parentHandle
IntPtr parentHandle = ((Menu)sender).Handle;
//get the list of children menuitems to "refresh"
List<MenuItem> mnuBitmapChildren = (List<MenuItem>)menuParents[parentHandle];
MENUITEMINFO_T_RW menuItemInfo = new MENUITEMINFO_T_RW();
menuItemInfo.fMask = MIIM_BITMAP;
foreach (MenuItem menuItem in mnuBitmapChildren)
{
//menuItem.
menuItemInfo.hbmpItem = ((Properties)properties[menuItem]).renderBmpHbitmap;
//refresh the menu item
SetMenuItemInfo(new HandleRef(null, parentHandle),
(int)typeof(MenuItem).InvokeMember("MenuID", BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty, null, menuItem, null),
false,
menuItemInfo);
}
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MENUITEMINFO_T_RW
{
public int cbSize = Marshal.SizeOf(typeof(MENUITEMINFO_T_RW));
public int fMask;
public int fType;
public int fState;
public int wID;
public IntPtr hSubMenu = IntPtr.Zero;
public IntPtr hbmpChecked = IntPtr.Zero;
public IntPtr hbmpUnchecked = IntPtr.Zero;
public IntPtr dwItemData = IntPtr.Zero;
public IntPtr dwTypeData = IntPtr.Zero;
public int cch;
public IntPtr hbmpItem = IntPtr.Zero;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MENUINFO
{
public int cbSize = Marshal.SizeOf(typeof(MENUINFO));
public int fMask = 0x00000010; //MIM_STYLE;
public int dwStyle = 0x04000000; //MNS_CHECKORBMP;
public uint cyMax;
public IntPtr hbrBack = IntPtr.Zero;
public int dwContextHelpID;
public IntPtr dwMenuData = IntPtr.Zero;
}
}