UserLantern.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace HslCommunication.Controls
  10. {
  11. /// <summary>
  12. /// 信号灯的控件类
  13. /// </summary>
  14. public partial class UserLantern : UserControl
  15. {
  16. /// <summary>
  17. /// 实例化一个信号灯控件的对象
  18. /// </summary>
  19. public UserLantern( )
  20. {
  21. InitializeComponent( );
  22. DoubleBuffered = true;
  23. brush_lantern_background = new SolidBrush( color_lantern_background );
  24. pen_lantern_background = new Pen( color_lantern_background, 2f );
  25. }
  26. private void UserLantern_Load( object sender, EventArgs e )
  27. {
  28. }
  29. private void UserLantern_Paint( object sender, PaintEventArgs e )
  30. {
  31. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  32. e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  33. Point center = GetCenterPoint( );
  34. e.Graphics.TranslateTransform( center.X, center.Y );
  35. int radius = (center.X - 5);
  36. if (radius < 5) return;
  37. Rectangle rectangle_larger = new Rectangle( -radius - 4, -radius - 4, 2 * radius + 8, 2 * radius + 8 );
  38. Rectangle rectangle = new Rectangle( -radius, -radius, 2 * radius, 2 * radius );
  39. e.Graphics.DrawEllipse( pen_lantern_background, rectangle_larger );
  40. e.Graphics.FillEllipse( brush_lantern_background, rectangle );
  41. }
  42. #region Private Member
  43. private Color color_lantern_background = Color.LimeGreen; // 按钮的背景颜色,包括边线颜色
  44. private Brush brush_lantern_background = null; // 按钮的背景画刷
  45. private Pen pen_lantern_background = null; // 按钮的背景画笔
  46. #endregion
  47. #region Public Member
  48. /// <summary>
  49. /// 获取或设置开关按钮的背景色
  50. /// </summary>
  51. [Browsable( true )]
  52. [Description( "获取或设置信号灯的背景色" )]
  53. [Category( "外观" )]
  54. [DefaultValue( typeof( Color ), "LimeGreen" )]
  55. public Color LanternBackground
  56. {
  57. get
  58. {
  59. return color_lantern_background;
  60. }
  61. set
  62. {
  63. color_lantern_background = value;
  64. brush_lantern_background?.Dispose( );
  65. pen_lantern_background?.Dispose( );
  66. brush_lantern_background = new SolidBrush( color_lantern_background );
  67. pen_lantern_background = new Pen( color_lantern_background, 2f );
  68. Invalidate( );
  69. }
  70. }
  71. #endregion
  72. #region Private Method
  73. private Point GetCenterPoint( )
  74. {
  75. if (Height > Width)
  76. {
  77. return new Point( (Width - 1) / 2, (Width - 1) / 2 );
  78. }
  79. else
  80. {
  81. return new Point( (Height - 1) / 2, (Height - 1) / 2 );
  82. }
  83. }
  84. #endregion
  85. }
  86. }