.Net-Komponente die einen Drehregler-Konpf darstellt
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Utility.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #region License
  2. /* Copyright (c) 2017 Fabrice Lacharme
  3. * This code was originally written by Jigar Desai
  4. * http://www.c-sharpcorner.com/article/knob-control-using-windows-forms-and-gdi/
  5. * Note that another implementation exists in vb.net by Blong
  6. * https://www.codeproject.com/Articles/2563/VB-NET-Knob-Control-using-Windows-Forms-and-GDI?msg=1884770#xx1884770xx
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to
  10. * deal in the Software without restriction, including without limitation the
  11. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #endregion
  27. #region Contact
  28. /*
  29. * Fabrice Lacharme
  30. * Email: fabrice.lacharme@gmail.com
  31. */
  32. #endregion
  33. using System;
  34. using System.Drawing;
  35. namespace KnobControl {
  36. /// <summary>
  37. /// Summary description for Utility.
  38. /// </summary>
  39. public class Utility {
  40. public static float GetRadian(float val) {
  41. return (float)(val * Math.PI / 180);
  42. }
  43. public static Color GetDarkColor(Color c, byte d) {
  44. byte r = 0;
  45. byte g = 0;
  46. byte b = 0;
  47. if (c.R > d) r = (byte)(c.R - d);
  48. if (c.G > d) g = (byte)(c.G - d);
  49. if (c.B > d) b = (byte)(c.B - d);
  50. Color c1 = Color.FromArgb(r, g, b);
  51. return c1;
  52. }
  53. public static Color GetLightColor(Color c, byte d) {
  54. byte r = 255;
  55. byte g = 255;
  56. byte b = 255;
  57. if (c.R + d < 255) r = (byte)(c.R + d);
  58. if (c.G + d < 255) g = (byte)(c.G + d);
  59. if (c.B + d < 255) b = (byte)(c.B + d);
  60. Color c2 = Color.FromArgb(r, g, b);
  61. return c2;
  62. }
  63. /// <summary>
  64. /// Method which checks is particular point is in rectangle
  65. /// </summary>
  66. /// <param name="p">Point to be Chaecked</param>
  67. /// <param name="r">Rectangle</param>
  68. /// <returns>true is Point is in rectangle, else false</returns>
  69. public static bool IsPointinRectangle(Point p, Rectangle r) {
  70. bool flag = false;
  71. if (p.X > r.X && p.X < r.X + r.Width && p.Y > r.Y && p.Y < r.Y + r.Height) {
  72. flag = true;
  73. }
  74. return flag;
  75. }
  76. public static bool IsPointInCircle(Point p, Point center, double diameter) {
  77. bool flag = false;
  78. double distToCenter = Math.Sqrt(Math.Pow(p.X - center.X, 2) + Math.Pow(p.Y - center.Y, 2));
  79. if (distToCenter <= diameter / 2) {
  80. flag = true;
  81. }
  82. return flag;
  83. }
  84. public static void DrawInsetCircle(ref Graphics g, Rectangle r, Pen p) {
  85. Pen p1 = new Pen(GetDarkColor(p.Color, 50));
  86. Pen p2 = new Pen(GetLightColor(p.Color, 50));
  87. for (int i = 0; i < p.Width; i++) {
  88. Rectangle r1 = new Rectangle(r.X + i, r.Y + i, r.Width - i * 2, r.Height - i * 2);
  89. g.DrawArc(p2, r1, -45, 180);
  90. g.DrawArc(p1, r1, 135, 180);
  91. }
  92. }
  93. public static Rectangle shrinkRectangle(Rectangle r, int shrinkWidth) {
  94. int x = r.X + shrinkWidth / 2;
  95. int y = r.Y + shrinkWidth / 2;
  96. int w = r.Width - shrinkWidth;
  97. int h = r.Height - shrinkWidth;
  98. return new Rectangle(x, y, w, h);
  99. }
  100. }
  101. }