forked from fabricelacharme/KnobControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnobControl.cs
1309 lines (1077 loc) · 40.7 KB
/
KnobControl.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
#region License
/* Copyright (c) 2017 Fabrice Lacharme
* This code was originally written by Jigar Desai
* http://www.c-sharpcorner.com/article/knob-control-using-windows-forms-and-gdi/
* Note that another implementation exists in vb.net by Blong
* https://www.codeproject.com/Articles/2563/VB-NET-Knob-Control-using-Windows-Forms-and-GDI?msg=1884770#xx1884770xx
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#endregion
#region Contact
/*
* Fabrice Lacharme
* Email: [email protected]
*/
#endregion
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace KnobControl
{
/* Original code from Jigar Desai on C-SharpCorner.com
* see https://www.c-sharpcorner.com/article/knob-control-using-windows-forms-and-gdi/
* KnobControl is a knob control written in C#
*
* CodeProject: https://www.codeproject.com/Tips/1187460/Csharp-Knob-Control-using-Windows-Forms
* Github: https://github.com/fabricelacharme/KnobControl
*
* 22/08/18 - version 1.0.O.1
* Fixed: erroneous display in case of minimum value <> 0 (negative or positive)
* Modified: DrawColorSlider, OnMouseMove
*
* Added: Font selection
*
*
* 25/08/18 - version 1.0.0.2
* Fixed: mouse click event: pointer button is not displayed correctly when the minimum is set to a non zero value.
* Modified: getValueFromPosition
*
*
* 04/01/2019 - version 1.0.0.3
* Font & Size selection for graduations:
* New property ScaleFontAutoSize:
* - false = no AutoSize => Allow font selection
* - true = AutoSize by program
*/
// A delegate type for hooking up ValueChanged notifications.
public delegate void ValueChangedEventHandler(object Sender);
/// <summary>
/// Summary description for KnobControl.
/// </summary>
public class KnobControl : System.Windows.Forms.UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
/// <summary>
/// Styles of pointer button
/// </summary>
public enum KnobPointerStyles
{
circle,
line,
}
private int _step = 1;
#region private properties
private KnobPointerStyles _knobPointerStyle = KnobPointerStyles.circle;
private int _minimum = 0;
private int _maximum = 25;
private int _scaleDivisions;
private int _scaleSubDivisions;
private Font _scaleFont;
private bool _scaleFontAutoSize = true;
private bool _drawDivInside;
private bool _showSmallScale = false;
private bool _showLargeScale = true;
private float _startAngle = 135;
private float _endAngle = 405;
private float deltaAngle;
private int _mouseWheelBarPartitions = 10;
private float drawRatio = 1;
private float gradLength = 4;
// Color of the pointer
private Color _PointerColor = Color.SlateBlue;
private Color _knobBackColor = Color.LightGray;
private Color _scaleColor = Color.Black;
private int _Value = 0;
private int _previousValue = 0;
private float _degrees = 0;
private bool isFocused = false;
private bool isKnobRotating = false;
private Rectangle rKnob;
private Point pKnob;
private Pen DottedPen;
Brush brushKnob;
Brush brushKnobPointer;
private Font knobFont;
//-------------------------------------------------------
// declare Off screen image and Offscreen graphics
//-------------------------------------------------------
private Image OffScreenImage;
private Graphics gOffScreen;
private int _mouseY = -1;
#endregion
#region event
//-------------------------------------------------------
// An event that clients can use to be notified whenever
// the Value is Changed.
//-------------------------------------------------------
public event ValueChangedEventHandler ValueChanged;
//-------------------------------------------------------
// Invoke the ValueChanged event; called when value
// is changed
//-------------------------------------------------------
protected virtual void OnValueChanged(object sender)
{
ValueChanged?.Invoke(sender);
}
#endregion
#region (* public Properties *)
/// <summary>
/// Font of graduations
/// </summary>
[Description("Font of graduations")]
[Category("KnobControl")]
public Font ScaleFont
{
get { return _scaleFont; }
set
{
_scaleFont = value;
// Redraw
SetDimensions();
Invalidate();
}
}
/// <summary>
/// Autosize or not for font of graduations
/// </summary>
[Description("Autosize Font of graduations")]
[Category("KnobControl")]
[DefaultValue(true)]
public bool ScaleFontAutoSize
{
get { return _scaleFontAutoSize; }
set
{
_scaleFontAutoSize = value;
// Redraw
SetDimensions();
Invalidate();
}
}
/// <summary>
/// Start angle to display graduations
/// </summary>
/// <value>The start angle to display graduations.</value>
[Description("Set the start angle to display graduations (min 90)")]
[Category("KnobControl")]
[DefaultValue(135)]
public float StartAngle
{
get { return _startAngle; }
set
{
if (value >= 90 && value < _endAngle)
{
_startAngle = value;
deltaAngle = _endAngle - StartAngle;
// Redraw
Invalidate();
}
}
}
/// <summary>
/// End angle to display graduations
/// </summary>
/// <value>The end angle to display graduations.</value>
[Description("Set the end angle to display graduations (max 450)")]
[Category("KnobControl")]
[DefaultValue(405)]
public float EndAngle
{
get { return _endAngle; }
set
{
if (value <= 450 && value > _startAngle)
{
_endAngle = value;
deltaAngle = _endAngle - _startAngle;
// Redraw
Invalidate();
}
}
}
/// <summary>
/// Style of pointer: circle or line
/// </summary>
[Description("Set the style of the knob pointer: a circle or a line")]
[Category("KnobControl")]
public KnobPointerStyles KnobPointerStyle
{
get { return _knobPointerStyle; }
set
{
_knobPointerStyle = value;
// Redraw
Invalidate();
}
}
/// <summary>
/// Gets or sets the mouse wheel bar partitions.
/// </summary>
/// <value>The mouse wheel bar partitions.</value>
/// <exception cref="T:System.ArgumentOutOfRangeException">exception thrown when value isn't greather than zero</exception>
[Description("Set to how many parts is bar divided when using mouse wheel")]
[Category("KnobControl")]
[DefaultValue(10)]
public int MouseWheelBarPartitions
{
get { return _mouseWheelBarPartitions; }
set
{
if (value > 0)
_mouseWheelBarPartitions = value;
else throw new ArgumentOutOfRangeException("MouseWheelBarPartitions has to be greather than zero");
}
}
/// <summary>
/// Draw string graduations inside or outside knob circle
/// </summary>
///
[Description("Draw graduation strings inside or outside the knob circle")]
[Category("KnobControl")]
[DefaultValue(false)]
public bool DrawDivInside
{
get { return _drawDivInside; }
set
{
_drawDivInside = value;
// Redraw
SetDimensions();
Invalidate();
}
}
/// <summary>
/// Color of graduations
/// </summary>
[Description("Color of graduations")]
[Category("KnobControl")]
public Color ScaleColor
{
get { return _scaleColor; }
set
{
_scaleColor = value;
// Redraw
Invalidate();
}
}
/// <summary>
/// Color of graduations
/// </summary>
[Description("Color of knob")]
[Category("KnobControl")]
public Color KnobBackColor
{
get { return _knobBackColor; }
set
{
_knobBackColor = value;
SetDimensions();
// Redraw
Invalidate();
}
}
/// <summary>
/// How many divisions of maximum?
/// </summary>
[Description("Set the number of intervals between minimum and maximum")]
[Category("KnobControl")]
public int ScaleDivisions
{
get { return _scaleDivisions; }
set
{
if (value > 1)
{
_scaleDivisions = value;
// Redraw
Invalidate();
}
}
}
/// <summary>
/// How many subdivisions for each division
/// </summary>
[Description("Set the number of subdivisions between main divisions of graduation.")]
[Category("KnobControl")]
public int ScaleSubDivisions
{
get { return _scaleSubDivisions; }
set
{
if (value > 0 && _scaleDivisions > 0 && (_maximum - _minimum) / (value * _scaleDivisions) > 0)
{
_scaleSubDivisions = value;
// Redraw
Invalidate();
}
}
}
/// <summary>
/// Shows Small Scale marking.
/// </summary>
[Description("Show or hide subdivisions of graduations")]
[Category("KnobControl")]
public bool ShowSmallScale
{
get { return _showSmallScale; }
set
{
if (value == true)
{
if (_scaleDivisions > 0 && _scaleSubDivisions > 0 && (_maximum - _minimum) / (_scaleSubDivisions * _scaleDivisions) > 0)
{
_showSmallScale = value;
// Redraw
Invalidate();
}
}
else
{
_showSmallScale = value;
// Redraw
Invalidate();
}
}
}
/// <summary>
/// Shows Large Scale marking
/// </summary>
[Description("Show or hide graduations")]
[Category("KnobControl")]
public bool ShowLargeScale
{
get { return _showLargeScale; }
set
{
_showLargeScale = value;
// need to redraw
SetDimensions();
// Redraw
Invalidate();
}
}
/// <summary>
/// Minimum Value for knob Control
/// </summary>
[Description("set the minimum value for the knob control")]
[Category("KnobControl")]
public int Minimum
{
get { return _minimum; }
set
{
_minimum = value;
// Redraw
Invalidate();
}
}
/// <summary>
/// Maximum value for knob control
/// </summary>
[Description("set the maximum value for the knob control")]
[Category("KnobControl")]
public int Maximum
{
get { return _maximum; }
set
{
if (value > _minimum)
{
_maximum = value;
if (_scaleSubDivisions > 0 && _scaleDivisions > 0 && (_maximum - _minimum) / (_scaleSubDivisions * _scaleDivisions) <= 0)
{
_showSmallScale = false;
}
SetDimensions();
// Redraw
Invalidate();
}
}
}
/// <summary>
/// Current Value of knob control
/// </summary>
[Description("set the current value of the knob control")]
[Category("KnobControl")]
public int Value
{
get { return _Value; }
set
{
if (value >= _minimum && value <= _maximum)
{
_previousValue = _Value;
_Value = value;
// Redraw
Invalidate();
// call delegate
OnValueChanged(this);
}
}
}
/// <summary>
/// Color of the button
/// </summary>
[Description("set the color of the pointer")]
[Category("KnobControl")]
public Color PointerColor
{
get { return _PointerColor; }
set
{
_PointerColor = value;
SetDimensions();
// Redraw
Invalidate();
}
}
public int Step {
get => _step;
set {
if(_step > 0)
_step = value;
}
}
#endregion properties
public KnobControl()
{
// This call is required by the Windows.Forms Form Designer.
DottedPen = new Pen(Utility.GetDarkColor(this.BackColor, 40))
{
DashStyle = System.Drawing.Drawing2D.DashStyle.Dash,
DashCap = System.Drawing.Drawing2D.DashCap.Flat
};
InitializeComponent();
knobFont = new Font(this.Font.FontFamily, this.Font.Size);
_scaleFont = new Font(this.Font.FontFamily, this.Font.Size);
// Properties initialisation
// "start angle" and "end angle" possible values:
// 90 = bottom (minimum value for "start angle")
// 180 = left
// 270 = top
// 360 = right
// 450 = bottom again (maximum value for "end angle")
// So the couple (90, 450) will give an entire circle and the couple (180, 360) will give half a circle.
_startAngle = 135;
_endAngle = 405;
deltaAngle = _endAngle - _startAngle;
_minimum = 0;
_maximum = 100;
_scaleDivisions = 11;
_scaleSubDivisions = 4;
_mouseWheelBarPartitions = 10;
_scaleColor = Color.Black;
_knobBackColor = Color.White;
SetDimensions();
}
#region override
/// <summary>
/// Paint event: draw all
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
// Set background color of Image...
gOffScreen.Clear(this.BackColor);
// Fill knob Background to give knob effect....
gOffScreen.FillEllipse(brushKnob, rKnob);
// Set antialias effect on
gOffScreen.SmoothingMode = SmoothingMode.AntiAlias;
// Draw border of knob
gOffScreen.DrawEllipse(new Pen(this.BackColor), rKnob);
//if control is focused
if (this.isFocused)
{
gOffScreen.DrawEllipse(DottedPen, rKnob);
}
// DrawPointer
DrawPointer(gOffScreen);
//---------------------------------------------
// draw small and large scale
//---------------------------------------------
DrawDivisions(gOffScreen, rKnob);
// Draw image on screen
g.DrawImage(OffScreenImage, 0, 0);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// Empty To avoid Flickring due do background Drawing.
}
/// <summary>
/// Mouse down event: select control
/// </summary>
/// <param name="e"></param>
protected override void OnMouseDown(MouseEventArgs e)
{
if (Utility.IsPointinRectangle(new Point(e.X, e.Y), rKnob))
{
if (isFocused)
{
// was already selected
// Start Rotation of knob only if it was selected before
isKnobRotating = true;
}
else
{
// Was not selected before => select it
Focus();
isFocused = true;
isKnobRotating = true;
// draw dotted border to show that it is selected
Invalidate();
}
if(e.Button == MouseButtons.Left)
{
_mouseY = e.Y; //store mouse Y coordinate
}
}
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
isFocused = true;
isKnobRotating = true;
}
//----------------------------------------------------------
// we need to override IsInputKey method to allow user to
// use up, down, right and bottom keys other wise using this
// keys will change focus from current object to another
// object on the form
//----------------------------------------------------------
protected override bool IsInputKey(Keys key)
{
switch (key)
{
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Left:
return true;
}
return base.IsInputKey(key);
}
/// <summary>
/// Mouse up event: reset mouse Y coordinate
/// </summary>
/// <param name="e"></param>
protected override void OnMouseUp(MouseEventArgs e)
{
_mouseY = -1;
}
/// <summary>
/// Mouse move event: change value according to mouse Y direction (if moved while left-clicked)
/// </summary>
/// <param name="e"></param>
protected override void OnMouseMove(MouseEventArgs e)
{
//--------------------------------------
// Following Handles Knob Rotating
//--------------------------------------
if (e.Button == MouseButtons.Left && this.isKnobRotating == true && _mouseY != -1)
{
bool goingUp = ((e.Y - _mouseY) < 0) ? true : false; //direction of mouse Y movement
int v = (goingUp) ? _step : -1 * (_step);
SetProperValue(Value + v);
}
}
/// <summary>
/// Mousewheel: change value
/// </summary>
/// <param name="e"></param>
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
if (isFocused && isKnobRotating && Utility.IsPointinRectangle(new Point(e.X, e.Y), rKnob))
{
int v = (e.Delta > 0) ? _step : -1 * (_step);
SetProperValue(Value + v);
// Avoid to send MouseWheel event to the parent container
((HandledMouseEventArgs)e).Handled = true;
}
}
/// <summary>
/// Leave event: disallow knob rotation
/// </summary>
/// <param name="e"></param>
protected override void OnLeave(EventArgs e)
{
// unselect the control (remove dotted border)
isFocused = false;
isKnobRotating = false;
Invalidate();
base.OnLeave(new EventArgs());
}
/// <summary>
/// Key down event: change value
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{
if (isFocused)
{
//--------------------------------------------------------
// Handles knob rotation with up,down,left and right keys
//--------------------------------------------------------
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Right)
{
if (_Value < _maximum) Value = _Value + 1;
this.Refresh();
}
else if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Left)
{
if (_Value > _minimum) Value = _Value - 1;
this.Refresh();
}
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#endregion
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// KnobControl
//
this.ImeMode = System.Windows.Forms.ImeMode.On;
this.Name = "KnobControl";
this.Resize += new System.EventHandler(this.KnobControl_Resize);
}
#endregion
#region Draw
/// <summary>
/// Draw the pointer of the knob (a small button inside the main button)
/// </summary>
/// <param name="Gr"></param>
private void DrawPointer(Graphics Gr)
{
try
{
float radius = (float)(rKnob.Width / 2);
// Draw a line
if (_knobPointerStyle == KnobPointerStyles.line)
{
int l = (int)radius / 2;
int w = l / 4;
Point[] pt = GetKnobLine(Gr, l);
Gr.DrawLine(new Pen(_PointerColor, w), pt[0], pt[1]);
}
else
{
// Draw a circle
int w = 0;
int h = 0;
int l = 0;
string strvalmax = _maximum.ToString();
string strvalmin = _minimum.ToString();
string strval = strvalmax.Length > strvalmin.Length ? strvalmax : strvalmin;
double val = Convert.ToDouble(strval);
String str = String.Format("{0,0:D}", (int)val);
float fSize;
SizeF strsize;
if (_scaleFontAutoSize)
{
// Use font family = _scaleFont, but size = automatic
fSize = (float)(6F * drawRatio);
if (fSize < 6)
fSize = 6;
strsize = Gr.MeasureString(str, new Font(_scaleFont.FontFamily, fSize));
}
else
{
// Use font family = _scaleFont, but size = fixed
fSize = _scaleFont.Size;
strsize = Gr.MeasureString(str, _scaleFont);
}
int strw = (int)strsize.Width;
int strh = (int)strsize.Height;
w = Math.Max(strw, strh);
// radius of small circle
l = (int)radius - w / 2;
h = w;
Point Arrow = this.GetKnobPosition(l - 2); // Remove 2 pixels to offset the small circle inside the knob
// Draw pointer arrow that shows knob position
Rectangle rPointer = new Rectangle(Arrow.X - w / 2, Arrow.Y - w / 2, w, h);
//Utility.DrawInsetCircle(ref Gr, rPointer, new Pen(_PointerColor));
Utility.DrawInsetCircle(ref Gr, rPointer, new Pen(Utility.GetLightColor(_PointerColor, 55)));
Gr.FillEllipse(brushKnobPointer, rPointer);
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
/// <summary>
/// Draw graduations
/// </summary>
/// <param name="Gr"></param>
/// <param name="rc">Knob rectangle</param>
/// <returns></returns>
private bool DrawDivisions(Graphics Gr, RectangleF rc)
{
if (this == null)
return false;
float cx = pKnob.X;
float cy = pKnob.Y;
float w = rc.Width;
float h = rc.Height;
float tx;
float ty;
float incr = Utility.GetRadian((_endAngle - _startAngle) / ((_scaleDivisions - 1) * (_scaleSubDivisions + 1)));
float currentAngle = Utility.GetRadian(_startAngle);
float radius = (float)(rc.Width / 2);
float rulerValue = (float)_minimum;
Font font;
Pen penL = new Pen(_scaleColor, (2 * drawRatio));
Pen penS = new Pen(_scaleColor, (1 * drawRatio));
SolidBrush br = new SolidBrush(_scaleColor);
PointF ptStart = new PointF(0, 0);
PointF ptEnd = new PointF(0, 0);
int n = 0;
if (_showLargeScale)
{
// Size of maxi string
string strvalmax = _maximum.ToString();
string strvalmin = _minimum.ToString();
string strval = strvalmax.Length > strvalmin.Length ? strvalmax : strvalmin;
double val = Convert.ToDouble(strval);
//double val = _maximum;
String str = String.Format("{0,0:D}", (int)val);
float fSize;
SizeF strsize;
if (_scaleFontAutoSize)
{
fSize = (float)(6F * drawRatio);
if (fSize < 6)
fSize = 6;
}
else
{
fSize = _scaleFont.Size;
}
int wmax = 0;
float l = 0;
gradLength = 2 * drawRatio;
for (; n < _scaleDivisions; n++)
{
// draw divisions
ptStart.X = (float)(cx + (radius) * Math.Cos(currentAngle));
ptStart.Y = (float)(cy + (radius) * Math.Sin(currentAngle));
ptEnd.X = (float)(cx + (radius + gradLength) * Math.Cos(currentAngle));
ptEnd.Y = (float)(cy + (radius + gradLength) * Math.Sin(currentAngle));
Gr.DrawLine(penL, ptStart, ptEnd);
//Draw graduation values
val = Math.Round(rulerValue);
str = String.Format("{0,0:D}", (int)val);
// If autosize
if (_scaleFontAutoSize)
strsize = Gr.MeasureString(str, new Font(_scaleFont.FontFamily, fSize));
else
strsize = Gr.MeasureString(str, new Font(_scaleFont.FontFamily, _scaleFont.Size));
if (_drawDivInside)
{
// graduations values inside the knob
l = (int)radius - (wmax / 2) - 2;
tx = (float)(cx + l * Math.Cos(currentAngle));
ty = (float)(cy + l * Math.Sin(currentAngle));
}
else
{
// graduation values outside the knob
//l = (Width / 2) - (wmax / 2) ;
l = radius + gradLength + wmax/2;
tx = (float)(cx + l * Math.Cos(currentAngle));
ty = (float)(cy + l * Math.Sin(currentAngle));
}
rulerValue += (float)((_maximum - _minimum) / (_scaleDivisions - 1));
if (n == _scaleDivisions - 1)
{
break;
}
// Subdivisions
#region SubDivisions
if (_scaleDivisions <= 0)
currentAngle += incr;