.Net Applikation für den PC um den Arduino-FSRemotePowerSwitch zu steuern
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System.Windows.Forms;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.ComponentModel;
  5. namespace FSRemotePowerSwitch {
  6. public class SButton : CheckBox
  7. {
  8. //Fields
  9. private Color onBackColor = Color.MediumSlateBlue;
  10. private Color onToggleColor = Color.WhiteSmoke;
  11. private Color offBackColor = Color.Gray;
  12. private Color offToggleColor = Color.Gainsboro;
  13. private bool solidStyle = true;
  14. //Properties
  15. public Color OnBackColor
  16. {
  17. get { return onBackColor; }
  18. set
  19. {
  20. onBackColor = value;
  21. this.Invalidate();
  22. }
  23. }
  24. public Color OnToggleColor
  25. {
  26. get { return onToggleColor; }
  27. set
  28. {
  29. onToggleColor = value;
  30. this.Invalidate();
  31. }
  32. }
  33. public Color OffBackColor
  34. {
  35. get { return offBackColor; }
  36. set
  37. {
  38. offBackColor = value;
  39. this.Invalidate();
  40. }
  41. }
  42. public Color OffToggleColor
  43. {
  44. get { return offToggleColor; }
  45. set
  46. {
  47. offToggleColor = value;
  48. this.Invalidate();
  49. }
  50. }
  51. [Browsable(false)]
  52. public override string Text
  53. {
  54. get { return base.Text; }
  55. set { }
  56. }
  57. [DefaultValue(true)]
  58. public bool SolidStyle
  59. {
  60. get { return solidStyle; }
  61. set
  62. {
  63. solidStyle = value;
  64. this.Invalidate();
  65. }
  66. }
  67. //Constructor
  68. public SButton()
  69. {
  70. this.MinimumSize = new Size(45, 22);
  71. }
  72. //Methods
  73. private GraphicsPath GetFigurePath()
  74. {
  75. int arcSize = this.Height - 1;
  76. Rectangle leftArc = new Rectangle(0, 0, arcSize, arcSize);
  77. Rectangle rightArc = new Rectangle(this.Width - arcSize - 2, 0, arcSize, arcSize);
  78. GraphicsPath path = new GraphicsPath();
  79. path.StartFigure();
  80. path.AddArc(leftArc, 90, 180);
  81. path.AddArc(rightArc, 270, 180);
  82. path.CloseFigure();
  83. return path;
  84. }
  85. protected override void OnPaint(PaintEventArgs pevent)
  86. {
  87. int toggleSize = this.Height - 5;
  88. pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  89. pevent.Graphics.Clear(this.Parent.BackColor);
  90. if (this.Checked) //ON
  91. {
  92. //Draw the control surface
  93. if (solidStyle)
  94. pevent.Graphics.FillPath(new SolidBrush(onBackColor), GetFigurePath());
  95. else pevent.Graphics.DrawPath(new Pen(onBackColor, 2), GetFigurePath());
  96. //Draw the toggle
  97. pevent.Graphics.FillEllipse(new SolidBrush(onToggleColor),
  98. new Rectangle(this.Width - this.Height + 1, 2, toggleSize, toggleSize));
  99. }
  100. else //OFF
  101. {
  102. //Draw the control surface
  103. if (solidStyle)
  104. pevent.Graphics.FillPath(new SolidBrush(offBackColor), GetFigurePath());
  105. else pevent.Graphics.DrawPath(new Pen(offBackColor, 2), GetFigurePath());
  106. //Draw the toggle
  107. pevent.Graphics.FillEllipse(new SolidBrush(offToggleColor),
  108. new Rectangle(2, 2, toggleSize, toggleSize));
  109. }
  110. }
  111. private void InitializeComponent() {
  112. this.SuspendLayout();
  113. this.ResumeLayout(false);
  114. }
  115. }
  116. public class RoundedButton : Button {
  117. public RoundedButton() {
  118. this.BackColor = Color.OrangeRed;
  119. this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  120. this.FlatAppearance.BorderColor = Color.Black;
  121. this.FlatAppearance.BorderSize = 1;
  122. this.Width = 140;
  123. this.Height = 45;
  124. }
  125. public int rdus = 30;
  126. System.Drawing.Drawing2D.GraphicsPath GetRoundPath(RectangleF Rect, int radius) {
  127. float r2 = radius / 2f;
  128. System.Drawing.Drawing2D.GraphicsPath GraphPath = new System.Drawing.Drawing2D.GraphicsPath();
  129. GraphPath.AddArc(Rect.X, Rect.Y, radius, radius, 180, 90);
  130. GraphPath.AddLine(Rect.X + r2, Rect.Y, Rect.Width - r2, Rect.Y);
  131. GraphPath.AddArc(Rect.X + Rect.Width - radius, Rect.Y, radius, radius, 270, 90);
  132. GraphPath.AddLine(Rect.Width, Rect.Y + r2, Rect.Width, Rect.Height - r2);
  133. GraphPath.AddArc(Rect.X + Rect.Width - radius,
  134. Rect.Y + Rect.Height - radius, radius, radius, 0, 90);
  135. GraphPath.AddLine(Rect.Width - r2, Rect.Height, Rect.X + r2, Rect.Height);
  136. GraphPath.AddArc(Rect.X, Rect.Y + Rect.Height - radius, radius, radius, 90, 90);
  137. GraphPath.AddLine(Rect.X, Rect.Height - r2, Rect.X, Rect.Y + r2);
  138. GraphPath.CloseFigure();
  139. return GraphPath;
  140. }
  141. protected override void OnPaint(PaintEventArgs e) {
  142. base.OnPaint(e);
  143. RectangleF Rect = new RectangleF(0, 0, this.Width, this.Height);
  144. using (System.Drawing.Drawing2D.GraphicsPath GraphPath = GetRoundPath(Rect, rdus)) {
  145. this.Region = new Region(GraphPath);
  146. using (Pen pen = new Pen(Color.CadetBlue, 1.75f)) {
  147. pen.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
  148. e.Graphics.DrawPath(pen, GraphPath);
  149. }
  150. }
  151. }
  152. }
  153. }