forked from master131/Black-Ops-II-Sound-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToolStripAeroRenderer.cs
555 lines (487 loc) · 20.7 KB
/
ToolStripAeroRenderer.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
// Thanks for fixes:
// * Marco Minerva, jachymko - http://www.codeplex.com/windowsformsaero
// * Ben Ryves - http://www.benryves.com/
//
// ** Note for anyone considering using this: **
//
// A better alternative to using this class is to use the MainMenu and ContextMenu
// controls instead of MenuStrip and ContextMenuStrip, as they provide true native
// rendering. If you require icons, try this:
//
// http://wyday.com/blog/2009/making-the-menus-in-your-net-app-look-professional/
namespace BlackOps2SoundStudio
{
public enum ToolbarTheme
{
Toolbar,
MediaToolbar,
CommunicationsToolbar,
BrowserTabBar,
HelpBar
}
/// <summary>Renders a toolstrip using the UxTheme API via VisualStyleRenderer and a specific style.</summary>
/// <remarks>Perhaps surprisingly, this does not need to be disposable.</remarks>
public class ToolStripAeroRenderer : ToolStripSystemRenderer
{
VisualStyleRenderer renderer;
public ToolStripAeroRenderer(ToolbarTheme theme)
{
Theme = theme;
}
/// <summary>
/// It shouldn't be necessary to P/Invoke like this, however VisualStyleRenderer.GetMargins
/// misses out a parameter in its own P/Invoke.
/// </summary>
static internal class NativeMethods
{
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("uxtheme.dll")]
public extern static int GetThemeMargins(IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, int iPropId, IntPtr rect, out MARGINS pMargins);
}
// See http://msdn2.microsoft.com/en-us/library/bb773210.aspx - "Parts and States"
// Only menu-related parts/states are needed here, VisualStyleRenderer handles most of the rest.
enum MenuParts : int
{
ItemTMSchema = 1,
DropDownTMSchema = 2,
BarItemTMSchema = 3,
BarDropDownTMSchema = 4,
ChevronTMSchema = 5,
SeparatorTMSchema = 6,
BarBackground = 7,
BarItem = 8,
PopupBackground = 9,
PopupBorders = 10,
PopupCheck = 11,
PopupCheckBackground = 12,
PopupGutter = 13,
PopupItem = 14,
PopupSeparator = 15,
PopupSubmenu = 16,
SystemClose = 17,
SystemMaximize = 18,
SystemMinimize = 19,
SystemRestore = 20
}
enum MenuBarStates : int
{
Active = 1,
Inactive = 2
}
enum MenuBarItemStates : int
{
Normal = 1,
Hover = 2,
Pushed = 3,
Disabled = 4,
DisabledHover = 5,
DisabledPushed = 6
}
enum MenuPopupItemStates : int
{
Normal = 1,
Hover = 2,
Disabled = 3,
DisabledHover = 4
}
enum MenuPopupCheckStates : int
{
CheckmarkNormal = 1,
CheckmarkDisabled = 2,
BulletNormal = 3,
BulletDisabled = 4
}
enum MenuPopupCheckBackgroundStates : int
{
Disabled = 1,
Normal = 2,
Bitmap = 3
}
enum MenuPopupSubMenuStates : int
{
Normal = 1,
Disabled = 2
}
enum MarginTypes : int
{
Sizing = 3601,
Content = 3602,
Caption = 3603
}
static readonly int RebarBackground = 6;
Padding GetThemeMargins(IDeviceContext dc, MarginTypes marginType)
{
NativeMethods.MARGINS margins;
try
{
IntPtr hDC = dc.GetHdc();
if (0 == NativeMethods.GetThemeMargins(renderer.Handle, hDC, renderer.Part, renderer.State, (int)marginType, IntPtr.Zero, out margins))
return new Padding(margins.cxLeftWidth, margins.cyTopHeight, margins.cxRightWidth, margins.cyBottomHeight);
return new Padding(0);
}
finally
{
dc.ReleaseHdc();
}
}
private static int GetItemState(ToolStripItem item)
{
bool hot = item.Selected;
if (item.IsOnDropDown)
{
if (item.Enabled)
return hot ? (int)MenuPopupItemStates.Hover : (int)MenuPopupItemStates.Normal;
return hot ? (int)MenuPopupItemStates.DisabledHover : (int)MenuPopupItemStates.Disabled;
}
else
{
if (item.Pressed)
return item.Enabled ? (int)MenuBarItemStates.Pushed : (int)MenuBarItemStates.DisabledPushed;
if (item.Enabled)
return hot ? (int)MenuBarItemStates.Hover : (int)MenuBarItemStates.Normal;
return hot ? (int)MenuBarItemStates.DisabledHover : (int)MenuBarItemStates.Disabled;
}
}
public ToolbarTheme Theme
{
get;
set;
}
private string RebarClass
{
get
{
return SubclassPrefix + "Rebar";
}
}
private string ToolbarClass
{
get
{
return SubclassPrefix + "ToolBar";
}
}
private string MenuClass
{
get
{
return SubclassPrefix + "Menu";
}
}
private string SubclassPrefix
{
get
{
switch (Theme)
{
case ToolbarTheme.MediaToolbar: return "Media::";
case ToolbarTheme.CommunicationsToolbar: return "Communications::";
case ToolbarTheme.BrowserTabBar: return "BrowserTabBar::";
case ToolbarTheme.HelpBar: return "Help::";
default: return string.Empty;
}
}
}
private VisualStyleElement Subclass(VisualStyleElement element)
{
return VisualStyleElement.CreateElement(SubclassPrefix + element.ClassName,
element.Part, element.State);
}
private bool EnsureRenderer()
{
if (!IsSupported)
return false;
if (renderer == null)
renderer = new VisualStyleRenderer(VisualStyleElement.Button.PushButton.Normal);
return true;
}
// Gives parented ToolStrips a transparent background.
protected override void Initialize(ToolStrip toolStrip)
{
if (toolStrip.Parent is ToolStripPanel)
toolStrip.BackColor = Color.Transparent;
base.Initialize(toolStrip);
}
// Using just ToolStripManager.Renderer without setting the Renderer individually per ToolStrip means
// that the ToolStrip is not passed to the Initialize method. ToolStripPanels, however, are. So we can
// simply initialize it here too, and this should guarantee that the ToolStrip is initialized at least
// once. Hopefully it isn't any more complicated than this.
protected override void InitializePanel(ToolStripPanel toolStripPanel)
{
foreach (Control control in toolStripPanel.Controls)
if (control is ToolStrip)
Initialize((ToolStrip)control);
base.InitializePanel(toolStripPanel);
}
protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
if (EnsureRenderer())
{
renderer.SetParameters(MenuClass, (int)MenuParts.PopupBorders, 0);
if (e.ToolStrip.IsDropDown)
{
Region oldClip = e.Graphics.Clip;
// Tool strip borders are rendered *after* the content, for some reason.
// So we have to exclude the inside of the popup otherwise we'll draw over it.
Rectangle insideRect = e.ToolStrip.ClientRectangle;
insideRect.Inflate(-1, -1);
e.Graphics.ExcludeClip(insideRect);
renderer.DrawBackground(e.Graphics, e.ToolStrip.ClientRectangle, e.AffectedBounds);
// Restore the old clip in case the Graphics is used again (does that ever happen?)
e.Graphics.Clip = oldClip;
}
}
else
{
base.OnRenderToolStripBorder(e);
}
}
Rectangle GetBackgroundRectangle(ToolStripItem item)
{
if (!item.IsOnDropDown)
return new Rectangle(new Point(), item.Bounds.Size);
// For a drop-down menu item, the background rectangles of the items should be touching vertically.
// This ensures that's the case.
Rectangle rect = item.Bounds;
// The background rectangle should be inset two pixels horizontally (on both sides), but we have
// to take into account the border.
rect.X = item.ContentRectangle.X + 1;
rect.Width = item.ContentRectangle.Width - 1;
// Make sure we're using all of the vertical space, so that the edges touch.
rect.Y = 0;
return rect;
}
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
if (EnsureRenderer())
{
int partID = e.Item.IsOnDropDown ? (int)MenuParts.PopupItem : (int)MenuParts.BarItem;
renderer.SetParameters(MenuClass, partID, GetItemState(e.Item));
Rectangle bgRect = GetBackgroundRectangle(e.Item);
renderer.DrawBackground(e.Graphics, bgRect, bgRect);
}
else
{
base.OnRenderMenuItemBackground(e);
}
}
protected override void OnRenderToolStripPanelBackground(ToolStripPanelRenderEventArgs e)
{
if (EnsureRenderer())
{
// Draw the background using Rebar & RP_BACKGROUND (or, if that is not available, fall back to
// Rebar.Band.Normal)
if (VisualStyleRenderer.IsElementDefined(VisualStyleElement.CreateElement(RebarClass, RebarBackground, 0)))
{
renderer.SetParameters(RebarClass, RebarBackground, 0);
}
else
{
renderer.SetParameters(RebarClass, 0, 0);
}
if (renderer.IsBackgroundPartiallyTransparent())
renderer.DrawParentBackground(e.Graphics, e.ToolStripPanel.ClientRectangle, e.ToolStripPanel);
renderer.DrawBackground(e.Graphics, e.ToolStripPanel.ClientRectangle);
e.Handled = true;
}
else
{
base.OnRenderToolStripPanelBackground(e);
}
}
// Render the background of an actual menu bar, dropdown menu or toolbar.
protected override void OnRenderToolStripBackground(System.Windows.Forms.ToolStripRenderEventArgs e)
{
if (EnsureRenderer())
{
if (e.ToolStrip.IsDropDown)
{
renderer.SetParameters(MenuClass, (int)MenuParts.PopupBackground, 0);
}
else
{
// It's a MenuStrip or a ToolStrip. If it's contained inside a larger panel, it should have a
// transparent background, showing the panel's background.
if (e.ToolStrip.Parent is ToolStripPanel)
{
// The background should be transparent, because the ToolStripPanel's background will be visible.
// (Of course, we assume the ToolStripPanel is drawn using the same theme, but it's not my fault
// if someone does that.)
return;
}
else
{
// A lone toolbar/menubar should act like it's inside a toolbox, I guess.
// Maybe I should use the MenuClass in the case of a MenuStrip, although that would break
// the other themes...
if (VisualStyleRenderer.IsElementDefined(VisualStyleElement.CreateElement(RebarClass, RebarBackground, 0)))
renderer.SetParameters(RebarClass, RebarBackground, 0);
else
renderer.SetParameters(RebarClass, 0, 0);
}
}
if (renderer.IsBackgroundPartiallyTransparent())
renderer.DrawParentBackground(e.Graphics, e.ToolStrip.ClientRectangle, e.ToolStrip);
renderer.DrawBackground(e.Graphics, e.ToolStrip.ClientRectangle, e.AffectedBounds);
}
else
{
base.OnRenderToolStripBackground(e);
}
}
// The only purpose of this override is to change the arrow colour.
// It's OK to just draw over the default arrow since we also pass down arrow drawing to the system renderer.
protected override void OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs e)
{
if (EnsureRenderer())
{
ToolStripSplitButton sb = (ToolStripSplitButton)e.Item;
base.OnRenderSplitButtonBackground(e);
// It doesn't matter what colour of arrow we tell it to draw. OnRenderArrow will compute it from the item anyway.
OnRenderArrow(new ToolStripArrowRenderEventArgs(e.Graphics, sb, sb.DropDownButtonBounds, Color.Red, ArrowDirection.Down));
}
else
{
base.OnRenderSplitButtonBackground(e);
}
}
Color GetItemTextColor(ToolStripItem item)
{
int partId = item.IsOnDropDown ? (int)MenuParts.PopupItem : (int)MenuParts.BarItem;
renderer.SetParameters(MenuClass, partId, GetItemState(item));
return renderer.GetColor(ColorProperty.TextColor);
}
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
if (EnsureRenderer())
e.TextColor = GetItemTextColor(e.Item);
base.OnRenderItemText(e);
}
protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
if (EnsureRenderer())
{
if (e.ToolStrip.IsDropDown)
{
renderer.SetParameters(MenuClass, (int)MenuParts.PopupGutter, 0);
// The AffectedBounds is usually too small, way too small to look right. Instead of using that,
// use the AffectedBounds but with the right width. Then narrow the rectangle to the correct edge
// based on whether or not it's RTL. (It doesn't need to be narrowed to an edge in LTR mode, but let's
// do that anyway.)
// Using the DisplayRectangle gets roughly the right size so that the separator is closer to the text.
Padding margins = GetThemeMargins(e.Graphics, MarginTypes.Sizing);
int extraWidth = (e.ToolStrip.Width - e.ToolStrip.DisplayRectangle.Width - margins.Left - margins.Right - 1) - e.AffectedBounds.Width;
Rectangle rect = e.AffectedBounds;
rect.Y += 2;
rect.Height -= 4;
int sepWidth = renderer.GetPartSize(e.Graphics, ThemeSizeType.True).Width;
if (e.ToolStrip.RightToLeft == RightToLeft.Yes)
{
rect = new Rectangle(rect.X - extraWidth, rect.Y, sepWidth, rect.Height);
rect.X += sepWidth;
}
else
{
rect = new Rectangle(rect.Width + extraWidth - sepWidth, rect.Y, sepWidth, rect.Height);
}
renderer.DrawBackground(e.Graphics, rect);
}
}
else
{
base.OnRenderImageMargin(e);
}
}
protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
{
if (e.ToolStrip.IsDropDown && EnsureRenderer())
{
renderer.SetParameters(MenuClass, (int)MenuParts.PopupSeparator, 0);
Rectangle rect = new Rectangle(e.ToolStrip.DisplayRectangle.Left, 0, e.ToolStrip.DisplayRectangle.Width, e.Item.Height);
renderer.DrawBackground(e.Graphics, rect, rect);
}
else
{
base.OnRenderSeparator(e);
}
}
protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e)
{
if (EnsureRenderer())
{
Rectangle bgRect = GetBackgroundRectangle(e.Item);
bgRect.Width = bgRect.Height;
// Now, mirror its position if the menu item is RTL.
if (e.Item.RightToLeft == RightToLeft.Yes)
bgRect = new Rectangle(e.ToolStrip.ClientSize.Width - bgRect.X - bgRect.Width, bgRect.Y, bgRect.Width, bgRect.Height);
renderer.SetParameters(MenuClass, (int)MenuParts.PopupCheckBackground, e.Item.Enabled ? (int)MenuPopupCheckBackgroundStates.Normal : (int)MenuPopupCheckBackgroundStates.Disabled);
renderer.DrawBackground(e.Graphics, bgRect);
Rectangle checkRect = e.ImageRectangle;
checkRect.X = bgRect.X + bgRect.Width / 2 - checkRect.Width / 2;
checkRect.Y = bgRect.Y + bgRect.Height / 2 - checkRect.Height / 2;
// I don't think ToolStrip even supports radio box items, so no need to render them.
renderer.SetParameters(MenuClass, (int)MenuParts.PopupCheck, e.Item.Enabled ? (int)MenuPopupCheckStates.CheckmarkNormal : (int)MenuPopupCheckStates.CheckmarkDisabled);
renderer.DrawBackground(e.Graphics, checkRect);
}
else
{
base.OnRenderItemCheck(e);
}
}
protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
{
// The default renderer will draw an arrow for us (the UXTheme API seems not to have one for all directions),
// but it will get the colour wrong in many cases. The text colour is probably the best colour to use.
if (EnsureRenderer())
e.ArrowColor = GetItemTextColor(e.Item);
base.OnRenderArrow(e);
}
protected override void OnRenderOverflowButtonBackground(ToolStripItemRenderEventArgs e)
{
if (EnsureRenderer())
{
// BrowserTabBar::Rebar draws the chevron using the default background. Odd.
string rebarClass = RebarClass;
if (Theme == ToolbarTheme.BrowserTabBar)
rebarClass = "Rebar";
int state = VisualStyleElement.Rebar.Chevron.Normal.State;
if (e.Item.Pressed)
state = VisualStyleElement.Rebar.Chevron.Pressed.State;
else if (e.Item.Selected)
state = VisualStyleElement.Rebar.Chevron.Hot.State;
renderer.SetParameters(rebarClass, VisualStyleElement.Rebar.Chevron.Normal.Part, state);
renderer.DrawBackground(e.Graphics, new Rectangle(Point.Empty, e.Item.Size));
}
else
{
base.OnRenderOverflowButtonBackground(e);
}
}
public bool IsSupported
{
get
{
if (!VisualStyleRenderer.IsSupported)
return false;
// Needs a more robust check. It seems mono supports very different style sets.
return
VisualStyleRenderer.IsElementDefined(
VisualStyleElement.CreateElement("Menu",
(int)MenuParts.BarBackground,
(int)MenuBarStates.Active));
}
}
}
}