Repository navigation
Expand file tree
/
Copy pathCustomControls.cs
More file actions
1984 lines (1695 loc) · 75.3 KB
/
Copy pathCustomControls.cs
File metadata and controls
1984 lines (1695 loc) · 75.3 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace RightClickTools
{
partial class Program
{
// Custom CheckBox with modern appearance
public class CustomCheckBox : CheckBox
{
private bool isHovered = false;
public CustomCheckBox()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
Padding = new Padding(0);
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
isHovered = true;
Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
isHovered = false;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
OnPaintBackground(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
// Calculate checkbox box position
int checkBoxSize = (int)(16 * ScaleFactor);
int cornerRadius = (int)(4 * ScaleFactor);
Rectangle checkBoxRect = new Rectangle(0, (Height - checkBoxSize) / 2, checkBoxSize, checkBoxSize);
if (Checked)
{
Color fillColor = isHovered ? (Dark ? Color.FromArgb(20, 117, 169) : Color.FromArgb(0, 150, 250)) : (Dark ? Color.FromArgb(85, 196, 255) : Color.FromArgb(0, 103, 192));
Color borderColor = isHovered ? (Dark ? Color.FromArgb(0, 150, 250) : Color.FromArgb(0, 150, 250)) : (Dark ? Color.FromArgb(0, 150, 250) : Color.FromArgb(0, 103, 192));
using (GraphicsPath path = GetRoundedRectangle(checkBoxRect, cornerRadius))
{
using (SolidBrush fillBrush = new SolidBrush(fillColor))
{
e.Graphics.FillPath(fillBrush, path);
}
using (Pen borderPen = new Pen(borderColor, 1.6f))
{
e.Graphics.DrawPath(borderPen, path);
}
}
// Draw checkmark
using (Pen checkPen = new Pen((Dark ? Color.Black : Color.White), 2))
{
Point[] checkPoints = new Point[]
{
new Point(checkBoxRect.X + (int)(3 * ScaleFactor), checkBoxRect.Y + (int)(7 * ScaleFactor)),
new Point(checkBoxRect.X + (int)(6 * ScaleFactor), checkBoxRect.Y + (int)(11 * ScaleFactor)),
new Point(checkBoxRect.X + (int)(13 * ScaleFactor), checkBoxRect.Y + (int)(4 * ScaleFactor))
};
e.Graphics.DrawLines(checkPen, checkPoints);
}
}
else
{
Color fillColor = isHovered ? (Dark ? Color.FromArgb(64, 64, 64) : Color.FromArgb(200, 200, 200)) : (Dark ? Color.FromArgb(32, 32, 32) : Color.FromArgb(240, 240, 240));
Color borderColor = isHovered ? (Dark ? Color.FromArgb(120, 120, 120) : Color.FromArgb(140, 140, 140)) : (Dark ? Color.FromArgb(120, 120, 120) : Color.FromArgb(140, 140, 140));
using (GraphicsPath path = GetRoundedRectangle(checkBoxRect, cornerRadius))
{
using (SolidBrush fillBrush = new SolidBrush(fillColor))
{
e.Graphics.FillPath(fillBrush, path);
}
using (Pen borderPen = new Pen(borderColor, 1.6f))
{
e.Graphics.DrawPath(borderPen, path);
}
}
}
// Draw text
int spacing = (int)(6 * ScaleFactor);
Rectangle textRect = new Rectangle(checkBoxSize + spacing, 0, Width - checkBoxSize - spacing, Height);
TextRenderer.DrawText(e.Graphics, Text, Font, textRect, ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}
private GraphicsPath GetRoundedRectangle(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
int diameter = radius * 2;
path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
path.CloseFigure();
return path;
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
}
protected override void OnCheckedChanged(EventArgs e)
{
base.OnCheckedChanged(e);
Invalidate();
}
public override Size GetPreferredSize(Size proposedSize)
{
int checkBoxSize = (int)(16 * ScaleFactor);
int spacing = (int)(6 * ScaleFactor);
Size textSize = TextRenderer.MeasureText(Text, Font);
int height = base.GetPreferredSize(proposedSize).Height;
return new Size(checkBoxSize + spacing + textSize.Width, height);
}
}
// Custom RadioButton with modern appearance and hover effect
public class CustomRadioButton : RadioButton
{
private bool isHovered = false;
public CustomRadioButton()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
Padding = new Padding(0);
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
isHovered = true;
Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
isHovered = false;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
OnPaintBackground(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
int size = (int)(16 * ScaleFactor);
Rectangle circleRect = new Rectangle(0, (Height - size) / 2, size, size);
if (Checked)
{
Color fillColor = isHovered ? (Dark ? Color.FromArgb(20, 117, 169) : Color.FromArgb(0, 150, 250)) : (Dark ? Color.FromArgb(85, 196, 255) : Color.FromArgb(0, 103, 192));
Color borderColor = isHovered ? (Dark ? Color.FromArgb(0, 150, 250) : Color.FromArgb(0, 150, 250)) : (Dark ? Color.FromArgb(0, 150, 250) : Color.FromArgb(0, 103, 192));
e.Graphics.FillEllipse(new SolidBrush(fillColor), circleRect);
e.Graphics.DrawEllipse(new Pen(borderColor, 1.6f), circleRect);
// Draw inner dot
int dotSize = (int)(6 * ScaleFactor);
Rectangle dotRect = new Rectangle(
circleRect.X + (size - dotSize) / 2,
circleRect.Y + (size - dotSize) / 2,
dotSize, dotSize);
e.Graphics.FillEllipse(new SolidBrush(Dark ? Color.Black : Color.White), dotRect);
}
else
{
Color fillColor = isHovered ? (Dark ? Color.FromArgb(64, 64, 64) : Color.FromArgb(200, 200, 200)) : (Dark ? Color.FromArgb(32, 32, 32) : Color.FromArgb(240, 240, 240));
Color borderColor = isHovered ? (Dark ? Color.FromArgb(120, 120, 120) : Color.FromArgb(100, 100, 100)) : (Dark ? Color.FromArgb(120, 120, 120) : Color.FromArgb(140, 140, 140));
e.Graphics.FillEllipse(new SolidBrush(fillColor), circleRect);
e.Graphics.DrawEllipse(new Pen(borderColor, 1.6f), circleRect);
}
// Draw text
int spacing = (int)(6 * ScaleFactor);
Rectangle textRect = new Rectangle(size + spacing, 0, Width - size - spacing, Height);
TextRenderer.DrawText(e.Graphics, Text, Font, textRect, ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}
protected override void OnCheckedChanged(EventArgs e)
{
base.OnCheckedChanged(e);
Invalidate();
}
public override Size GetPreferredSize(Size proposedSize)
{
int size = (int)(16 * ScaleFactor);
int spacing = (int)(6 * ScaleFactor);
Size textSize = TextRenderer.MeasureText(Text, Font);
int height = base.GetPreferredSize(proposedSize).Height;
return new Size(size + spacing + textSize.Width, height);
}
}
public class CustomGroupBox : GroupBox
{
private Color _borderColor = Color.Gray;
private Color _titleColor = Color.Black;
public Color BorderColor
{
get { return _borderColor; }
set
{
_borderColor = value;
Invalidate();
}
}
public Color TitleColor
{
get { return _titleColor; }
set
{
_titleColor = value;
Invalidate();
}
}
public CustomGroupBox()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw | ControlStyles.ContainerControl, true);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
// Clear background
e.Graphics.Clear(BackColor);
// Measure title text
SizeF titleSize = e.Graphics.MeasureString(Text, Font);
int titleHeight = (int)titleSize.Height;
int titleWidth = (int)titleSize.Width;
// Calculate border rectangle (starts below the title)
int borderTop = titleHeight / 2;
Rectangle borderRect = new Rectangle(
0,
borderTop,
Width - 1,
Height - borderTop - 1
);
// Draw border with gap for title
using (Pen borderPen = new Pen(_borderColor, 1))
{
int titleStart = (int)(8 * ScaleFactor);
int titleEnd = titleStart + titleWidth + (int)(4 * ScaleFactor);
// Top line (with gap for title)
e.Graphics.DrawLine(borderPen, 0, borderTop, titleStart, borderTop);
e.Graphics.DrawLine(borderPen, titleEnd, borderTop, Width - 1, borderTop);
// Other sides
e.Graphics.DrawLine(borderPen, 0, borderTop, 0, Height - 1); // Left
e.Graphics.DrawLine(borderPen, Width - 1, borderTop, Width - 1, Height - 1); // Right
e.Graphics.DrawLine(borderPen, 0, Height - 1, Width - 1, Height - 1); // Bottom
}
// Draw title text
if (!string.IsNullOrEmpty(Text))
{
using (SolidBrush titleBrush = new SolidBrush(_titleColor))
{
e.Graphics.DrawString(Text, Font, titleBrush, (int)(8 * ScaleFactor), 0);
}
}
}
}
// Custom ComboBox with dark mode support
public class CustomComboBox : ComboBox
{
private bool _isHovered = false;
public string PlaceholderText { get; set; }
public int HeaderIndex { get; set; } = -1;
[StructLayout(LayoutKind.Sequential)]
private struct TRACKMOUSEEVENT
{
public int cbSize;
public uint dwFlags;
public IntPtr hwndTrack;
public uint dwHoverTime;
}
[DllImport("user32.dll")]
private static extern bool TrackMouseEvent(ref TRACKMOUSEEVENT lpEventTrack);
private const uint TME_LEAVE = 0x00000002;
private const uint TME_CANCEL = 0x80000000;
public CustomComboBox()
{
DropDownStyle = ComboBoxStyle.DropDownList;
BackColor = Color.FromArgb(225, 225, 225);
// Always use custom painting
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
DrawMode = DrawMode.OwnerDrawFixed;
if (Dark)
{
FlatStyle = FlatStyle.Flat;
BackColor = Color.FromArgb(60, 60, 60);
ForeColor = Color.White;
}
DropDownClosed += (s, e) =>
{
_isHovered = false;
Invalidate();
// Force Windows to reset mouse tracking so OnMouseEnter will fire again
TRACKMOUSEEVENT tme = new TRACKMOUSEEVENT();
tme.cbSize = Marshal.SizeOf(typeof(TRACKMOUSEEVENT));
tme.dwFlags = TME_CANCEL | TME_LEAVE;
tme.hwndTrack = Handle;
TrackMouseEvent(ref tme);
// Immediately restart tracking
tme.dwFlags = TME_LEAVE;
TrackMouseEvent(ref tme);
};
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
_isHovered = true;
Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
_isHovered = false;
Invalidate();
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (e.Index < 0) return;
bool isDark = Dark;
bool isHeader = (HeaderIndex >= 0 && e.Index == HeaderIndex);
bool isSelected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
Color backColor;
Color foreColor;
if (isHeader)
{
backColor = isDark ? Color.FromArgb(50, 50, 50) : SystemColors.Control;
foreColor = isDark ? Color.DimGray : SystemColors.GrayText;
}
else if (isDark)
{
backColor = isSelected ? Color.Black : Color.FromArgb(60, 60, 60);
foreColor = Color.White;
}
else
{
backColor = isSelected ? SystemColors.Highlight : SystemColors.Window;
foreColor = isSelected ? SystemColors.HighlightText : SystemColors.WindowText;
}
e.Graphics.FillRectangle(new SolidBrush(backColor), e.Bounds);
e.Graphics.DrawString(Items[e.Index].ToString(), e.Font, new SolidBrush(foreColor), e.Bounds);
}
protected override void OnPaint(PaintEventArgs e)
{
if (Dark)
{
Color bgColor = _isHovered ? Color.Black : Color.FromArgb(60, 60, 60);
// Fill entire control with background
e.Graphics.FillRectangle(new SolidBrush(bgColor), 0, 0, Width, Height);
// Draw thin border
using (Pen borderPen = new Pen(SystemColors.Highlight, 1))
{
e.Graphics.DrawRectangle(borderPen, 0, 0, Width - 1, Height - 1);
}
// Draw text on left side (placeholder if no selection, otherwise selected item)
Rectangle textBounds = new Rectangle(3, 0, Width - 20, Height);
string displayText = (SelectedIndex == -1 && !string.IsNullOrEmpty(PlaceholderText)) ? PlaceholderText : Text;
TextRenderer.DrawText(e.Graphics, displayText, Font, textBounds, Color.White, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
// Draw V-shape arrow to match light mode appearance
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
int arrowSize = 4;
int arrowX = Width - 13;
int arrowY = Height / 2 - 1;
Point[] arrowPoints = new Point[]
{
new Point(arrowX - arrowSize, arrowY - 2),
new Point(arrowX, arrowY + 2),
new Point(arrowX + arrowSize, arrowY - 2)
};
using (Pen arrowPen = new Pen(Color.White, 1.5f))
{
e.Graphics.DrawLines(arrowPen, arrowPoints);
}
}
else if (SelectedIndex == -1 && !string.IsNullOrEmpty(PlaceholderText))
{
// Light mode with placeholder - use custom painting
Color bgColor = _isHovered ? Color.FromArgb(229, 241, 251) : BackColor;
Color fgColor = _isHovered ? Color.Black : ForeColor;
// Fill entire control with background
e.Graphics.FillRectangle(new SolidBrush(bgColor), 0, 0, Width, Height);
// Draw border
ControlPaint.DrawBorder(e.Graphics, ClientRectangle, SystemColors.ControlDark, ButtonBorderStyle.Solid);
// Draw placeholder text
Rectangle textBounds = new Rectangle(3, 0, Width - 20, Height);
TextRenderer.DrawText(e.Graphics, PlaceholderText, Font, textBounds, fgColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
// Draw dropdown arrow
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
int arrowSize = 4;
int arrowX = Width - 13;
int arrowY = Height / 2 - 1;
Point[] arrowPoints = new Point[]
{
new Point(arrowX - arrowSize, arrowY - 2),
new Point(arrowX, arrowY + 2),
new Point(arrowX + arrowSize, arrowY - 2)
};
using (Pen arrowPen = new Pen(fgColor, 1.5f))
{
e.Graphics.DrawLines(arrowPen, arrowPoints);
}
}
else
{
// Light mode without placeholder - paint normally with hover effect
Color bgColor = _isHovered ? Color.FromArgb(229, 241, 251) : BackColor;
Color fgColor = ForeColor;
e.Graphics.FillRectangle(new SolidBrush(bgColor), 0, 0, Width, Height);
// Draw border
ControlPaint.DrawBorder(e.Graphics, ClientRectangle, SystemColors.ControlDark, ButtonBorderStyle.Solid);
// Draw selected text
Rectangle textBounds = new Rectangle(3, 0, Width - 20, Height);
TextRenderer.DrawText(e.Graphics, Text, Font, textBounds, fgColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
// Draw dropdown arrow
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
int arrowSize = 4;
int arrowX = Width - 13;
int arrowY = Height / 2 - 1;
Point[] arrowPoints = new Point[]
{
new Point(arrowX - arrowSize, arrowY - 2),
new Point(arrowX, arrowY + 2),
new Point(arrowX + arrowSize, arrowY - 2)
};
using (Pen arrowPen = new Pen(fgColor, 1.5f))
{
e.Graphics.DrawLines(arrowPen, arrowPoints);
}
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
// For light mode, handle placeholder text
if (!Dark && m.Msg == 0x000F) // WM_PAINT
{
if (SelectedIndex == -1 && !string.IsNullOrEmpty(PlaceholderText))
{
using (Graphics g = CreateGraphics())
{
// Use appropriate background color based on hover state
Color bgColor = _isHovered ? Color.FromArgb(229, 241, 251) : BackColor;
Color fgColor = _isHovered ? Color.Black : ForeColor;
Rectangle textBounds = new Rectangle(3, 0, Width - 20, Height);
TextRenderer.DrawText(g, PlaceholderText, Font, textBounds, fgColor, bgColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}
}
}
}
}
}
public enum ScrollBarOrientation
{
Horizontal,
Vertical
}
public enum UITheme
{
Custom = -1,
VS2019DarkBlue = 0,
VS2019LightBlue = 1
}
public class FlatScrollBar : Control
{
private bool _isDrawing;
private ScrollBarOrientation _barOrientation = ScrollBarOrientation.Vertical;
private ScrollOrientation _scrollOrientation = ScrollOrientation.VerticalScroll;
private Rectangle _rectClickBar;
private Rectangle _rectThumb;
private Rectangle _rectTopArrow;
private Rectangle _rectBottomArrow;
private Rectangle _rectChannel;
private bool _isTopArrowClicked;
private bool _isBottomArrowClicked;
private bool _isTopBarClicked;
private bool _isBottomBarClicked;
private bool _isThumbClicked;
private ScrollBarState _thumbState = ScrollBarState.Normal;
private ScrollBarArrowButtonState _topArrowButtonState = ScrollBarArrowButtonState.UpNormal;
private ScrollBarArrowButtonState _bottomArrowButtonState = ScrollBarArrowButtonState.DownNormal;
private int _minimum;
private int _maximum = 100;
private int _smallChange = 1;
private int _largeChange = 10;
private int _value;
private int _thumbWidth = 10;
private int _thumbHeight;
private int _arrowWidth = 18;
private int _arrowHeight = 18;
private int _thumbBottomLimitBottom;
private int _thumbBottomLimitTop;
private int _thumbTopLimit;
private int _thumbPosition;
private int _trackPosition;
private readonly Timer scrollTimer = new Timer();
private UITheme _theme = UITheme.VS2019LightBlue;
private Color _backColor = Color.FromArgb(225, 225, 225);
private Color _borderColor = Color.FromArgb(225, 225, 225);
private Color _borderColorDisabled = Color.FromArgb(225, 225, 225);
private readonly Color[] _thumbColors = new Color[3];
private readonly Color[] _arrowColors = new Color[3];
private const int SETREDRAW = 11;
private const int MINIMUM_SIZE = 10;
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr wnd, int msg, bool param, int lparam);
public event ScrollEventHandler Scroll;
public FlatScrollBar()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |
ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
SetStyle(ControlStyles.Selectable, false);
SetUpScrollBar();
scrollTimer.Tick += ScrollTimer_Tick;
_thumbColors[0] = Color.FromArgb(194, 195, 201);
_thumbColors[1] = Color.FromArgb(104, 104, 104);
_thumbColors[2] = Color.FromArgb(91, 91, 91);
_arrowColors[0] = Color.FromArgb(134, 137, 153);
_arrowColors[1] = Color.FromArgb(70, 181, 255);
_arrowColors[2] = Color.FromArgb(0, 122, 204);
}
[Category("Layout")]
[Description("Gets or sets the ScrollBar orientation.")]
[DefaultValue(ScrollBarOrientation.Vertical)]
public ScrollBarOrientation Orientation
{
get { return _barOrientation; }
set
{
if (value != _barOrientation)
{
_barOrientation = value;
_scrollOrientation = value == ScrollBarOrientation.Vertical
? ScrollOrientation.VerticalScroll
: ScrollOrientation.HorizontalScroll;
if (DesignMode)
{
Size = new Size(Height, Width);
}
SetUpScrollBar();
}
}
}
[Category("Behavior")]
[Description("Gets or sets the ScrollBar minimum value.")]
[DefaultValue(0)]
public int Minimum
{
get { return _minimum; }
set
{
if (_minimum == value || value < 0 || value >= _maximum)
return;
_minimum = value;
if (_largeChange > _maximum - _minimum)
_largeChange = _maximum - _minimum;
SetUpScrollBar();
if (_value < value)
{
Value = value;
}
else
{
ChangeThumbPosition(GetThumbPosition());
Refresh();
}
}
}
[Category("Behavior")]
[Description("Gets or sets the ScrollBar maximum value.")]
[DefaultValue(100)]
public int Maximum
{
get { return _maximum; }
set
{
if (value == _maximum || value < 1 || value <= _minimum)
return;
_maximum = value;
if (_largeChange > _maximum - _minimum)
_largeChange = _maximum - _minimum;
SetUpScrollBar();
if (_value > _maximum)
{
Value = _maximum;
}
else
{
ChangeThumbPosition(GetThumbPosition());
Refresh();
}
}
}
[Category("Behavior")]
[Description("Gets or sets the ScrollBar small change value.")]
[DefaultValue(1)]
public int SmallChange
{
get { return _smallChange; }
set
{
if (value == _smallChange || value < 1 || value >= _largeChange)
return;
_smallChange = value;
SetUpScrollBar();
}
}
[Category("Behavior")]
[Description("Gets or sets the ScrollBar large change value.")]
[DefaultValue(10)]
public int LargeChange
{
get { return _largeChange; }
set
{
if (value == _largeChange || value < _smallChange || value < 2)
return;
_largeChange = value > _maximum - _minimum ? _maximum - _minimum : value;
SetUpScrollBar();
}
}
[Category("Behavior")]
[Description("Gets or sets the ScrollBar current value.")]
[DefaultValue(0)]
public int Value
{
get { return _value; }
set
{
if (_value == value || value < _minimum || value > _maximum)
return;
_value = value;
ChangeThumbPosition(GetThumbPosition());
OnScroll(new ScrollEventArgs(ScrollEventType.ThumbPosition, -1, _value, _scrollOrientation));
Refresh();
}
}
[Category("Appearance")]
[Description("The theme to apply to the Flat ScrollBar control.")]
[DefaultValue(UITheme.VS2019LightBlue)]
public UITheme Theme
{
get { return _theme; }
set
{
_theme = value;
if (_theme == UITheme.VS2019DarkBlue)
{
_backColor = Color.FromArgb(62, 62, 66);
_borderColor = Color.FromArgb(62, 62, 66);
_borderColorDisabled = Color.FromArgb(62, 62, 66);
_thumbColors[0] = Color.FromArgb(104, 104, 104);
_thumbColors[1] = Color.FromArgb(158, 158, 158);
_thumbColors[2] = Color.FromArgb(239, 235, 239);
_arrowColors[0] = Color.FromArgb(153, 153, 153);
_arrowColors[1] = Color.FromArgb(28, 151, 234);
_arrowColors[2] = Color.FromArgb(0, 122, 204);
}
else if (_theme == UITheme.VS2019LightBlue)
{
_backColor = Color.FromArgb(245, 245, 245);
_borderColor = Color.FromArgb(245, 245, 245);
_borderColorDisabled = Color.FromArgb(245, 245, 245);
_thumbColors[0] = Color.FromArgb(194, 195, 201);
_thumbColors[1] = Color.FromArgb(104, 104, 104);
_thumbColors[2] = Color.FromArgb(91, 91, 91);
_arrowColors[0] = Color.FromArgb(134, 137, 153);
_arrowColors[1] = Color.FromArgb(28, 151, 234);
_arrowColors[2] = Color.FromArgb(0, 122, 204);
}
Invalidate();
}
}
public void BeginUpdate()
{
SendMessage(Handle, SETREDRAW, false, 0);
_isDrawing = true;
}
public void EndUpdate()
{
SendMessage(Handle, SETREDRAW, true, 0);
_isDrawing = false;
SetUpScrollBar();
Refresh();
}
protected virtual void OnScroll(ScrollEventArgs e)
{
Scroll?.Invoke(this, e);
}
protected override void OnPaint(PaintEventArgs e)
{
if (e == null) return;
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
DrawBackground(g, ClientRectangle);
DrawThumb(g, _rectThumb, _thumbState);
DrawArrowButton(g, _rectTopArrow, _topArrowButtonState, true, _barOrientation);
DrawArrowButton(g, _rectBottomArrow, _bottomArrowButtonState, false, _barOrientation);
if (_isTopBarClicked)
{
if (_barOrientation == ScrollBarOrientation.Vertical)
{
_rectClickBar.Y = _thumbTopLimit;
_rectClickBar.Height = _rectThumb.Y - _thumbTopLimit;
}
else
{
_rectClickBar.X = _thumbTopLimit;
_rectClickBar.Width = _rectThumb.X - _thumbTopLimit;
}
}
else if (_isBottomBarClicked)
{
if (_barOrientation == ScrollBarOrientation.Vertical)
{
_rectClickBar.Y = _rectThumb.Bottom + 1;
_rectClickBar.Height = _thumbBottomLimitBottom - _rectClickBar.Y + 1;
}
else
{
_rectClickBar.X = _rectThumb.Right + 1;
_rectClickBar.Width = _thumbBottomLimitBottom - _rectClickBar.X + 1;
}
}
using (Pen p = new Pen(Enabled ? _borderColor : _borderColorDisabled))
{
e.Graphics.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
Focus();
if (e.Button == MouseButtons.Left)
{
Point mouseLocation = e.Location;
if (_rectThumb.Contains(mouseLocation))
{
_isThumbClicked = true;
_thumbPosition = _barOrientation == ScrollBarOrientation.Vertical
? mouseLocation.Y - _rectThumb.Y
: mouseLocation.X - _rectThumb.X;
_thumbState = ScrollBarState.Pressed;
Invalidate(_rectThumb);
}
else if (_rectTopArrow.Contains(mouseLocation))
{
_isTopArrowClicked = true;
_topArrowButtonState = ScrollBarArrowButtonState.UpPressed;
Invalidate(_rectTopArrow);
ProgressThumb(true);
}
else if (_rectBottomArrow.Contains(mouseLocation))
{
_isBottomArrowClicked = true;
_bottomArrowButtonState = ScrollBarArrowButtonState.DownPressed;
Invalidate(_rectBottomArrow);
ProgressThumb(true);
}
else
{
_trackPosition = _barOrientation == ScrollBarOrientation.Vertical ? mouseLocation.Y : mouseLocation.X;
if (_trackPosition < (_barOrientation == ScrollBarOrientation.Vertical ? _rectThumb.Y : _rectThumb.X))
_isTopBarClicked = true;
else
_isBottomBarClicked = true;
ProgressThumb(true);
}
}
else if (e.Button == MouseButtons.Right)
{
_trackPosition = _barOrientation == ScrollBarOrientation.Vertical ? e.Y : e.X;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Left)
{
if (_isThumbClicked)
{
_isThumbClicked = false;
_thumbState = ScrollBarState.Normal;
OnScroll(new ScrollEventArgs(ScrollEventType.EndScroll, -1, _value, _scrollOrientation));
}
else if (_isTopArrowClicked)
{
_isTopArrowClicked = false;
_topArrowButtonState = ScrollBarArrowButtonState.UpNormal;
scrollTimer.Stop();
}
else if (_isBottomArrowClicked)
{
_isBottomArrowClicked = false;
_bottomArrowButtonState = ScrollBarArrowButtonState.DownNormal;
scrollTimer.Stop();
}
else if (_isTopBarClicked)
{
_isTopBarClicked = false;
scrollTimer.Stop();
}
else if (_isBottomBarClicked)
{
_isBottomBarClicked = false;
scrollTimer.Stop();
}
Invalidate();
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
RefreshScrollBar();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
if (_isThumbClicked)
{
int oldValue = _value;
int pos = _barOrientation == ScrollBarOrientation.Vertical ? e.Location.Y : e.Location.X;
if (pos <= (_thumbTopLimit + _thumbPosition))
{
ChangeThumbPosition(_thumbTopLimit);
_value = _minimum;
}
else if (pos >= (_thumbBottomLimitTop + _thumbPosition))
{
ChangeThumbPosition(_thumbBottomLimitTop);
_value = _maximum;
}
else
{
ChangeThumbPosition(pos - _thumbPosition);
int pixelRange, thumbPos, arrowSize;
if (_barOrientation == ScrollBarOrientation.Vertical)
{
pixelRange = Height - (2 * _arrowHeight) - _thumbHeight;
thumbPos = _rectThumb.Y;
arrowSize = _arrowHeight;
}
else
{
pixelRange = Width - (2 * _arrowWidth) - _thumbWidth;
thumbPos = _rectThumb.X;
arrowSize = _arrowWidth;
}
float perc = 0F;
if (pixelRange != 0)
perc = (float)(thumbPos - arrowSize) / (float)pixelRange;
_value = Convert.ToInt32((perc * (_maximum - _minimum)) + _minimum);
}