-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOffice2007Renderer.cs
1566 lines (1436 loc) · 55 KB
/
Office2007Renderer.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Must call base class, otherwise the subsequent drawing does not appear!
/********************************************************************/
/* Office 2007 Renderer Project */
/* */
/* Use the Office2007Renderer class as a custom renderer by */
/* providing it to the ToolStripManager.Renderer property. Then */
/* all tool strips, menu strips, status strips etc will be drawn */
/* using the Office 2007 style renderer in your application. */
/* */
/* Author: Phil Wright */
/* Website: www.componentfactory.com */
/* Contact: phil.wright@componentfactory.com */
/********************************************************************/
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Office2007Rendering
{
/// <summary>
/// Draw ToolStrips using the Office 2007 themed appearance.
/// </summary>
public class Office2007Renderer : ToolStripProfessionalRenderer
{
#region GradientItemColors
/// <summary>
///
/// </summary>
private class GradientItemColors
{
#region Public Fields
/// <summary>
///
/// </summary>
public Color InsideTop1;
/// <summary>
///
/// </summary>
public Color InsideTop2;
/// <summary>
///
/// </summary>
public Color InsideBottom1;
/// <summary>
///
/// </summary>
public Color InsideBottom2;
/// <summary>
///
/// </summary>
public Color FillTop1;
/// <summary>
///
/// </summary>
public Color FillTop2;
/// <summary>
///
/// </summary>
public Color FillBottom1;
/// <summary>
///
/// </summary>
public Color FillBottom2;
/// <summary>
///
/// </summary>
public Color Border1;
/// <summary>
///
/// </summary>
public Color Border2;
#endregion
#region Identity
/// <summary>
///
/// </summary>
/// <param name="insideTop1"></param>
/// <param name="insideTop2"></param>
/// <param name="insideBottom1"></param>
/// <param name="insideBottom2"></param>
/// <param name="fillTop1"></param>
/// <param name="fillTop2"></param>
/// <param name="fillBottom1"></param>
/// <param name="fillBottom2"></param>
/// <param name="border1"></param>
/// <param name="border2"></param>
public GradientItemColors(Color insideTop1, Color insideTop2,
Color insideBottom1, Color insideBottom2,
Color fillTop1, Color fillTop2,
Color fillBottom1, Color fillBottom2,
Color border1, Color border2)
{
InsideTop1 = insideTop1;
InsideTop2 = insideTop2;
InsideBottom1 = insideBottom1;
InsideBottom2 = insideBottom2;
FillTop1 = fillTop1;
FillTop2 = fillTop2;
FillBottom1 = fillBottom1;
FillBottom2 = fillBottom2;
Border1 = border1;
Border2 = border2;
}
#endregion
}
#endregion
#region Static Metrics
private const int _gripOffset = 1;
private const int _gripSquare = 2;
private const int _gripSize = 3;
private const int _gripMove = 4;
private const int _gripLines = 3;
private const int _checkInset = 1;
private const int _marginInset = 2;
private const int _separatorInset = 31;
private const float _cutToolItemMenu = 1.0f;
private const float _cutContextMenu = 0f;
private const float _cutMenuItemBack = 1.2f;
private const float _contextCheckTickThickness = 1.6f;
private static readonly Blend _statusStripBlend;
#endregion
#region Static Colors
private static readonly Color _c1 = Color.FromArgb(red: 167, green: 167, blue: 167);
private static readonly Color _c2 = Color.FromArgb(red: 21, green: 66, blue: 139);
private static readonly Color _c3 = Color.FromArgb(red: 76, green: 83, blue: 92);
private static readonly Color _c4 = Color.FromArgb(red: 250, green: 250, blue: 250);
private static readonly Color _c5 = Color.FromArgb(red: 248, green: 248, blue: 248);
private static readonly Color _c6 = Color.FromArgb(red: 243, green: 243, blue: 243);
private static readonly Color _r1 = Color.FromArgb(red: 255, green: 255, blue: 251);
private static readonly Color _r2 = Color.FromArgb(red: 255, green: 249, blue: 227);
private static readonly Color _r3 = Color.FromArgb(red: 255, green: 242, blue: 201);
private static readonly Color _r4 = Color.FromArgb(red: 255, green: 248, blue: 181);
private static readonly Color _r5 = Color.FromArgb(red: 255, green: 252, blue: 229);
private static readonly Color _r6 = Color.FromArgb(red: 255, green: 235, blue: 166);
private static readonly Color _r7 = Color.FromArgb(red: 255, green: 213, blue: 103);
private static readonly Color _r8 = Color.FromArgb(red: 255, green: 228, blue: 145);
private static readonly Color _r9 = Color.FromArgb(red: 160, green: 188, blue: 228);
private static readonly Color _rA = Color.FromArgb(red: 121, green: 153, blue: 194);
private static readonly Color _rB = Color.FromArgb(red: 182, green: 190, blue: 192);
private static readonly Color _rC = Color.FromArgb(red: 155, green: 163, blue: 167);
private static readonly Color _rD = Color.FromArgb(red: 233, green: 168, blue: 97);
private static readonly Color _rE = Color.FromArgb(red: 247, green: 164, blue: 39);
private static readonly Color _rF = Color.FromArgb(red: 246, green: 156, blue: 24);
private static readonly Color _rG = Color.FromArgb(red: 253, green: 173, blue: 17);
private static readonly Color _rH = Color.FromArgb(red: 254, green: 185, blue: 108);
private static readonly Color _rI = Color.FromArgb(red: 253, green: 164, blue: 97);
private static readonly Color _rJ = Color.FromArgb(red: 252, green: 143, blue: 61);
private static readonly Color _rK = Color.FromArgb(red: 255, green: 208, blue: 134);
private static readonly Color _rL = Color.FromArgb(red: 249, green: 192, blue: 103);
private static readonly Color _rM = Color.FromArgb(red: 250, green: 195, blue: 93);
private static readonly Color _rN = Color.FromArgb(red: 248, green: 190, blue: 81);
private static readonly Color _rO = Color.FromArgb(red: 255, green: 208, blue: 49);
private static readonly Color _rP = Color.FromArgb(red: 254, green: 214, blue: 168);
private static readonly Color _rQ = Color.FromArgb(red: 252, green: 180, blue: 100);
private static readonly Color _rR = Color.FromArgb(red: 252, green: 161, blue: 54);
private static readonly Color _rS = Color.FromArgb(red: 254, green: 238, blue: 170);
private static readonly Color _rT = Color.FromArgb(red: 249, green: 202, blue: 113);
private static readonly Color _rU = Color.FromArgb(red: 250, green: 205, blue: 103);
private static readonly Color _rV = Color.FromArgb(red: 248, green: 200, blue: 91);
private static readonly Color _rW = Color.FromArgb(red: 255, green: 218, blue: 59);
private static readonly Color _rX = Color.FromArgb(red: 254, green: 185, blue: 108);
private static readonly Color _rY = Color.FromArgb(red: 252, green: 161, blue: 54);
private static readonly Color _rZ = Color.FromArgb(red: 254, green: 238, blue: 170);
// Color scheme values
private static readonly Color _textDisabled = _c1;
private static readonly Color _textMenuStripItem = _c2;
private static readonly Color _textStatusStripItem = _c2;
private static readonly Color _textContextMenuItem = _c2;
private static readonly Color _arrowDisabled = _c1;
private static readonly Color _arrowLight = Color.FromArgb(red: 106, green: 126, blue: 197);
private static readonly Color _arrowDark = Color.FromArgb(red: 64, green: 70, blue: 90);
private static readonly Color _separatorMenuLight = Color.FromArgb(red: 245, green: 245, blue: 245);
private static readonly Color _separatorMenuDark = Color.FromArgb(red: 197, green: 197, blue: 197);
private static readonly Color _contextMenuBack = _c4;
private static readonly Color _contextCheckBorder = Color.FromArgb(red: 242, green: 149, blue: 54);
private static readonly Color _contextCheckTick = Color.FromArgb(red: 66, green: 75, blue: 138);
private static readonly Color _statusStripBorderDark = Color.FromArgb(red: 86, green: 125, blue: 176);
private static readonly Color _statusStripBorderLight = Color.White;
private static readonly Color _gripDark = Color.FromArgb(red: 114, green: 152, blue: 204);
private static readonly Color _gripLight = _c5;
private static readonly GradientItemColors _itemContextItemEnabledColors = new GradientItemColors(insideTop1: _r1, insideTop2: _r2, insideBottom1: _r3, insideBottom2: _r4, fillTop1: _r5, fillTop2: _r6, fillBottom1: _r7, fillBottom2: _r8, border1: Color.FromArgb(red: 217, green: 203, blue: 150), border2: Color.FromArgb(red: 192, green: 167, blue: 118));
private static readonly GradientItemColors _itemDisabledColors = new GradientItemColors(insideTop1: _c4, insideTop2: _c6, insideBottom1: Color.FromArgb(red: 236, green: 236, blue: 236), insideBottom2: Color.FromArgb(red: 230, green: 230, blue: 230), fillTop1: _c6, fillTop2: Color.FromArgb(red: 224, green: 224, blue: 224), fillBottom1: Color.FromArgb(red: 200, green: 200, blue: 200), fillBottom2: Color.FromArgb(red: 210, green: 210, blue: 210), border1: Color.FromArgb(red: 212, green: 212, blue: 212), border2: Color.FromArgb(red: 195, green: 195, blue: 195));
private static readonly GradientItemColors _itemToolItemSelectedColors = new GradientItemColors(insideTop1: _r1, insideTop2: _r2, insideBottom1: _r3, insideBottom2: _r4, fillTop1: _r5, fillTop2: _r6, fillBottom1: _r7, fillBottom2: _r8, border1: _r9, border2: _rA);
private static readonly GradientItemColors _itemToolItemPressedColors = new GradientItemColors(insideTop1: _rD, insideTop2: _rE, insideBottom1: _rF, insideBottom2: _rG, fillTop1: _rH, fillTop2: _rI, fillBottom1: _rJ, fillBottom2: _rK, border1: _r9, border2: _rA);
private static readonly GradientItemColors _itemToolItemCheckedColors = new GradientItemColors(insideTop1: _rL, insideTop2: _rM, insideBottom1: _rN, insideBottom2: _rO, fillTop1: _rP, fillTop2: _rQ, fillBottom1: _rR, fillBottom2: _rS, border1: _r9, border2: _rA);
private static readonly GradientItemColors _itemToolItemCheckPressColors = new GradientItemColors(insideTop1: _rT, insideTop2: _rU, insideBottom1: _rV, insideBottom2: _rW, fillTop1: _rX, fillTop2: _rI, fillBottom1: _rY, fillBottom2: _rZ, border1: _r9, border2: _rA);
#endregion
#region Identity
/// <summary>
///
/// </summary>
static Office2007Renderer()
{
// One time creation of the blend for the status strip gradient brush
_statusStripBlend = new Blend
{
Positions = new float[] { 0.0f, 0.25f, 0.25f, 0.57f, 0.86f, 1.0f },
Factors = new float[] { 0.1f, 0.6f, 1.0f, 0.4f, 0.0f, 0.95f }
};
}
/// <summary>
/// Initialize a new instance of the Office2007Renderer class.
/// </summary>
public Office2007Renderer() : base(professionalColorTable: new Office2007ColorTable())
{
}
#endregion
#region OnRenderArrow
/// <summary>
/// Raises the RenderArrow event.
/// </summary>
/// <param name="e">An ToolStripArrowRenderEventArgs containing the event data.</param>
protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
{
// Cannot paint a zero sized area
if ((e.ArrowRectangle.Width > 0) && (e.ArrowRectangle.Height > 0))
{
// Create a path that is used to fill the arrow
using (GraphicsPath arrowPath = CreateArrowPath(item: e.Item, rect: e.ArrowRectangle, direction: e.Direction))
{
// Get the rectangle that encloses the arrow and expand slightly
// so that the gradient is always within the expanding bounds
RectangleF boundsF = arrowPath.GetBounds();
boundsF.Inflate(x: 1f, y: 1f);
Color color1 = (e.Item.Enabled ? _arrowLight : _arrowDisabled);
Color color2 = (e.Item.Enabled ? _arrowDark : _arrowDisabled);
float angle = 0;
// Use gradient angle to match the arrow direction
switch (e.Direction)
{
case ArrowDirection.Right:
angle = 0;
break;
case ArrowDirection.Left:
angle = 180f;
break;
case ArrowDirection.Down:
angle = 90f;
break;
case ArrowDirection.Up:
angle = 270f;
break;
}
// Draw the actual arrow using a gradient
using (LinearGradientBrush arrowBrush = new LinearGradientBrush(rect: boundsF, color1: color1, color2: color2, angle: angle))
{
e.Graphics.FillPath(brush: arrowBrush, path: arrowPath);
}
}
}
}
#endregion
#region OnRenderButtonBackground
/// <summary>
/// Raises the RenderButtonBackground event.
/// </summary>
/// <param name="e">An ToolStripItemRenderEventArgs containing the event data.</param>
protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
{
// Cast to correct type
ToolStripButton button = (ToolStripButton)e.Item;
if (button.Selected || button.Pressed || button.Checked)
{
RenderToolButtonBackground(g: e.Graphics, button: button, toolstrip: e.ToolStrip);
}
}
#endregion
#region OnRenderDropDownButtonBackground
/// <summary>
/// Raises the RenderDropDownButtonBackground event.
/// </summary>
/// <param name="e">An ToolStripItemRenderEventArgs containing the event data.</param>
protected override void OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs e)
{
if (e.Item.Selected || e.Item.Pressed)
{
RenderToolDropButtonBackground(g: e.Graphics, item: e.Item, toolstrip: e.ToolStrip);
}
}
#endregion
#region OnRenderItemCheck
/// <summary>
/// Raises the RenderItemCheck event.
/// </summary>
/// <param name="e">An ToolStripItemImageRenderEventArgs containing the event data.</param>
protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e)
{
// Staring size of the checkbox is the image rectangle
Rectangle checkBox = e.ImageRectangle;
// Make the border of the check box 1 pixel bigger on all sides, as a minimum
checkBox.Inflate(width: 1, height: 1);
// Can we extend upwards?
if (checkBox.Top > _checkInset)
{
int diff = checkBox.Top - _checkInset;
checkBox.Y -= diff;
checkBox.Height += diff;
}
// Can we extend downwards?
if (checkBox.Height <= (e.Item.Bounds.Height - (_checkInset * 2)))
{
int diff = e.Item.Bounds.Height - (_checkInset * 2) - checkBox.Height;
checkBox.Height += diff;
}
// Drawing with anti aliasing to create smoother appearance
using (UseAntiAlias uaa = new UseAntiAlias(g: e.Graphics))
{
// Create border path for the check box
using (GraphicsPath borderPath = CreateBorderPath(rect: checkBox, cut: _cutMenuItemBack))
{
// Fill the background in a solid color
using (SolidBrush fillBrush = new SolidBrush(color: ColorTable.CheckBackground))
{
e.Graphics.FillPath(fillBrush, borderPath);
}
// Draw the border around the check box
using (Pen borderPen = new Pen(color: _contextCheckBorder))
{
e.Graphics.DrawPath(borderPen, borderPath);
}
// If there is not an image, then we can draw the tick, square etc...
if (e.Image != null)
{
CheckState checkState = CheckState.Unchecked;
// Extract the check state from the item
if (e.Item is ToolStripMenuItem item) // old if (e.Item is ToolStripMenuItem)
{
//old: ToolStripMenuItem item = (ToolStripMenuItem)e.Item;
checkState = item.CheckState;
}
// Decide what graphic to draw
switch (checkState)
{
case CheckState.Checked:
// Create a path for the tick
using (GraphicsPath tickPath = CreateTickPath(rect: checkBox))
{
// Draw the tick with a thickish brush
using (Pen tickPen = new Pen(color: _contextCheckTick, width: _contextCheckTickThickness))
{
e.Graphics.DrawPath(tickPen, tickPath);
}
}
break;
case CheckState.Indeterminate:
// Create a path for the indeterminate diamond
using (GraphicsPath tickPath = CreateIndeterminatePath(rect: checkBox))
{
// Draw the tick with a thickish brush
using (SolidBrush tickBrush = new SolidBrush(color: _contextCheckTick))
{
e.Graphics.FillPath(brush: tickBrush, path: tickPath);
}
}
break;
}
}
}
}
}
#endregion
#region OnRenderItemText
/// <summary>
/// Raises the RenderItemText event.
/// </summary>
/// <param name="e">An ToolStripItemTextRenderEventArgs containing the event data.</param>
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
if ((e.ToolStrip is MenuStrip)
|| (e.ToolStrip is ToolStrip)
|| (e.ToolStrip is ContextMenuStrip)
|| (e.ToolStrip is ToolStripDropDownMenu))
{
// We set the color depending on the enabled state
if (!e.Item.Enabled)
{
e.TextColor = _textDisabled;
}
else
{
if ((e.ToolStrip is MenuStrip) && !e.Item.Pressed && !e.Item.Selected)
{
e.TextColor = _textMenuStripItem;
}
else if ((e.ToolStrip is StatusStrip) && !e.Item.Pressed && !e.Item.Selected)
{
e.TextColor = _textStatusStripItem;
}
else
{
e.TextColor = _textContextMenuItem;
}
}
// All text is draw using the ClearTypeGridFit text rendering hint
using (UseClearTypeGridFit clearTypeGridFit = new UseClearTypeGridFit(g: e.Graphics))
{
base.OnRenderItemText(e);
}
}
else
{
base.OnRenderItemText(e);
}
}
#endregion
#region OnRenderItemImage
/// <summary>
/// Raises the RenderItemImage event.
/// </summary>
/// <param name="e">An ToolStripItemImageRenderEventArgs containing the event data.</param>
protected override void OnRenderItemImage(ToolStripItemImageRenderEventArgs e)
{
// We only override the image drawing for context menus
if ((e.ToolStrip is ContextMenuStrip)
|| (e.ToolStrip is ToolStripDropDownMenu))
{
if (e.Image != null)
{
if (e.Item.Enabled)
{
e.Graphics.DrawImage(e.Image, e.ImageRectangle);
}
else
{
ControlPaint.DrawImageDisabled(graphics: e.Graphics, image: e.Image, x: e.ImageRectangle.X, y: e.ImageRectangle.Y, background: Color.Transparent);
}
}
}
else
{
base.OnRenderItemImage(e);
}
}
#endregion
#region OnRenderMenuItemBackground
/// <summary>
/// Raises the RenderMenuItemBackground event.
/// </summary>
/// <param name="e">An ToolStripItemRenderEventArgs containing the event data.</param>
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
if ((e.ToolStrip is MenuStrip)
|| (e.ToolStrip is ContextMenuStrip)
|| (e.ToolStrip is ToolStripDropDownMenu))
{
if ((e.Item.Pressed) && (e.ToolStrip is MenuStrip))
{
// Draw the menu/tool strip as a header for a context menu item
DrawContextMenuHeader(g: e.Graphics, item: e.Item);
}
else
{
// We only draw a background if the item is selected and enabled
if (e.Item.Selected)
{
if (e.Item.Enabled)
{
// Do we draw as a menu strip or context menu item?
if (e.ToolStrip is MenuStrip)
{
DrawGradientToolItem(g: e.Graphics, item: e.Item, colors: _itemToolItemSelectedColors);
}
else
{
DrawGradientContextMenuItem(g: e.Graphics, item: e.Item, colors: _itemContextItemEnabledColors);
}
}
else
{
// Get the mouse position in tool strip coordinates
Point mousePos = e.ToolStrip.PointToClient(p: Control.MousePosition);
// If the mouse is not in the item area, then draw disabled
if (!e.Item.Bounds.Contains(pt: mousePos))
{
// Do we draw as a menu strip or context menu item?
if (e.ToolStrip is MenuStrip)
{
DrawGradientToolItem(g: e.Graphics, item: e.Item, colors: _itemDisabledColors);
}
else
{
DrawGradientContextMenuItem(g: e.Graphics, item: e.Item, colors: _itemDisabledColors);
}
}
}
}
}
}
else
{
base.OnRenderMenuItemBackground(e);
}
}
#endregion
#region OnRenderSplitButtonBackground
/// <summary>
/// Raises the RenderSplitButtonBackground event.
/// </summary>
/// <param name="e">An ToolStripItemRenderEventArgs containing the event data.</param>
protected override void OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs e)
{
if (e.Item.Selected || e.Item.Pressed)
{
// Cast to correct type
ToolStripSplitButton splitButton = (ToolStripSplitButton)e.Item;
// Draw the border and background
RenderToolSplitButtonBackground(g: e.Graphics, splitButton: splitButton, toolstrip: e.ToolStrip);
// Get the rectangle that needs to show the arrow
Rectangle arrowBounds = splitButton.DropDownButtonBounds;
// Draw the arrow on top of the background
OnRenderArrow(new ToolStripArrowRenderEventArgs(g: e.Graphics,
toolStripItem: splitButton,
arrowRectangle: arrowBounds,
arrowColor: SystemColors.ControlText,
arrowDirection: ArrowDirection.Down));
}
else
{
base.OnRenderSplitButtonBackground(e);
}
}
#endregion
#region OnRenderStatusStripSizingGrip
/// <summary>
/// Raises the RenderStatusStripSizingGrip event.
/// </summary>
/// <param name="e">An ToolStripRenderEventArgs containing the event data.</param>
protected override void OnRenderStatusStripSizingGrip(ToolStripRenderEventArgs e)
{
using (SolidBrush darkBrush = new SolidBrush(_gripDark),
lightBrush = new SolidBrush(_gripLight))
{
// Do we need to invert the drawing edge?
bool rtl = (e.ToolStrip.RightToLeft == RightToLeft.Yes);
// Find vertical position of the lowest grip line
int y = e.AffectedBounds.Bottom - (_gripSize * 2) + 1;
// Draw three lines of grips
for (int i = _gripLines; i >= 1; i--)
{
// Find the rightmost grip position on the line
int x = (rtl ? e.AffectedBounds.Left + 1 :
e.AffectedBounds.Right - (_gripSize * 2) + 1);
// Draw grips from right to left on line
for (int j = 0; j < i; j++)
{
// Just the single grip glyph
DrawGripGlyph(g: e.Graphics, x: x, y: y, darkBrush: darkBrush, lightBrush: lightBrush);
// Move left to next grip position
x -= (rtl ? -_gripMove : _gripMove);
}
// Move upwards to next grip line
y -= _gripMove;
}
}
}
#endregion
#region OnRenderToolStripContentPanelBackground
/// <summary>
/// Raises the RenderToolStripContentPanelBackground event.
/// </summary>
/// <param name="e">An ToolStripContentPanelRenderEventArgs containing the event data.</param>
protected override void OnRenderToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs e)
{
// Must call base class, otherwise the subsequent drawing does not appear!
base.OnRenderToolStripContentPanelBackground(e);
// Cannot paint a zero sized area
if ((e.ToolStripContentPanel.Width > 0)
&& (e.ToolStripContentPanel.Height > 0))
{
using (LinearGradientBrush backBrush = new LinearGradientBrush(rect: e.ToolStripContentPanel.ClientRectangle,
color1: ColorTable.ToolStripContentPanelGradientEnd,
color2: ColorTable.ToolStripContentPanelGradientBegin,
angle: 90f))
{
e.Graphics.FillRectangle(brush: backBrush, rect: e.ToolStripContentPanel.ClientRectangle);
}
}
}
#endregion
#region OnRenderSeparator
/// <summary>
/// Raises the RenderSeparator event.
/// </summary>
/// <param name="e">An ToolStripSeparatorRenderEventArgs containing the event data.</param>
protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
{
if ((e.ToolStrip is ContextMenuStrip)
|| (e.ToolStrip is ToolStripDropDownMenu))
{
// Create the light and dark line pens
using (Pen lightPen = new Pen(_separatorMenuLight),
darkPen = new Pen(_separatorMenuDark))
{
DrawSeparator(g: e.Graphics, vertical: e.Vertical, rect: e.Item.Bounds,
lightPen: lightPen, darkPen: darkPen, horizontalInset: _separatorInset,
rtl: e.ToolStrip.RightToLeft == RightToLeft.Yes);
}
}
else if (e.ToolStrip is StatusStrip)
{
// Create the light and dark line pens
using (Pen lightPen = new Pen(color: ColorTable.SeparatorLight),
darkPen = new Pen(color: ColorTable.SeparatorDark))
{
DrawSeparator(g: e.Graphics, vertical: e.Vertical, rect: e.Item.Bounds,
lightPen: lightPen, darkPen: darkPen, horizontalInset: 0, rtl: false);
}
}
else
{
base.OnRenderSeparator(e);
}
}
#endregion
#region OnRenderToolStripBackground
/// <summary>
/// Raises the RenderToolStripBackground event.
/// </summary>
/// <param name="e">An ToolStripRenderEventArgs containing the event data.</param>
protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
{
if ((e.ToolStrip is ContextMenuStrip)
|| (e.ToolStrip is ToolStripDropDownMenu))
{
// Create border and clipping paths
using (GraphicsPath borderPath = CreateBorderPath(rect: e.AffectedBounds, cut: _cutContextMenu),
clipPath = CreateClipBorderPath(rect: e.AffectedBounds, cut: _cutContextMenu))
{
// Clip all drawing to within the border path
using (UseClipping clipping = new UseClipping(g: e.Graphics, path: clipPath))
{
// Create the background brush
using (SolidBrush backBrush = new SolidBrush(color: _contextMenuBack))
{
e.Graphics.FillPath(brush: backBrush, path: borderPath);
}
}
}
}
else if (e.ToolStrip is StatusStrip)
{
// We do not paint the top two pixel lines, so are drawn by the status strip border render method
RectangleF backRect = new RectangleF(x: 0, y: 1.5f, width: e.ToolStrip.Width, height: e.ToolStrip.Height - 2);
// Cannot paint a zero sized area
if ((backRect.Width > 0) && (backRect.Height > 0))
{
using (LinearGradientBrush backBrush = new LinearGradientBrush(rect: backRect,
color1: ColorTable.StatusStripGradientBegin,
color2: ColorTable.StatusStripGradientEnd,
angle: 90f))
{
backBrush.Blend = _statusStripBlend;
e.Graphics.FillRectangle(brush: backBrush, rect: backRect);
}
}
}
else
{
base.OnRenderToolStripBackground(e);
}
}
#endregion
#region OnRenderImageMargin
/// <summary>
/// Raises the RenderImageMargin event.
/// </summary>
/// <param name="e">An ToolStripRenderEventArgs containing the event data.</param>
protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
if ((e.ToolStrip is ContextMenuStrip)
|| (e.ToolStrip is ToolStripDropDownMenu))
{
// Start with the total margin area
Rectangle marginRect = e.AffectedBounds;
// Do we need to draw with separator on the opposite edge?
bool rtl = (e.ToolStrip.RightToLeft == RightToLeft.Yes);
marginRect.Y += _marginInset;
marginRect.Height -= _marginInset * 2;
// Reduce so it is inside the border
if (!rtl)
marginRect.X += _marginInset;
else
marginRect.X += _marginInset / 2;
// Draw the entire margin area in a solid color
using (SolidBrush backBrush = new SolidBrush(color: ColorTable.ImageMarginGradientBegin))
{
e.Graphics.FillRectangle(backBrush, marginRect);
}
// Create the light and dark line pens
using (Pen lightPen = new Pen(color: _separatorMenuLight),
darkPen = new Pen(color: _separatorMenuDark))
{
if (!rtl)
{
// Draw the light and dark lines on the right hand side
e.Graphics.DrawLine(pen: lightPen, x1: marginRect.Right, y1: marginRect.Top, x2: marginRect.Right, y2: marginRect.Bottom);
e.Graphics.DrawLine(pen: darkPen, x1: marginRect.Right - 1, y1: marginRect.Top, x2: marginRect.Right - 1, y2: marginRect.Bottom);
}
else
{
// Draw the light and dark lines on the left hand side
e.Graphics.DrawLine(pen: lightPen, x1: marginRect.Left - 1, y1: marginRect.Top, x2: marginRect.Left - 1, y2: marginRect.Bottom);
e.Graphics.DrawLine(pen: darkPen, x1: marginRect.Left, y1: marginRect.Top, x2: marginRect.Left, y2: marginRect.Bottom);
}
}
}
else
{
base.OnRenderImageMargin(e);
}
}
#endregion
#region OnRenderToolStripBorder
/// <summary>
/// Raises the RenderToolStripBorder event.
/// </summary>
/// <param name="e">An ToolStripRenderEventArgs containing the event data.</param>
protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
if ((e.ToolStrip is ContextMenuStrip)
|| (e.ToolStrip is ToolStripDropDownMenu))
{
// If there is a connected area to be drawn
if (!e.ConnectedArea.IsEmpty)
{
using (SolidBrush excludeBrush = new SolidBrush(color: _contextMenuBack))
{
e.Graphics.FillRectangle(excludeBrush, e.ConnectedArea);
}
}
// Create border and clipping paths
using (GraphicsPath borderPath = CreateBorderPath(e.AffectedBounds, exclude: e.ConnectedArea, cut: _cutContextMenu),
insidePath = CreateInsideBorderPath(rect: e.AffectedBounds, exclude: e.ConnectedArea, cut: _cutContextMenu),
clipPath = CreateClipBorderPath(rect: e.AffectedBounds, exclude: e.ConnectedArea, cut: _cutContextMenu))
{
// Create the different pen colors we need
using (Pen borderPen = new Pen(color: ColorTable.MenuBorder),
insidePen = new Pen(color: _separatorMenuLight))
{
// Clip all drawing to within the border path
using (UseClipping clipping = new UseClipping(g: e.Graphics, path: clipPath))
{
// Drawing with anti aliasing to create smoother appearance
using (UseAntiAlias uaa = new UseAntiAlias(g: e.Graphics))
{
// Draw the inside area first
e.Graphics.DrawPath(insidePen, path: insidePath);
// Draw the border area second, so any overlapping gives it priority
e.Graphics.DrawPath(borderPen, path: borderPath);
}
// Draw the pixel at the bottom right of the context menu
e.Graphics.DrawLine(pen: borderPen, x1: e.AffectedBounds.Right, y1: e.AffectedBounds.Bottom,
x2: e.AffectedBounds.Right - 1, y2: e.AffectedBounds.Bottom - 1);
}
}
}
}
else if (e.ToolStrip is StatusStrip)
{
// Draw two lines at top of the status strip
using (Pen darkBorder = new Pen(color: _statusStripBorderDark),
lightBorder = new Pen(color: _statusStripBorderLight))
{
e.Graphics.DrawLine(pen: darkBorder, x1: 0, y1: 0, x2: e.ToolStrip.Width, y2: 0);
e.Graphics.DrawLine(pen: lightBorder, x1: 0, y1: 1, x2: e.ToolStrip.Width, y2: 1);
}
}
else
{
base.OnRenderToolStripBorder(e);
}
}
#endregion
#region Implementation
/// <summary>
///
/// </summary>
/// <param name="g"></param>
/// <param name="button"></param>
/// <param name="toolstrip"></param>
private void RenderToolButtonBackground(Graphics g,
ToolStripButton button,
ToolStrip toolstrip)
{
// We only draw a background if the item is selected or being pressed
if (button.Enabled)
{
if (button.Checked)
{
if (button.Pressed)
{
DrawGradientToolItem(g: g, item: button, colors: _itemToolItemPressedColors);
}
else if (button.Selected)
{
DrawGradientToolItem(g: g, item: button, colors: _itemToolItemCheckPressColors);
}
else
{
DrawGradientToolItem(g: g, item: button, colors: _itemToolItemCheckedColors);
}
}
else
{
if (button.Pressed)
{
DrawGradientToolItem(g: g, item: button, colors: _itemToolItemPressedColors);
}
else if (button.Selected)
{
DrawGradientToolItem(g: g, item: button, colors: _itemToolItemSelectedColors);
}
}
}
else
{
if (button.Selected)
{
// Get the mouse position in tool strip coordinates
Point mousePos = toolstrip.PointToClient(p: Control.MousePosition);
// If the mouse is not in the item area, then draw disabled
if (!button.Bounds.Contains(pt: mousePos))
{
DrawGradientToolItem(g: g, item: button, colors: _itemDisabledColors);
}
}
}
}
/// <summary>
///
/// </summary>
/// <param name="g"></param>
/// <param name="item"></param>
/// <param name="toolstrip"></param>
private void RenderToolDropButtonBackground(Graphics g,
ToolStripItem item,
ToolStrip toolstrip)
{
// We only draw a background if the item is selected or being pressed
if (item.Selected || item.Pressed)
{
if (item.Enabled)
{
if (item.Pressed)
{
DrawContextMenuHeader(g: g, item: item);
}
else
{
DrawGradientToolItem(g: g, item: item, colors: _itemToolItemSelectedColors);
}
}
else
{
// Get the mouse position in tool strip coordinates
Point mousePos = toolstrip.PointToClient(p: Control.MousePosition);
// If the mouse is not in the item area, then draw disabled
if (!item.Bounds.Contains(pt: mousePos))
{
DrawGradientToolItem(g: g, item: item, colors: _itemDisabledColors);
}
}
}
}
/// <summary>
///
/// </summary>
/// <param name="g"></param>
/// <param name="splitButton"></param>
/// <param name="toolstrip"></param>
private void RenderToolSplitButtonBackground(Graphics g,
ToolStripSplitButton splitButton,
ToolStrip toolstrip)
{
// We only draw a background if the item is selected or being pressed
if (splitButton.Selected || splitButton.Pressed)
{
if (splitButton.Enabled)
{
if (!splitButton.Pressed && splitButton.ButtonPressed)
{
DrawGradientToolSplitItem(g: g, splitButton: splitButton,
colorsButton: _itemToolItemPressedColors,
colorsDrop: _itemToolItemSelectedColors,
colorsSplit: _itemContextItemEnabledColors);
}
else if (splitButton.Pressed && !splitButton.ButtonPressed)
{
DrawContextMenuHeader(g: g, item: splitButton);
}
else
{
DrawGradientToolSplitItem(g: g, splitButton: splitButton,
colorsButton: _itemToolItemSelectedColors,
colorsDrop: _itemToolItemSelectedColors,
colorsSplit: _itemContextItemEnabledColors);
}
}
else
{
// Get the mouse position in tool strip coordinates
Point mousePos = toolstrip.PointToClient(p: Control.MousePosition);
// If the mouse is not in the item area, then draw disabled
if (!splitButton.Bounds.Contains(pt: mousePos))
{
DrawGradientToolItem(g: g, item: splitButton, colors: _itemDisabledColors);
}
}
}
}
/// <summary>
///
/// </summary>
/// <param name="g"></param>
/// <param name="item"></param>
/// <param name="colors"></param>
private void DrawGradientToolItem(Graphics g,
ToolStripItem item,
GradientItemColors colors) =>
// Perform drawing into the entire background of the item
DrawGradientItem(g, new Rectangle(location: Point.Empty, size: item.Bounds.Size), colors);
/// <summary>
///
/// </summary>
/// <param name="g"></param>
/// <param name="splitButton"></param>
/// <param name="colorsButton"></param>
/// <param name="colorsDrop"></param>
/// <param name="colorsSplit"></param>
private void DrawGradientToolSplitItem(Graphics g,
ToolStripSplitButton splitButton,