using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace UserControlClass { public partial class Lamp : UserControl { public Lamp() { InitializeComponent(); base.SetStyle(ControlStyles.ResizeRedraw, true); this.DoubleBuffered = true; } private void Lamp_Load(object sender, EventArgs e) { } private static Color[] COLORS = new Color[] { Color.Green, Color.Orange, Color.Red, Color.White, Color.Silver, Color.Transparent, Color.Yellow, Color.Blue }; private const int OK = 0; private const int WARN = 1; private const int ERROR = 2; private const int DEBUG = 4; private const int UNDEF = -1; private int _state = -1; private string Dtext = ""; private bool _shadow = false; /// 设置颜色 public int State { get { return this._state; } set { this._state = value; this.Refresh(); } } /// 设置文字 public string LText { get { return this.Dtext; } set { this.Dtext = value; } } /// 设置阴影 public bool Shadow { get { return this._shadow; } set { this._shadow = value; } } protected override void OnPaint(PaintEventArgs gr) { base.OnPaint(gr); int num = Math.Min(base.Width, base.Height); bool flag = num >= 8; if (flag) { this.DrawLamp(gr.Graphics, num); } } private void DrawLamp(Graphics g, int rad) { g.SmoothingMode = SmoothingMode.AntiAlias; RectangleF rect = new RectangleF(1f, 1f, (float)(rad - 5), (float)(rad - 5)); float num = rect.Width / 12f; RectangleF rect2 = new RectangleF(rect.Location, rect.Size); rect2.Inflate(1f, 1f); if (_shadow) { rect2.X += num; rect2.Y += num; g.FillEllipse(Brushes.Gray, rect2); rect2.X -= num; rect2.Y -= num; } g.FillEllipse(Brushes.Black, rect2); if (_state != -1) { using (GraphicsPath graphicsPath = new GraphicsPath()) { graphicsPath.AddEllipse(rect); int x = (int)(rect.X + rect.Width / 3f); int y = (int)(rect.Y + rect.Height / 3f); Color color = Lamp.COLORS[_state]; using (PathGradientBrush pathGradientBrush = new PathGradientBrush(graphicsPath)) { pathGradientBrush.CenterColor = Color.Snow; pathGradientBrush.CenterPoint = new Point(x, y); pathGradientBrush.SurroundColors = new Color[] { color }; g.FillPath(pathGradientBrush, graphicsPath); } } } else { using (Brush brush = new SolidBrush(this.BackColor)) { g.FillEllipse(brush, rect); } } if (Dtext.Length > 0) { float emSize = rect.Height / 6f; Font font = new Font("Microsoft Sans Serif", emSize, FontStyle.Bold, GraphicsUnit.Point, 0); int num2 = (int)font.GetHeight(); int y2 = (int)rect.Height / 2 - num2 / 2; int num3 = (int)g.MeasureString(Dtext, font).Width; int num4 = (int)(rect.Width - (float)num3) / 2; bool flag3 = num4 < 0; if (flag3) { num4 = 0; } g.DrawString(Dtext, font, Brushes.Black, new Point(num4, y2)); } } } public class LampColor { public const int OK = 0; public const int WARN = 1; public const int ERROR = 2; public const int DEBUG = 4; public const int UNDEF = -1; public const int Green = 0; public const int Orange = 1; public const int Red = 2; public const int White = 3; public const int Silver = 4; public const int Transparent = 5; public const int Yellow = 6; public const int Blue = 7; } }