m0lDaViA

Untitled

Jun 19th, 2023
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 71.30 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6.  
  7. namespace AudioTest.Controls
  8. {
  9. [ToolboxBitmap(typeof(TrackBar))]
  10. [DefaultEvent("Scroll"), DefaultProperty("BarInnerColor")]
  11. public partial class Slider : Control
  12. {
  13. #region Events
  14.  
  15. /// <summary>
  16. /// Fires when Slider position has changed
  17. /// </summary>
  18. [Description("Event fires when the Value property changes")]
  19. [Category("Action")]
  20.  
  21. public event EventHandler ValueChanged;
  22. private event PositionChangeEventDelegate PositionChanged;
  23.  
  24. public event PositionChangeEventDelegate PositionChangeNotify
  25. {
  26. add => PositionChanged += value;
  27. remove => PositionChanged -= value;
  28. }
  29. public delegate void PositionChangeEventDelegate(float fPercent, Slider objSender);
  30.  
  31. /// <summary>
  32. /// Fires when user scrolls the Slider
  33. /// </summary>
  34. [Description("Event fires when the Slider position is changed")]
  35. [Category("Behavior")]
  36. public event ScrollEventHandler Scroll;
  37.  
  38. #endregion
  39.  
  40.  
  41. #region Properties
  42.  
  43. private Rectangle barRect; //bounding rectangle of bar area
  44. private Rectangle barHalfRect;
  45. private Rectangle thumbHalfRect;
  46. private Rectangle elapsedRect; //bounding rectangle of elapsed area
  47.  
  48. // Margin left & right (bottom & Top)
  49. private int OffsetL = 0;
  50. private int OffsetR = 0;
  51. private uint m_nPosition;
  52.  
  53.  
  54.  
  55. public float PositionFloat
  56. {
  57. get => m_nPosition / 100f;
  58. set
  59. {
  60. uint num = (uint)((double)value * 100.0);
  61. if (num < 0U)
  62. num = 0U;
  63. if (num > 100U)
  64. num = 100U;
  65. Position = num;
  66. PositionChanged?.Invoke(Position / 100f, this);
  67. Invalidate();
  68. }
  69. }
  70.  
  71. public uint Position
  72. {
  73. get => m_nPosition;
  74. set
  75. {
  76. if (value < 0U)
  77. value = 0U;
  78. if (value > 100U)
  79. value = 100U;
  80. m_nPosition = value;
  81. PositionChanged?.Invoke(Position / 100f, this);
  82. Invalidate();
  83. }
  84. }
  85.  
  86.  
  87. #region thumb
  88.  
  89. private Rectangle thumbRect; //bounding rectangle of thumb area
  90. /// <summary>
  91. /// Gets the thumb rect. Usefull to determine bounding rectangle when creating custom thumb shape.
  92. /// </summary>
  93. /// <value>The thumb rect.</value>
  94. [Browsable(false)]
  95. public Rectangle ThumbRect
  96. {
  97. get { return thumbRect; }
  98. }
  99.  
  100. private Size _thumbSize = new Size(20, 20);
  101.  
  102. /// <summary>
  103. /// Gets or sets the size of the thumb.
  104. /// </summary>
  105. /// <value>The size of the thumb.</value>
  106. /// <exception cref="T:System.ArgumentOutOfRangeException">exception thrown when value is lower than zero or grather than half of appropiate dimension</exception>
  107. [Description("Set Slider thumb size")]
  108. [Category("ColorSlider")]
  109. [DefaultValue(20)]
  110. public Size ThumbSize
  111. {
  112. get { return _thumbSize; }
  113. set
  114. {
  115. int h = value.Height;
  116. int w = value.Width;
  117. if (h > 0 && w > 0)
  118. {
  119. _thumbSize = new Size(w, h);
  120. }
  121. else
  122. throw new ArgumentOutOfRangeException(
  123. "TrackSize has to be greather than zero and lower than half of Slider width");
  124.  
  125. Invalidate();
  126. }
  127. }
  128.  
  129. private GraphicsPath _thumbCustomShape = null;
  130. /// <summary>
  131. /// Gets or sets the thumb custom shape. Use ThumbRect property to determine bounding rectangle.
  132. /// </summary>
  133. /// <value>The thumb custom shape. null means default shape</value>
  134. [Description("Set Slider's thumb's custom shape")]
  135. [Category("ColorSlider")]
  136. [Browsable(false)]
  137. [DefaultValue(typeof(GraphicsPath), "null")]
  138. public GraphicsPath ThumbCustomShape
  139. {
  140. get { return _thumbCustomShape; }
  141. set
  142. {
  143. _thumbCustomShape = value;
  144. //_thumbSize = (int) (_barOrientation == Orientation.Horizontal ? value.GetBounds().Width : value.GetBounds().Height) + 1;
  145. _thumbSize = new Size((int)value.GetBounds().Width, (int)value.GetBounds().Height);
  146.  
  147. Invalidate();
  148. }
  149. }
  150.  
  151. private Size _thumbRoundRectSize = new Size(5, 5);
  152. /// <summary>
  153. /// Gets or sets the size of the thumb round rectangle edges.
  154. /// </summary>
  155. /// <value>The size of the thumb round rectangle edges.</value>
  156. [Description("Set Slider's thumb round rect size")]
  157. [Category("ColorSlider")]
  158. [DefaultValue(typeof(Size), "5; 5")]
  159. public Size ThumbRoundRectSize
  160. {
  161. get { return _thumbRoundRectSize; }
  162. set
  163. {
  164. int h = value.Height, w = value.Width;
  165. if (h <= 0) h = 1;
  166. if (w <= 0) w = 1;
  167. _thumbRoundRectSize = new Size(w, h);
  168. Invalidate();
  169. }
  170. }
  171.  
  172. private Size _borderRoundRectSize = new Size(8, 8);
  173. /// <summary>
  174. /// Gets or sets the size of the border round rect.
  175. /// </summary>
  176. /// <value>The size of the border round rect.</value>
  177. [Description("Set Slider's border round rect size")]
  178. [Category("ColorSlider")]
  179. [DefaultValue(typeof(Size), "8; 8")]
  180. public Size BorderRoundRectSize
  181. {
  182. get { return _borderRoundRectSize; }
  183. set
  184. {
  185. int h = value.Height, w = value.Width;
  186. if (h <= 0) h = 1;
  187. if (w <= 0) w = 1;
  188. _borderRoundRectSize = new Size(w, h);
  189. Invalidate();
  190. }
  191. }
  192.  
  193. private bool _drawSemitransparentThumb = true;
  194. /// <summary>
  195. /// Gets or sets a value indicating whether to draw semitransparent thumb.
  196. /// </summary>
  197. /// <value><c>true</c> if semitransparent thumb should be drawn; otherwise, <c>false</c>.</value>
  198. [Description("Set whether to draw semitransparent thumb")]
  199. [Category("ColorSlider")]
  200. [DefaultValue(true)]
  201. public bool DrawSemitransparentThumb
  202. {
  203. get { return _drawSemitransparentThumb; }
  204. set
  205. {
  206. _drawSemitransparentThumb = value;
  207. Invalidate();
  208. }
  209. }
  210.  
  211. private Image _thumbImage = null;
  212. //private Image _thumbImage = Properties.Resources.BTN_Thumb_Blue;
  213. /// <summary>
  214. /// Gets or sets the Image used to render the thumb.
  215. /// </summary>
  216. /// <value>the thumb Image</value>
  217. [Description("Set to use a specific Image for the thumb")]
  218. [Category("ColorSlider")]
  219. [DefaultValue(null)]
  220. public Image ThumbImage
  221. {
  222. get { return _thumbImage; }
  223. set
  224. {
  225. if (value != null)
  226. _thumbImage = value;
  227. else
  228. _thumbImage = null;
  229. Invalidate();
  230. }
  231. }
  232.  
  233. #endregion
  234.  
  235.  
  236. #region Appearance
  237.  
  238. private int _padding = 0;
  239. /// <summary>
  240. /// Gets or sets the padding (inside margins: left & right or bottom & top)
  241. /// </summary>
  242. /// <value>The padding.</value>
  243. [Description("Set Slider padding (inside margins: left & right or bottom & top)")]
  244. [Category("ColorSlider")]
  245. [DefaultValue(0)]
  246. public new int Padding
  247. {
  248. get { return _padding; }
  249. set
  250. {
  251. if (_padding != value)
  252. {
  253. _padding = value;
  254. OffsetL = OffsetR = _padding;
  255.  
  256. Invalidate();
  257. }
  258. }
  259. }
  260.  
  261. private Orientation _barOrientation = Orientation.Vertical;
  262. /// <summary>
  263. /// Gets or sets the orientation of Slider.
  264. /// </summary>
  265. /// <value>The orientation.</value>
  266. [Description("Set Slider orientation")]
  267. [Category("ColorSlider")]
  268. [DefaultValue(Orientation.Vertical)]
  269. public Orientation Orientation
  270. {
  271. get { return _barOrientation; }
  272. set
  273. {
  274. if (_barOrientation != value)
  275. {
  276. _barOrientation = value;
  277. // Switch from horizontal to vertical (design mode)
  278. // Comment these lines if problems in Run mode
  279. if (DesignMode)
  280. {
  281. (Height, Width) = (Width, Height);
  282. }
  283.  
  284. Invalidate();
  285. }
  286. }
  287. }
  288.  
  289. private bool _drawFocusRectangle = false;
  290. /// <summary>
  291. /// Gets or sets a value indicating whether to draw focus rectangle.
  292. /// </summary>
  293. /// <value><c>true</c> if focus rectangle should be drawn; otherwise, <c>false</c>.</value>
  294. [Description("Set whether to draw focus rectangle")]
  295. [Category("ColorSlider")]
  296. [DefaultValue(false)]
  297. public bool DrawFocusRectangle
  298. {
  299. get { return _drawFocusRectangle; }
  300. set
  301. {
  302. _drawFocusRectangle = value;
  303. Invalidate();
  304. }
  305. }
  306.  
  307. private bool _mouseEffects = true;
  308. /// <summary>
  309. /// Gets or sets whether mouse entry and exit actions have impact on how control look.
  310. /// </summary>
  311. /// <value><c>true</c> if mouse entry and exit actions have impact on how control look; otherwise, <c>false</c>.</value>
  312. [Description("Set whether mouse entry and exit actions have impact on how control look")]
  313. [Category("ColorSlider")]
  314. [DefaultValue(true)]
  315. public bool MouseEffects
  316. {
  317. get { return _mouseEffects; }
  318. set
  319. {
  320. _mouseEffects = value;
  321. Invalidate();
  322. }
  323. }
  324.  
  325. #endregion
  326.  
  327.  
  328. #region values
  329.  
  330. private decimal _trackerValue = 30;
  331. /// <summary>
  332. /// Gets or sets the value of Slider.
  333. /// </summary>
  334. /// <value>The value.</value>
  335. /// <exception cref="T:System.ArgumentOutOfRangeException">exception thrown when value is outside appropriate range (min, max)</exception>
  336. [Description("Set Slider value")]
  337. [Category("ColorSlider")]
  338. [DefaultValue(30)]
  339. public decimal Value
  340. {
  341. get { return _trackerValue; }
  342. set
  343. {
  344. if (value >= _minimum & value <= _maximum)
  345. {
  346. _trackerValue = value;
  347. ValueChanged?.Invoke(this, new EventArgs());
  348. Invalidate();
  349. }
  350. else throw new ArgumentOutOfRangeException("Value is outside appropriate range (min, max)");
  351. }
  352. }
  353.  
  354. private decimal _minimum = 0;
  355. /// <summary>
  356. /// Gets or sets the minimum value.
  357. /// </summary>
  358. /// <value>The minimum value.</value>
  359. /// <exception cref="T:System.ArgumentOutOfRangeException">exception thrown when minimal value is greather than maximal one</exception>
  360. [Description("Set Slider minimal point")]
  361. [Category("ColorSlider")]
  362. [DefaultValue(0)]
  363. public decimal Minimum
  364. {
  365. get { return _minimum; }
  366. set
  367. {
  368. if (value < _maximum)
  369. {
  370. _minimum = value;
  371. if (_trackerValue < _minimum)
  372. {
  373. _trackerValue = _minimum;
  374. ValueChanged?.Invoke(this, new EventArgs());
  375. }
  376. Invalidate();
  377. }
  378. else throw new ArgumentOutOfRangeException("Minimal value is greather than maximal one");
  379. }
  380. }
  381.  
  382. private decimal _maximum = 100;
  383. /// <summary>
  384. /// Gets or sets the maximum value.
  385. /// </summary>
  386. /// <value>The maximum value.</value>
  387. /// <exception cref="T:System.ArgumentOutOfRangeException">exception thrown when maximal value is lower than minimal one</exception>
  388. [Description("Set Slider maximal point")]
  389. [Category("ColorSlider")]
  390. [DefaultValue(100)]
  391. public decimal Maximum
  392. {
  393. get { return _maximum; }
  394. set
  395. {
  396. if (value > _minimum)
  397. {
  398. _maximum = value;
  399. if (_trackerValue > _maximum)
  400. {
  401. _trackerValue = _maximum;
  402. ValueChanged?.Invoke(this, new EventArgs());
  403. }
  404. Invalidate();
  405. }
  406. //else throw new ArgumentOutOfRangeException("Maximal value is lower than minimal one");
  407. }
  408. }
  409.  
  410. private decimal _smallChange = 1;
  411. /// <summary>
  412. /// Gets or sets trackbar's small change. It affects how to behave when directional keys are pressed
  413. /// </summary>
  414. /// <value>The small change value.</value>
  415. [Description("Set trackbar's small change")]
  416. [Category("ColorSlider")]
  417. [DefaultValue(1)]
  418. public decimal SmallChange
  419. {
  420. get { return _smallChange; }
  421. set { _smallChange = value; }
  422. }
  423.  
  424. private decimal _largeChange = 5;
  425. /// <summary>
  426. /// Gets or sets trackbar's large change. It affects how to behave when PageUp/PageDown keys are pressed
  427. /// </summary>
  428. /// <value>The large change value.</value>
  429. [Description("Set trackbar's large change")]
  430. [Category("ColorSlider")]
  431. [DefaultValue(5)]
  432. public decimal LargeChange
  433. {
  434. get { return _largeChange; }
  435. set { _largeChange = value; }
  436. }
  437.  
  438. private int _mouseWheelBarPartitions = 10;
  439. /// <summary>
  440. /// Gets or sets the mouse wheel bar partitions.
  441. /// </summary>
  442. /// <value>The mouse wheel bar partitions.</value>
  443. /// <exception cref="T:System.ArgumentOutOfRangeException">exception thrown when value isn't greather than zero</exception>
  444. [Description("Set to how many parts is bar divided when using mouse wheel")]
  445. [Category("ColorSlider")]
  446. [DefaultValue(10)]
  447. public int MouseWheelBarPartitions
  448. {
  449. get { return _mouseWheelBarPartitions; }
  450. set
  451. {
  452. if (value > 0)
  453. _mouseWheelBarPartitions = value;
  454. else throw new ArgumentOutOfRangeException("MouseWheelBarPartitions has to be greather than zero");
  455. }
  456. }
  457.  
  458. #endregion
  459.  
  460.  
  461. #region colors
  462.  
  463. private Color _thumbOuterColor = Color.FromArgb(45, 182, 186);
  464. /// <summary>
  465. /// Gets or sets the thumb outer color.
  466. /// </summary>
  467. /// <value>The thumb outer color.</value>
  468. [Description("Sets Slider thumb outer color")]
  469. [Category("ColorSlider")]
  470. [DefaultValue(typeof(Color), "Turquoise")]
  471. public Color ThumbOuterColor
  472. {
  473. get { return _thumbOuterColor; }
  474. set
  475. {
  476. _thumbOuterColor = value;
  477. Invalidate();
  478. }
  479. }
  480.  
  481. private Color _thumbInnerColor = Color.FromArgb(45, 182, 186);
  482. /// <summary>
  483. /// Gets or sets the inner color of the thumb.
  484. /// </summary>
  485. /// <value>The inner color of the thumb.</value>
  486. [Description("Set Slider thumb inner color")]
  487. [Category("ColorSlider")]
  488. public Color ThumbInnerColor
  489. {
  490. get { return _thumbInnerColor; }
  491. set
  492. {
  493. _thumbInnerColor = value;
  494. Invalidate();
  495. }
  496. }
  497.  
  498. private Color _thumbPenColor = Color.FromArgb(185, 230, 232);
  499. /// <summary>
  500. /// Gets or sets the color of the thumb pen.
  501. /// </summary>
  502. /// <value>The color of the thumb pen.</value>
  503. [Description("Set Slider thumb pen color")]
  504. [Category("ColorSlider")]
  505. public Color ThumbPenColor
  506. {
  507. get { return _thumbPenColor; }
  508. set
  509. {
  510. _thumbPenColor = value;
  511. Invalidate();
  512. }
  513. }
  514.  
  515. private Color _barInnerColor = Color.DimGray;
  516. /// <summary>
  517. /// Gets or sets the inner color of the bar.
  518. /// </summary>
  519. /// <value>The inner color of the bar.</value>
  520. [Description("Set Slider bar inner color")]
  521. [Category("ColorSlider")]
  522. [DefaultValue(typeof(Color), "DimGray")]
  523. public Color BarInnerColor
  524. {
  525. get { return _barInnerColor; }
  526. set
  527. {
  528. _barInnerColor = value;
  529. Invalidate();
  530. }
  531. }
  532.  
  533. private Color _elapsedPenColorTop = Color.White;
  534. /// <summary>
  535. /// Gets or sets the top color of the Elapsed
  536. /// </summary>
  537. [Description("Gets or sets the top color of the elapsed")]
  538. [Category("ColorSlider")]
  539. public Color ElapsedPenColorTop
  540. {
  541. get { return _elapsedPenColorTop; }
  542. set
  543. {
  544. _elapsedPenColorTop = value;
  545. Invalidate();
  546. }
  547. }
  548.  
  549. private Color _elapsedPenColorBottom = Color.White;
  550. /// <summary>
  551. /// Gets or sets the bottom color of the elapsed
  552. /// </summary>
  553. [Description("Gets or sets the bottom color of the elapsed")]
  554. [Category("ColorSlider")]
  555. public Color ElapsedPenColorBottom
  556. {
  557. get { return _elapsedPenColorBottom; }
  558. set
  559. {
  560. _elapsedPenColorBottom = value;
  561. Invalidate();
  562. }
  563. }
  564.  
  565. private Color _barPenColorTop = Color.DimGray;
  566. /// <summary>
  567. /// Gets or sets the top color of the bar
  568. /// </summary>
  569. [Description("Gets or sets the top color of the bar")]
  570. [Category("ColorSlider")]
  571. public Color BarPenColorTop
  572. {
  573. get { return _barPenColorTop; }
  574. set
  575. {
  576. _barPenColorTop = value;
  577. Invalidate();
  578. }
  579. }
  580.  
  581. private Color _barPenColorBottom = Color.DimGray;
  582. /// <summary>
  583. /// Gets or sets the bottom color of bar
  584. /// </summary>
  585. [Description("Gets or sets the bottom color of the bar")]
  586. [Category("ColorSlider")]
  587. public Color BarPenColorBottom
  588. {
  589. get { return _barPenColorBottom; }
  590. set
  591. {
  592. _barPenColorBottom = value;
  593. Invalidate();
  594. }
  595. }
  596.  
  597. private Color _elapsedInnerColor = Color.White;
  598. /// <summary>
  599. /// Gets or sets the inner color of the elapsed.
  600. /// </summary>
  601. /// <value>The inner color of the elapsed.</value>
  602. [Description("Set Slider's elapsed part inner color")]
  603. [Category("ColorSlider")]
  604. public Color ElapsedInnerColor
  605. {
  606. get { return _elapsedInnerColor; }
  607. set
  608. {
  609. _elapsedInnerColor = value;
  610. Invalidate();
  611. }
  612. }
  613.  
  614. private Color _tickColor = Color.FromArgb(45, 182, 186);
  615. /// <summary>
  616. /// Gets or sets the color of the graduations
  617. /// </summary>
  618. [Description("Color of graduations")]
  619. [Category("ColorSlider")]
  620. public Color TickColor
  621. {
  622. get { return _tickColor; }
  623. set
  624. {
  625. if (value != _tickColor)
  626. {
  627. _tickColor = value;
  628. Invalidate();
  629. }
  630. }
  631. }
  632.  
  633. #endregion
  634.  
  635.  
  636. #region divisions
  637.  
  638. // For ex: if values are multiples of 50,
  639. // values = 0, 50, 100, 150 etc...
  640. //set TickDivide to 50
  641. // And ticks will be displayed as
  642. // values = 0, 1, 2, 3 etc...
  643. private float _tickDivide = 0;
  644.  
  645. [Description("Gets or sets a value used to divide the graduation")]
  646. [Category("ColorSlider")]
  647. public float TickDivide
  648. {
  649. get { return _tickDivide; }
  650. set
  651. {
  652. _tickDivide = value;
  653. Invalidate();
  654. }
  655. }
  656.  
  657. private float _tickAdd = 0;
  658. [Description("Gets or sets a value added to the graduation")]
  659. [Category("ColorSlider")]
  660. public float TickAdd
  661. {
  662. get { return _tickAdd; }
  663. set
  664. {
  665. _tickAdd = value;
  666. Invalidate();
  667. }
  668. }
  669.  
  670. private TickStyle _tickStyle = TickStyle.None;
  671. /// <summary>
  672. /// Gets or sets where to display the ticks (None, both top-left, bottom-right)
  673. /// </summary>
  674. [Description("Gets or sets where to display the ticks")]
  675. [Category("ColorSlider")]
  676. [DefaultValue(TickStyle.None)]
  677. public TickStyle TickStyle
  678. {
  679. get { return _tickStyle; }
  680. set
  681. {
  682. _tickStyle = value;
  683. Invalidate();
  684. }
  685. }
  686.  
  687. private decimal _scaleDivisions = 10;
  688. /// <summary>
  689. /// How many divisions of maximum?
  690. /// </summary>
  691. [Description("Set the number of intervals between minimum and maximum")]
  692. [Category("ColorSlider")]
  693. public decimal ScaleDivisions
  694. {
  695. get { return _scaleDivisions; }
  696. set
  697. {
  698. if (value > 0)
  699. {
  700. _scaleDivisions = value;
  701. }
  702. //else throw new ArgumentOutOfRangeException("TickFreqency must be > 0 and < Maximum");
  703.  
  704. Invalidate();
  705. }
  706. }
  707.  
  708. private decimal _scaleSubDivisions = 5;
  709. /// <summary>
  710. /// How many subdivisions for each division
  711. /// </summary>
  712. [Description("Set the number of subdivisions between main divisions of graduation.")]
  713. [Category("ColorSlider")]
  714. public decimal ScaleSubDivisions
  715. {
  716. get { return _scaleSubDivisions; }
  717. set
  718. {
  719. if (value > 0 && _scaleDivisions > 0 && (_maximum - _minimum) / ((value + 1) * _scaleDivisions) > 0)
  720. {
  721. _scaleSubDivisions = value;
  722.  
  723. }
  724. //else throw new ArgumentOutOfRangeException("TickSubFreqency must be > 0 and < TickFrequency");
  725.  
  726. Invalidate();
  727. }
  728. }
  729.  
  730. private bool _showSmallScale = false;
  731. /// <summary>
  732. /// Shows Small Scale marking.
  733. /// </summary>
  734. [Description("Show or hide subdivisions of graduations")]
  735. [Category("ColorSlider")]
  736. public bool ShowSmallScale
  737. {
  738. get { return _showSmallScale; }
  739. set
  740. {
  741.  
  742. if (value == true)
  743. {
  744. if (_scaleDivisions > 0 && _scaleSubDivisions > 0 && (_maximum - _minimum) / ((_scaleSubDivisions + 1) * _scaleDivisions) > 0)
  745. {
  746. _showSmallScale = value;
  747. Invalidate();
  748. }
  749. else
  750. {
  751. _showSmallScale = false;
  752. }
  753. }
  754. else
  755. {
  756. _showSmallScale = value;
  757. // need to redraw
  758. Invalidate();
  759. }
  760. }
  761. }
  762.  
  763. private bool _showDivisionsText = false;
  764. /// <summary>
  765. /// Shows Small Scale marking.
  766. /// </summary>
  767. [Description("Show or hide text value of graduations")]
  768. [Category("ColorSlider")]
  769. public bool ShowDivisionsText
  770. {
  771. get { return _showDivisionsText; }
  772. set
  773. {
  774. _showDivisionsText = value;
  775. Invalidate();
  776. }
  777. }
  778.  
  779. #endregion
  780.  
  781.  
  782. #region Font
  783.  
  784. /// <summary>
  785. /// Get or Sets the Font of the Text being displayed.
  786. /// </summary>
  787. [Bindable(true),
  788. Browsable(true),
  789. Category("ColorSlider"),
  790. Description("Get or Sets the Font of the Text being displayed."),
  791. DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
  792. EditorBrowsable(EditorBrowsableState.Always)]
  793. public override Font Font
  794. {
  795. get
  796. {
  797. return base.Font;
  798. }
  799. set
  800. {
  801. base.Font = value;
  802. Invalidate();
  803. OnFontChanged(EventArgs.Empty);
  804. }
  805. }
  806.  
  807. /// <summary>
  808. /// Get or Sets the Font of the Text being displayed.
  809. /// </summary>
  810. [Bindable(true),
  811. Browsable(true),
  812. Category("ColorSlider"),
  813. Description("Get or Sets the Color of the Text being displayed."),
  814. DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
  815. EditorBrowsable(EditorBrowsableState.Always)]
  816. public override Color ForeColor
  817. {
  818. get
  819. {
  820. return base.ForeColor;
  821. }
  822. set
  823. {
  824. base.ForeColor = value;
  825. Invalidate();
  826. OnForeColorChanged(EventArgs.Empty);
  827. }
  828. }
  829.  
  830. #endregion
  831.  
  832. #endregion
  833.  
  834.  
  835. #region Color schemas
  836.  
  837. //define own color schemas
  838. private readonly Color[,] aColorSchema = new Color[,]
  839. {
  840.  
  841. {
  842. Color.FromArgb(45, 182, 186),
  843. Color.FromArgb(45, 182, 186),
  844. Color.FromArgb(185, 230, 232),
  845.  
  846.  
  847. Color.DimGray,
  848. Color.DimGray,
  849. Color.DimGray,
  850.  
  851.  
  852. Color.White,
  853. Color.White,
  854. Color.White
  855. },
  856.  
  857. };
  858.  
  859. public enum ColorSchemas
  860. {
  861. ViPER4Windows,
  862. None
  863. }
  864.  
  865. private ColorSchemas colorSchema = ColorSchemas.None;
  866.  
  867. /// <summary>
  868. /// Sets color schema. Color generalization / fast color changing. Has no effect when slider colors are changed manually after schema was applied.
  869. /// </summary>
  870. /// <value>New color schema value</value>
  871. [Description("Set Slider color schema. Has no effect when slider colors are changed manually after schema was applied.")]
  872. [Category("ColorSlider")]
  873. [DefaultValue(typeof(ColorSchemas), "None")]
  874. public ColorSchemas ColorSchema
  875. {
  876. get { return colorSchema; }
  877. set
  878. {
  879. colorSchema = value;
  880. byte sn = (byte)value;
  881. _thumbOuterColor = aColorSchema[sn, 0];
  882. _thumbInnerColor = aColorSchema[sn, 1];
  883. _thumbPenColor = aColorSchema[sn, 2];
  884.  
  885. _barInnerColor = aColorSchema[sn, 3];
  886. _barPenColorTop = aColorSchema[sn, 4];
  887. _barPenColorBottom = aColorSchema[sn, 5];
  888.  
  889. _elapsedPenColorTop = aColorSchema[sn, 6];
  890. _elapsedPenColorBottom = aColorSchema[sn, 7];
  891. _elapsedInnerColor = aColorSchema[sn, 8];
  892.  
  893. Invalidate();
  894. }
  895. }
  896.  
  897. #endregion
  898.  
  899.  
  900. #region Constructors
  901.  
  902. /// <summary>
  903. /// Initializes a new instance of the <see cref="Slider"/> class.
  904. /// </summary>
  905. /// <param name="min">The minimum value.</param>
  906. /// <param name="max">The maximum value.</param>
  907. /// <param name="value">The current value.</param>
  908. public Slider(decimal min, decimal max, decimal value)
  909. {
  910.  
  911. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer |
  912. ControlStyles.ResizeRedraw | ControlStyles.Selectable |
  913. ControlStyles.SupportsTransparentBackColor | ControlStyles.UserMouse |
  914. ControlStyles.UserPaint, true);
  915.  
  916. // Default backcolor
  917. BackColor = Color.Transparent;
  918. ForeColor = Color.White;
  919.  
  920. // Font
  921. //this.Font = new Font("Tahoma", 6.75f);
  922. Font = new Font("Microsoft Sans Serif", 6f);
  923.  
  924. Minimum = min;
  925. Maximum = max;
  926. Value = value;
  927. Size = new Size(78, 244);
  928. }
  929.  
  930. /// <summary>
  931. /// Initializes a new instance of the <see cref="Slider"/> class.
  932. /// </summary>
  933. public Slider() : this(0, 100, 30) { }
  934.  
  935. #endregion
  936.  
  937. #region Paint
  938.  
  939. /// <summary>
  940. /// Raises the <see cref="E:System.Windows.Forms.Control.Paint"></see> event.
  941. /// </summary>
  942. /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"></see> that contains the event data.</param>
  943. protected override void OnPaint(PaintEventArgs e)
  944. {
  945. if (!Enabled)
  946. {
  947. Color[] desaturatedColors = DesaturateColors(_thumbOuterColor, _thumbInnerColor, _thumbPenColor,
  948. _barInnerColor,
  949. _elapsedPenColorTop, _elapsedPenColorBottom,
  950. _barPenColorTop, _barPenColorBottom,
  951. _elapsedInnerColor);
  952. DrawColorSlider(e,
  953. desaturatedColors[0], desaturatedColors[1], desaturatedColors[2],
  954. desaturatedColors[3],
  955. desaturatedColors[4], desaturatedColors[5],
  956. desaturatedColors[6], desaturatedColors[7],
  957. desaturatedColors[8]);
  958. }
  959. else
  960. {
  961. if (_mouseEffects && mouseInRegion)
  962. {
  963. Color[] lightenedColors = LightenColors(_thumbOuterColor, _thumbInnerColor, _thumbPenColor,
  964. _barInnerColor,
  965. _elapsedPenColorTop, _elapsedPenColorBottom,
  966. _barPenColorTop, _barPenColorBottom,
  967. _elapsedInnerColor);
  968. DrawColorSlider(e,
  969. lightenedColors[0], lightenedColors[1], lightenedColors[2],
  970. lightenedColors[3],
  971. lightenedColors[4], lightenedColors[5],
  972. lightenedColors[6], lightenedColors[7],
  973. lightenedColors[8]);
  974. }
  975. else
  976. {
  977. DrawColorSlider(e,
  978. _thumbOuterColor, _thumbInnerColor, _thumbPenColor,
  979. _barInnerColor,
  980. _elapsedPenColorTop, _elapsedPenColorBottom,
  981. _barPenColorTop, _barPenColorBottom,
  982. _elapsedInnerColor);
  983. }
  984. }
  985. }
  986.  
  987. /// <summary>
  988. /// Draws the colorslider control using passed colors.
  989. /// </summary>
  990. /// <param name="e">The <see cref="T:System.Windows.Forms.PaintEventArgs"/> instance containing the event data.</param>
  991. /// <param name="thumbOuterColorPaint">The thumb outer color paint.</param>
  992. /// <param name="thumbInnerColorPaint">The thumb inner color paint.</param>
  993. /// <param name="thumbPenColorPaint">The thumb pen color paint.</param>
  994. /// <param name="barInnerColorPaint">The bar inner color paint.</param>
  995. /// <param name="barPenColorPaint">The bar pen color paint.</param>
  996. /// <param name="elapsedInnerColorPaint">The elapsed inner color paint.</param>
  997. private void DrawColorSlider(PaintEventArgs e,
  998. Color thumbOuterColorPaint, Color thumbInnerColorPaint, Color thumbPenColorPaint,
  999. Color barInnerColorPaint,
  1000. Color ElapsedTopPenColorPaint, Color ElapsedBottomPenColorPaint,
  1001. Color barTopPenColorPaint, Color barBottomPenColorPaint,
  1002. Color elapsedInnerColorPaint)
  1003. {
  1004. try
  1005. {
  1006. //adjust drawing rects
  1007. barRect = ClientRectangle;
  1008.  
  1009. //set up thumbRect approprietly
  1010. if (_barOrientation == Orientation.Horizontal)
  1011. {
  1012. #region horizontal
  1013. if (_thumbImage != null)
  1014. {
  1015. decimal TrackX = OffsetL + (_trackerValue - _minimum) * (ClientRectangle.Width - OffsetL - OffsetR - _thumbImage.Width) / (_maximum - _minimum);
  1016. thumbRect = new Rectangle((int)TrackX, ClientRectangle.Height / 2 - _thumbImage.Height / 2, _thumbImage.Width, _thumbImage.Height);
  1017. }
  1018. else
  1019. {
  1020. decimal TrackX = OffsetL + (_trackerValue - _minimum) * (ClientRectangle.Width - OffsetL - OffsetR - _thumbSize.Width) / (_maximum - _minimum);
  1021. thumbRect = new Rectangle((int)TrackX, barRect.Y + ClientRectangle.Height / 2 - _thumbSize.Height / 2, _thumbSize.Width, _thumbSize.Height);
  1022. }
  1023. #endregion
  1024. }
  1025. else
  1026. {
  1027. #region vertical
  1028. if (_thumbImage != null)
  1029. {
  1030. decimal TrackY = OffsetR + (_maximum - _trackerValue) * (ClientRectangle.Height - OffsetL - OffsetR - _thumbImage.Height) / (_maximum - _minimum);
  1031. thumbRect = new Rectangle(ClientRectangle.Width / 2 - _thumbImage.Width / 2, (int)TrackY, _thumbImage.Width, _thumbImage.Height);
  1032. }
  1033. else
  1034. {
  1035. decimal TrackY = OffsetR + (_maximum - _trackerValue) * (ClientRectangle.Height - OffsetL - OffsetR - _thumbSize.Height) / (_maximum - _minimum);
  1036. thumbRect = new Rectangle(barRect.X + ClientRectangle.Width / 2 - _thumbSize.Width / 2, (int)TrackY, _thumbSize.Width, _thumbSize.Height);
  1037. }
  1038. #endregion
  1039. }
  1040.  
  1041. thumbHalfRect = thumbRect;
  1042. LinearGradientMode gradientOrientation;
  1043.  
  1044. if (_barOrientation == Orientation.Horizontal)
  1045. {
  1046. #region horizontal
  1047.  
  1048. barRect.X += OffsetL;
  1049. barRect.Width = barRect.Width - OffsetL - OffsetR;
  1050.  
  1051. barHalfRect = barRect;
  1052. barHalfRect.Height /= 2;
  1053.  
  1054. gradientOrientation = LinearGradientMode.Vertical;
  1055.  
  1056.  
  1057. thumbHalfRect.Height /= 2;
  1058. elapsedRect = barRect;
  1059. elapsedRect.Width = thumbRect.Left + _thumbSize.Width / 2 - OffsetL;
  1060.  
  1061. #endregion
  1062. }
  1063. else
  1064. {
  1065. #region vertical
  1066.  
  1067. barRect.Y += OffsetR;
  1068. barRect.Height = barRect.Height - OffsetL - OffsetR;
  1069.  
  1070. barHalfRect = barRect;
  1071. barHalfRect.Width /= 2;
  1072.  
  1073. gradientOrientation = LinearGradientMode.Vertical;
  1074.  
  1075. thumbHalfRect.Width /= 2;
  1076. elapsedRect = barRect;
  1077.  
  1078. elapsedRect.Height = barRect.Height - (thumbRect.Top + ThumbSize.Height / 2) + OffsetR;
  1079. elapsedRect.Y = 1 + thumbRect.Top + ThumbSize.Height / 2 + OffsetR;
  1080.  
  1081. #endregion
  1082. }
  1083.  
  1084. //get thumb shape path
  1085. GraphicsPath thumbPath;
  1086. if (_thumbCustomShape == null)
  1087. thumbPath = CreateRoundRectPath(thumbRect, _thumbRoundRectSize);
  1088. else
  1089. {
  1090. thumbPath = _thumbCustomShape;
  1091. Matrix m = new Matrix();
  1092. m.Translate(thumbRect.Left - thumbPath.GetBounds().Left, thumbRect.Top - thumbPath.GetBounds().Top);
  1093. thumbPath.Transform(m);
  1094. }
  1095.  
  1096.  
  1097. //draw bar
  1098.  
  1099. #region draw inner bar
  1100.  
  1101. // inner bar is a single line
  1102. // draw the line on the whole lenght of the control
  1103. if (_barOrientation == Orientation.Horizontal)
  1104. {
  1105. e.Graphics.DrawLine(new Pen(barInnerColorPaint, 1f), barRect.X, barRect.Y + barRect.Height / 2, barRect.X + barRect.Width, barRect.Y + barRect.Height / 2);
  1106. }
  1107. else
  1108. {
  1109. e.Graphics.DrawLine(new Pen(barInnerColorPaint, 1f), barRect.X + barRect.Width / 2, barRect.Y, barRect.X + barRect.Width / 2, barRect.Y + barRect.Height);
  1110. }
  1111. #endregion
  1112.  
  1113.  
  1114. #region draw elapsed bar
  1115.  
  1116. //draw elapsed inner bar (single line too)
  1117. if (_barOrientation == Orientation.Horizontal)
  1118. {
  1119. e.Graphics.DrawLine(new Pen(elapsedInnerColorPaint, 1f), barRect.X, barRect.Y + barRect.Height / 2, barRect.X + elapsedRect.Width, barRect.Y + barRect.Height / 2);
  1120. }
  1121. else
  1122. {
  1123. e.Graphics.DrawLine(new Pen(elapsedInnerColorPaint, 1f), barRect.X + barRect.Width / 2, barRect.Y + (barRect.Height - elapsedRect.Height), barRect.X + barRect.Width / 2, barRect.Y + barRect.Height);
  1124. }
  1125.  
  1126. #endregion draw elapsed bar
  1127.  
  1128.  
  1129. #region draw external contours
  1130.  
  1131. //draw external bar band
  1132. // 2 lines: top and bottom
  1133. if (_barOrientation == Orientation.Horizontal)
  1134. {
  1135. #region horizontal
  1136. // Elapsed top
  1137. e.Graphics.DrawLine(new Pen(ElapsedTopPenColorPaint, 1f), barRect.X, barRect.Y - 1 + barRect.Height / 2, barRect.X + elapsedRect.Width, barRect.Y - 1 + barRect.Height / 2);
  1138. // Elapsed bottom
  1139. e.Graphics.DrawLine(new Pen(ElapsedBottomPenColorPaint, 1f), barRect.X, barRect.Y + 1 + barRect.Height / 2, barRect.X + elapsedRect.Width, barRect.Y + 1 + barRect.Height / 2);
  1140.  
  1141.  
  1142. // Remain top
  1143. e.Graphics.DrawLine(new Pen(barTopPenColorPaint, 1f), barRect.X + elapsedRect.Width, barRect.Y - 1 + barRect.Height / 2, barRect.X + barRect.Width, barRect.Y - 1 + barRect.Height / 2);
  1144. // Remain bottom
  1145. e.Graphics.DrawLine(new Pen(barBottomPenColorPaint, 1f), barRect.X + elapsedRect.Width, barRect.Y + 1 + barRect.Height / 2, barRect.X + barRect.Width, barRect.Y + 1 + barRect.Height / 2);
  1146.  
  1147.  
  1148. // Left vertical (dark)
  1149. e.Graphics.DrawLine(new Pen(barTopPenColorPaint, 1f), barRect.X, barRect.Y - 1 + barRect.Height / 2, barRect.X, barRect.Y + barRect.Height / 2 + 1);
  1150.  
  1151. // Right vertical (light)
  1152. e.Graphics.DrawLine(new Pen(barBottomPenColorPaint, 1f), barRect.X + barRect.Width, barRect.Y - 1 + barRect.Height / 2, barRect.X + barRect.Width, barRect.Y + 1 + barRect.Height / 2);
  1153. #endregion
  1154. }
  1155. else
  1156. {
  1157. #region vertical
  1158. // Elapsed top
  1159. e.Graphics.DrawLine(new Pen(ElapsedTopPenColorPaint, 1f), barRect.X - 1 + barRect.Width / 2, barRect.Y + (barRect.Height - elapsedRect.Height), barRect.X - 1 + barRect.Width / 2, barRect.Y + barRect.Height);
  1160.  
  1161. // Elapsed bottom
  1162. e.Graphics.DrawLine(new Pen(ElapsedBottomPenColorPaint, 1f), barRect.X + 1 + barRect.Width / 2, barRect.Y + (barRect.Height - elapsedRect.Height), barRect.X + 1 + barRect.Width / 2, barRect.Y + barRect.Height);
  1163.  
  1164.  
  1165. // Remain top
  1166. e.Graphics.DrawLine(new Pen(barTopPenColorPaint, 1f), barRect.X - 1 + barRect.Width / 2, barRect.Y, barRect.X - 1 + barRect.Width / 2, barRect.Y + barRect.Height - elapsedRect.Height);
  1167.  
  1168.  
  1169. // Remain bottom
  1170. e.Graphics.DrawLine(new Pen(barBottomPenColorPaint, 1f), barRect.X + 1 + barRect.Width / 2, barRect.Y, barRect.X + 1 + barRect.Width / 2, barRect.Y + barRect.Height - elapsedRect.Height);
  1171.  
  1172.  
  1173. // top horizontal (dark)
  1174. e.Graphics.DrawLine(new Pen(barTopPenColorPaint, 1f), barRect.X - 1 + barRect.Width / 2, barRect.Y, barRect.X + 1 + barRect.Width / 2, barRect.Y);
  1175.  
  1176. // bottom horizontal (light)
  1177. e.Graphics.DrawLine(new Pen(barBottomPenColorPaint, 1f), barRect.X - 1 + barRect.Width / 2, barRect.Y + barRect.Height, barRect.X + 1 + barRect.Width / 2, barRect.Y + barRect.Height);
  1178. #endregion
  1179.  
  1180. }
  1181.  
  1182. #endregion draw contours
  1183.  
  1184.  
  1185.  
  1186. #region draw thumb
  1187.  
  1188. //draw thumb
  1189. Color newthumbOuterColorPaint = thumbOuterColorPaint, newthumbInnerColorPaint = thumbInnerColorPaint;
  1190. if (Capture && _drawSemitransparentThumb)
  1191. {
  1192. newthumbOuterColorPaint = Color.FromArgb(175, thumbOuterColorPaint);
  1193. newthumbInnerColorPaint = Color.FromArgb(175, thumbInnerColorPaint);
  1194. }
  1195.  
  1196. LinearGradientBrush lgbThumb;
  1197. if (_barOrientation == Orientation.Horizontal)
  1198. {
  1199. lgbThumb = new LinearGradientBrush(thumbRect, newthumbOuterColorPaint, newthumbInnerColorPaint, gradientOrientation);
  1200. }
  1201. else
  1202. {
  1203. lgbThumb = new LinearGradientBrush(thumbHalfRect, newthumbOuterColorPaint, newthumbInnerColorPaint, gradientOrientation);
  1204. }
  1205. using (lgbThumb)
  1206. {
  1207. lgbThumb.WrapMode = WrapMode.TileFlipXY;
  1208.  
  1209. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  1210. e.Graphics.FillPath(lgbThumb, thumbPath);
  1211.  
  1212. //draw thumb band
  1213. Color newThumbPenColor = thumbPenColorPaint;
  1214.  
  1215. if (_mouseEffects && (Capture || mouseInThumbRegion))
  1216. newThumbPenColor = ControlPaint.Dark(newThumbPenColor);
  1217. using (Pen thumbPen = new Pen(newThumbPenColor))
  1218. {
  1219.  
  1220. if (_thumbImage != null)
  1221. {
  1222. Bitmap bmp = new Bitmap(_thumbImage);
  1223. bmp.MakeTransparent(Color.FromArgb(255, 0, 255));
  1224. Rectangle srceRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  1225.  
  1226. e.Graphics.DrawImage(bmp, thumbRect, srceRect, GraphicsUnit.Pixel);
  1227. bmp.Dispose();
  1228.  
  1229. }
  1230. else
  1231. {
  1232. e.Graphics.DrawPath(thumbPen, thumbPath);
  1233. }
  1234. }
  1235.  
  1236. }
  1237. #endregion draw thumb
  1238.  
  1239.  
  1240. #region draw focusing rectangle
  1241. //draw focusing rectangle
  1242. if (Focused & _drawFocusRectangle)
  1243. using (Pen p = new Pen(Color.FromArgb(200, ElapsedTopPenColorPaint)))
  1244. {
  1245. p.DashStyle = DashStyle.Dot;
  1246. Rectangle r = ClientRectangle;
  1247. r.Width -= 2;
  1248. r.Height--;
  1249. r.X++;
  1250.  
  1251. using (GraphicsPath gpBorder = CreateRoundRectPath(r, _borderRoundRectSize))
  1252. {
  1253. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  1254. e.Graphics.DrawPath(p, gpBorder);
  1255. }
  1256. }
  1257. #endregion draw focusing rectangle
  1258.  
  1259.  
  1260. #region draw ticks
  1261.  
  1262. // Draw the ticks (main divisions, subdivisions and text)
  1263. if (_tickStyle != TickStyle.None)
  1264. {
  1265. int x1, x2, y1, y2 = 0;
  1266. int nbticks = 1 + (int)(_scaleDivisions * (_scaleSubDivisions + 1));
  1267. int interval = 0;
  1268. int start = 0;
  1269. int W = 0;
  1270. float rulerValue = 0;
  1271. int offset = 0;
  1272.  
  1273. // Calculate width W to draw graduations
  1274. // Remove the width of the thumb (half thumb at each end)
  1275. // in order that when the thumb is at minimum position or maximum position,
  1276. // the graduation coincide with the middle of the thumb
  1277. if (_barOrientation == Orientation.Horizontal)
  1278. {
  1279. start = thumbRect.Width / 2;
  1280. W = barRect.Width - thumbRect.Width;
  1281. rulerValue = (float)_minimum;
  1282. offset = 2 + thumbRect.Height / 2;
  1283. }
  1284. else
  1285. {
  1286. start = thumbRect.Height / 2;
  1287. W = barRect.Height - thumbRect.Height;
  1288. rulerValue = (float)_maximum;
  1289. offset = 2 + thumbRect.Width / 2;
  1290. }
  1291.  
  1292. // pen for ticks
  1293. // TODO: color for subdivision different?
  1294. Pen penTickL = new Pen(_tickColor, 1f);
  1295. Pen penTickS = new Pen(_tickColor, 1f);
  1296. int idx = 0;
  1297. int scaleL = 5; // division length
  1298. int scaleS = 3; // subdivision length
  1299.  
  1300.  
  1301. // strings graduations
  1302. // TODO: color for Text different?
  1303. float tx = 0;
  1304. float ty = 0;
  1305. int startDiv = 0;
  1306.  
  1307. Color _scaleColor = ForeColor;
  1308. SolidBrush br = new SolidBrush(_scaleColor);
  1309.  
  1310. // Calculate max size of text
  1311. //string str = String.Format("{0,0:D}", (int)_maximum);
  1312. string str = string.Format("{0,0:##}", _maximum);
  1313. Font font = Font;
  1314. SizeF maxsize = e.Graphics.MeasureString(str, font);
  1315.  
  1316. for (int i = 0; i <= _scaleDivisions; i++)
  1317. {
  1318. // Calculate current text size
  1319. float val = rulerValue;
  1320.  
  1321. // apply a transformation to the ticks displayed
  1322. if (_tickDivide != 0)
  1323. val /= _tickDivide;
  1324.  
  1325. if (_tickAdd != 0)
  1326. val += _tickAdd;
  1327.  
  1328. str = string.Format("{0:0.##}", val);
  1329. SizeF size = e.Graphics.MeasureString(str, font);
  1330.  
  1331.  
  1332. // HORIZONTAL
  1333. if (_barOrientation == Orientation.Horizontal)
  1334. {
  1335. #region horizontal
  1336.  
  1337. // Draw string graduations
  1338. if (_showDivisionsText)
  1339. {
  1340. if (_tickStyle == TickStyle.TopLeft || _tickStyle == TickStyle.Both)
  1341. {
  1342. tx = start + barRect.X + interval - (float)(size.Width * 0.5);
  1343. //ty = barRect.Y + barRect.Height / 2 - (1.5F)*(size.Height) - scaleL - offset;
  1344. ty = barRect.Y + barRect.Height / 2 - size.Height - scaleL - offset;
  1345. e.Graphics.DrawString(str, font, br, tx, ty);
  1346. }
  1347. if (_tickStyle == TickStyle.BottomRight || _tickStyle == TickStyle.Both)
  1348. {
  1349. tx = start + barRect.X + interval - (float)(size.Width * 0.5);
  1350. //ty = barRect.Y + barRect.Height/2 + (size.Height/2) + scaleL + offset;
  1351. ty = barRect.Y + barRect.Height / 2 + scaleL + offset;
  1352. e.Graphics.DrawString(str, font, br, tx, ty);
  1353. }
  1354. }
  1355.  
  1356. // draw main ticks
  1357. if (_tickStyle == TickStyle.TopLeft || _tickStyle == TickStyle.Both)
  1358. {
  1359. x1 = start + barRect.X + interval;
  1360. y1 = barRect.Y + barRect.Height / 2 - scaleL - offset;
  1361. x2 = start + barRect.X + interval;
  1362. y2 = barRect.Y + barRect.Height / 2 - offset;
  1363. e.Graphics.DrawLine(penTickL, x1, y1, x2, y2);
  1364. }
  1365. if (_tickStyle == TickStyle.BottomRight || _tickStyle == TickStyle.Both)
  1366. {
  1367.  
  1368. x1 = start + barRect.X + interval;
  1369. y1 = barRect.Y + barRect.Height / 2 + offset;
  1370. x2 = start + barRect.X + interval;
  1371. y2 = barRect.Y + barRect.Height / 2 + scaleL + offset;
  1372. e.Graphics.DrawLine(penTickL, x1, y1, x2, y2);
  1373. }
  1374.  
  1375. rulerValue += (float)((_maximum - _minimum) / _scaleDivisions);
  1376.  
  1377. // Draw subdivisions
  1378. if (i < _scaleDivisions)
  1379. {
  1380. for (int j = 0; j <= _scaleSubDivisions; j++)
  1381. {
  1382. idx++;
  1383. interval = idx * W / (nbticks - 1);
  1384.  
  1385. if (_showSmallScale)
  1386. {
  1387. // Horizontal
  1388. if (_tickStyle == TickStyle.TopLeft || _tickStyle == TickStyle.Both)
  1389. {
  1390. x1 = start + barRect.X + interval;
  1391. y1 = barRect.Y + barRect.Height / 2 - scaleS - offset;
  1392. x2 = start + barRect.X + interval;
  1393. y2 = barRect.Y + barRect.Height / 2 - offset;
  1394. e.Graphics.DrawLine(penTickS, x1, y1, x2, y2);
  1395. }
  1396. if (_tickStyle == TickStyle.BottomRight || _tickStyle == TickStyle.Both)
  1397. {
  1398. x1 = start + barRect.X + interval;
  1399. y1 = barRect.Y + barRect.Height / 2 + offset;
  1400. x2 = start + barRect.X + interval;
  1401. y2 = barRect.Y + barRect.Height / 2 + scaleS + offset;
  1402. e.Graphics.DrawLine(penTickS, x1, y1, x2, y2);
  1403. }
  1404. }
  1405. }
  1406. }
  1407. #endregion
  1408. }
  1409. else
  1410. {
  1411. #region vertical
  1412.  
  1413. // Draw string graduations
  1414. if (_showDivisionsText)
  1415. {
  1416. if (_tickStyle == TickStyle.TopLeft || _tickStyle == TickStyle.Both)
  1417. {
  1418. //tx = lineLeftX - size.Width / 2;
  1419. tx = barRect.X + barRect.Width / 2 - scaleL - size.Width - offset;
  1420. ty = start + barRect.Y + interval - (float)(size.Height * 0.5);
  1421. e.Graphics.DrawString(str, font, br, tx, ty);
  1422. }
  1423. if (_tickStyle == TickStyle.BottomRight || _tickStyle == TickStyle.Both)
  1424. {
  1425. //tx = lineRightX - size.Width / 2;
  1426. tx = barRect.X + barRect.Width / 2 + scaleL + offset;
  1427. ty = start + barRect.Y + interval - (float)(size.Height * 0.5);
  1428. e.Graphics.DrawString(str, font, br, tx, ty);
  1429. }
  1430.  
  1431. startDiv = (int)maxsize.Width + 3;
  1432. }
  1433.  
  1434.  
  1435. // draw main ticks
  1436. if (_tickStyle == TickStyle.TopLeft || _tickStyle == TickStyle.Both)
  1437. {
  1438. x1 = barRect.X + barRect.Width / 2 - scaleL - offset;
  1439. y1 = start + barRect.Y + interval;
  1440. x2 = barRect.X + barRect.Width / 2 - offset;
  1441. y2 = start + barRect.Y + interval;
  1442. e.Graphics.DrawLine(penTickL, x1, y1, x2, y2);
  1443. }
  1444. if (_tickStyle == TickStyle.BottomRight || _tickStyle == TickStyle.Both)
  1445. {
  1446. x1 = barRect.X + barRect.Width / 2 + offset;
  1447. y1 = start + barRect.Y + interval;
  1448. x2 = barRect.X + barRect.Width / 2 + scaleL + offset;
  1449. y2 = start + barRect.Y + interval;
  1450. e.Graphics.DrawLine(penTickL, x1, y1, x2, y2);
  1451. }
  1452.  
  1453. rulerValue -= (float)((_maximum - _minimum) / _scaleDivisions);
  1454.  
  1455. // draw subdivisions
  1456. if (i < _scaleDivisions)
  1457. {
  1458. for (int j = 0; j <= _scaleSubDivisions; j++)
  1459. {
  1460. idx++;
  1461. interval = idx * W / (nbticks - 1);
  1462.  
  1463. if (_showSmallScale)
  1464. {
  1465. if (_tickStyle == TickStyle.TopLeft || _tickStyle == TickStyle.Both)
  1466. {
  1467. x1 = barRect.X + barRect.Width / 2 - scaleS - offset;
  1468. y1 = start + barRect.Y + interval;
  1469. x2 = barRect.X + barRect.Width / 2 - offset;
  1470. y2 = start + barRect.Y + interval;
  1471. e.Graphics.DrawLine(penTickS, x1, y1, x2, y2);
  1472. }
  1473. if (_tickStyle == TickStyle.BottomRight || _tickStyle == TickStyle.Both)
  1474. {
  1475. x1 = barRect.X + barRect.Width / 2 + offset;
  1476. y1 = start + barRect.Y + interval;
  1477. x2 = barRect.X + barRect.Width / 2 + scaleS + offset;
  1478. y2 = start + barRect.Y + interval;
  1479. e.Graphics.DrawLine(penTickS, x1, y1, x2, y2);
  1480. }
  1481. }
  1482. }
  1483. }
  1484. #endregion
  1485. }
  1486. }
  1487. }
  1488. #endregion
  1489. }
  1490. catch (Exception Err)
  1491. {
  1492. Console.WriteLine("DrawBackGround Error in " + Name + ":" + Err.Message);
  1493. }
  1494. finally
  1495. {
  1496. }
  1497. }
  1498.  
  1499. #endregion
  1500.  
  1501. #region Overided events
  1502.  
  1503. private bool mouseInRegion = false;
  1504. /// <summary>
  1505. /// Raises the <see cref="E:System.Windows.Forms.Control.EnabledChanged"></see> event.
  1506. /// </summary>
  1507. /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
  1508. protected override void OnEnabledChanged(EventArgs e)
  1509. {
  1510. base.OnEnabledChanged(e);
  1511. Invalidate();
  1512. }
  1513.  
  1514. /// <summary>
  1515. /// Raises the <see cref="E:System.Windows.Forms.Control.MouseEnter"></see> event.
  1516. /// </summary>
  1517. /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
  1518. protected override void OnMouseEnter(EventArgs e)
  1519. {
  1520. base.OnMouseEnter(e);
  1521. mouseInRegion = true;
  1522. Invalidate();
  1523. }
  1524.  
  1525. /// <summary>
  1526. /// Raises the <see cref="E:System.Windows.Forms.Control.MouseLeave"></see> event.
  1527. /// </summary>
  1528. /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
  1529. protected override void OnMouseLeave(EventArgs e)
  1530. {
  1531. base.OnMouseLeave(e);
  1532. mouseInRegion = false;
  1533. mouseInThumbRegion = false;
  1534. Invalidate();
  1535. }
  1536.  
  1537. /// <summary>
  1538. /// Raises the <see cref="E:System.Windows.Forms.Control.MouseDown"></see> event.
  1539. /// </summary>
  1540. /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"></see> that contains the event data.</param>
  1541. protected override void OnMouseDown(MouseEventArgs e)
  1542. {
  1543. base.OnMouseDown(e);
  1544. if (e.Button == MouseButtons.Left)
  1545. {
  1546. Capture = true;
  1547. Scroll?.Invoke(this, new ScrollEventArgs(ScrollEventType.ThumbTrack, (int)_trackerValue));
  1548. ValueChanged?.Invoke(this, new EventArgs());
  1549. OnMouseMove(e);
  1550. }
  1551. }
  1552.  
  1553. private bool mouseInThumbRegion = false;
  1554.  
  1555. /// <summary>
  1556. /// Raises the <see cref="E:System.Windows.Forms.Control.MouseMove"></see> event.
  1557. /// </summary>
  1558. /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"></see> that contains the event data.</param>
  1559. protected override void OnMouseMove(MouseEventArgs e)
  1560. {
  1561. base.OnMouseMove(e);
  1562. mouseInThumbRegion = IsPointInRect(e.Location, thumbRect);
  1563.  
  1564. if (Capture & e.Button == MouseButtons.Left)
  1565. {
  1566. ScrollEventType set = ScrollEventType.ThumbPosition;
  1567. Point pt = e.Location;
  1568. int p = _barOrientation == Orientation.Horizontal ? pt.X : pt.Y;
  1569.  
  1570. int margin = _thumbSize.Height >> 1;
  1571. p -= margin;
  1572.  
  1573. if (_barOrientation == Orientation.Horizontal)
  1574. {
  1575. if (_thumbImage != null)
  1576. {
  1577. _trackerValue = _minimum + (p - OffsetL) * (_maximum - _minimum) / (ClientRectangle.Width - OffsetL - OffsetR - _thumbImage.Width);
  1578. }
  1579. else
  1580. {
  1581. _trackerValue = _minimum + (p - OffsetL) * (_maximum - _minimum) / (ClientRectangle.Width - OffsetL - OffsetR - _thumbSize.Width);
  1582. }
  1583. }
  1584. else
  1585. {
  1586. if (_thumbImage != null)
  1587. {
  1588. _trackerValue = _maximum - (p - OffsetR) * (_maximum - _minimum) / (ClientRectangle.Height - OffsetL - OffsetR - _thumbImage.Height);
  1589. }
  1590. else
  1591. {
  1592. _trackerValue = _maximum - (p - OffsetR) * (_maximum - _minimum) / (ClientRectangle.Height - OffsetL - OffsetR - _thumbSize.Height);
  1593. }
  1594. }
  1595.  
  1596. // Number of divisions
  1597. int nbdiv = (int)Math.Round(_trackerValue / _smallChange);
  1598. _trackerValue = nbdiv * _smallChange;
  1599.  
  1600. if (_trackerValue <= _minimum)
  1601. {
  1602. _trackerValue = _minimum;
  1603. set = ScrollEventType.First;
  1604. }
  1605. else if (_trackerValue >= _maximum)
  1606. {
  1607. _trackerValue = _maximum;
  1608. set = ScrollEventType.Last;
  1609. }
  1610.  
  1611. Scroll?.Invoke(this, new ScrollEventArgs(set, (int)_trackerValue));
  1612. ValueChanged?.Invoke(this, new EventArgs());
  1613. }
  1614. Invalidate();
  1615. }
  1616.  
  1617. /// <summary>
  1618. /// Raises the <see cref="E:System.Windows.Forms.Control.MouseUp"></see> event.
  1619. /// </summary>
  1620. /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"></see> that contains the event data.</param>
  1621. protected override void OnMouseUp(MouseEventArgs e)
  1622. {
  1623. base.OnMouseUp(e);
  1624. Capture = false;
  1625. mouseInThumbRegion = IsPointInRect(e.Location, thumbRect);
  1626. Scroll?.Invoke(this, new ScrollEventArgs(ScrollEventType.EndScroll, (int)_trackerValue));
  1627. ValueChanged?.Invoke(this, new EventArgs());
  1628. Invalidate();
  1629. }
  1630.  
  1631. /// <summary>
  1632. /// Raises the <see cref="E:System.Windows.Forms.Control.MouseWheel"></see> event.
  1633. /// </summary>
  1634. /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"></see> that contains the event data.</param>
  1635. protected override void OnMouseWheel(MouseEventArgs e)
  1636. {
  1637. base.OnMouseWheel(e);
  1638.  
  1639. if (mouseInRegion)
  1640. {
  1641. decimal v = e.Delta / 120 * (_maximum - _minimum) / _mouseWheelBarPartitions;
  1642. SetProperValue(Value + v);
  1643.  
  1644. // Avoid to send MouseWheel event to the parent container
  1645. ((HandledMouseEventArgs)e).Handled = true;
  1646. }
  1647. }
  1648.  
  1649. /// <summary>
  1650. /// Raises the <see cref="E:System.Windows.Forms.Control.GotFocus"></see> event.
  1651. /// </summary>
  1652. /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
  1653. protected override void OnGotFocus(EventArgs e)
  1654. {
  1655. base.OnGotFocus(e);
  1656. Invalidate();
  1657. }
  1658.  
  1659. /// <summary>
  1660. /// Raises the <see cref="E:System.Windows.Forms.Control.LostFocus"></see> event.
  1661. /// </summary>
  1662. /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
  1663. protected override void OnLostFocus(EventArgs e)
  1664. {
  1665. base.OnLostFocus(e);
  1666. Invalidate();
  1667. }
  1668.  
  1669. /// <summary>
  1670. /// Raises the <see cref="E:System.Windows.Forms.Control.KeyUp"></see> event.
  1671. /// </summary>
  1672. /// <param name="e">A <see cref="T:System.Windows.Forms.KeyEventArgs"></see> that contains the event data.</param>
  1673. protected override void OnKeyUp(KeyEventArgs e)
  1674. {
  1675. base.OnKeyUp(e);
  1676. switch (e.KeyCode)
  1677. {
  1678. case Keys.Down:
  1679. case Keys.Left:
  1680. SetProperValue(Value - (int)_smallChange);
  1681. Scroll?.Invoke(this, new ScrollEventArgs(ScrollEventType.SmallDecrement, (int)Value));
  1682. break;
  1683. case Keys.Up:
  1684. case Keys.Right:
  1685. SetProperValue(Value + (int)_smallChange);
  1686. Scroll?.Invoke(this, new ScrollEventArgs(ScrollEventType.SmallIncrement, (int)Value));
  1687. break;
  1688. case Keys.Home:
  1689. Value = _minimum;
  1690. break;
  1691. case Keys.End:
  1692. Value = _maximum;
  1693. break;
  1694. case Keys.PageDown:
  1695. SetProperValue(Value - (int)_largeChange);
  1696. Scroll?.Invoke(this, new ScrollEventArgs(ScrollEventType.LargeDecrement, (int)Value));
  1697. break;
  1698. case Keys.PageUp:
  1699. SetProperValue(Value + (int)_largeChange);
  1700. Scroll?.Invoke(this, new ScrollEventArgs(ScrollEventType.LargeIncrement, (int)Value));
  1701. break;
  1702. }
  1703. if (Scroll != null && Value == _minimum) Scroll(this, new ScrollEventArgs(ScrollEventType.First, (int)Value));
  1704. if (Scroll != null && Value == _maximum) Scroll(this, new ScrollEventArgs(ScrollEventType.Last, (int)Value));
  1705. Point pt = PointToClient(Cursor.Position);
  1706. OnMouseMove(new MouseEventArgs(MouseButtons.None, 0, pt.X, pt.Y, 0));
  1707. }
  1708.  
  1709. /// <summary>
  1710. /// Processes a dialog key.
  1711. /// </summary>
  1712. /// <param name="keyData">One of the <see cref="T:System.Windows.Forms.Keys"></see> values that represents the key to process.</param>
  1713. /// <returns>
  1714. /// true if the key was processed by the control; otherwise, false.
  1715. /// </returns>
  1716. protected override bool ProcessDialogKey(Keys keyData)
  1717. {
  1718. if (keyData == Keys.Tab | ModifierKeys == Keys.Shift)
  1719. return base.ProcessDialogKey(keyData);
  1720. else
  1721. {
  1722. OnKeyDown(new KeyEventArgs(keyData));
  1723. return true;
  1724. }
  1725. }
  1726.  
  1727. #endregion
  1728.  
  1729. #region Help routines
  1730.  
  1731. /// <summary>
  1732. /// Creates the round rect path.
  1733. /// </summary>
  1734. /// <param name="rect">The rectangle on which graphics path will be spanned.</param>
  1735. /// <param name="size">The size of rounded rectangle edges.</param>
  1736. /// <returns></returns>
  1737. public static GraphicsPath CreateRoundRectPath(Rectangle rect, Size size)
  1738. {
  1739. GraphicsPath gp = new GraphicsPath();
  1740. gp.AddLine(rect.Left + size.Width / 2, rect.Top, rect.Right - size.Width / 2, rect.Top);
  1741. gp.AddArc(rect.Right - size.Width, rect.Top, size.Width, size.Height, 270, 90);
  1742.  
  1743. gp.AddLine(rect.Right, rect.Top + size.Height / 2, rect.Right, rect.Bottom - size.Width / 2);
  1744. gp.AddArc(rect.Right - size.Width, rect.Bottom - size.Height, size.Width, size.Height, 0, 90);
  1745.  
  1746. gp.AddLine(rect.Right - size.Width / 2, rect.Bottom, rect.Left + size.Width / 2, rect.Bottom);
  1747. gp.AddArc(rect.Left, rect.Bottom - size.Height, size.Width, size.Height, 90, 90);
  1748.  
  1749. gp.AddLine(rect.Left, rect.Bottom - size.Height / 2, rect.Left, rect.Top + size.Height / 2);
  1750. gp.AddArc(rect.Left, rect.Top, size.Width, size.Height, 180, 90);
  1751. return gp;
  1752. }
  1753.  
  1754. /// <summary>
  1755. /// Desaturates colors from given array.
  1756. /// </summary>
  1757. /// <param name="colorsToDesaturate">The colors to be desaturated.</param>
  1758. /// <returns></returns>
  1759. public static Color[] DesaturateColors(params Color[] colorsToDesaturate)
  1760. {
  1761. Color[] colorsToReturn = new Color[colorsToDesaturate.Length];
  1762. for (int i = 0; i < colorsToDesaturate.Length; i++)
  1763. {
  1764. //use NTSC weighted avarage
  1765. int gray =
  1766. (int)(colorsToDesaturate[i].R * 0.3 + colorsToDesaturate[i].G * 0.6 + colorsToDesaturate[i].B * 0.1);
  1767. colorsToReturn[i] = Color.FromArgb(-0x010101 * (255 - gray) - 1);
  1768. }
  1769. return colorsToReturn;
  1770. }
  1771.  
  1772. /// <summary>
  1773. /// Lightens colors from given array.
  1774. /// </summary>
  1775. /// <param name="colorsToLighten">The colors to lighten.</param>
  1776. /// <returns></returns>
  1777. public static Color[] LightenColors(params Color[] colorsToLighten)
  1778. {
  1779. Color[] colorsToReturn = new Color[colorsToLighten.Length];
  1780. for (int i = 0; i < colorsToLighten.Length; i++)
  1781. {
  1782. colorsToReturn[i] = ControlPaint.Light(colorsToLighten[i]);
  1783. }
  1784. return colorsToReturn;
  1785. }
  1786.  
  1787. /// <summary>
  1788. /// Sets the trackbar value so that it wont exceed allowed range.
  1789. /// </summary>
  1790. /// <param name="val">The value.</param>
  1791. private void SetProperValue(decimal val)
  1792. {
  1793. if (val < _minimum) Value = _minimum;
  1794. else if (val > _maximum) Value = _maximum;
  1795. else Value = val;
  1796. }
  1797.  
  1798. /// <summary>
  1799. /// Determines whether rectangle contains given point.
  1800. /// </summary>
  1801. /// <param name="pt">The point to test.</param>
  1802. /// <param name="rect">The base rectangle.</param>
  1803. /// <returns>
  1804. /// <c>true</c> if rectangle contains given point; otherwise, <c>false</c>.
  1805. /// </returns>
  1806. private static bool IsPointInRect(Point pt, Rectangle rect)
  1807. {
  1808. if (pt.X > rect.Left & pt.X < rect.Right & pt.Y > rect.Top & pt.Y < rect.Bottom)
  1809. return true;
  1810. else return false;
  1811. }
  1812.  
  1813. #endregion
  1814. }
  1815. }
Advertisement
Add Comment
Please, Sign In to add comment