using Model; using Permission; using Sunny.UI; using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; [ToolboxItem(true)] [Description("提示灯控件")] public class MyUiLight : UIControl, IPermissionControl { private Timer timer; private UIShape sharpType; private int interval = 500; private UILightState state; private bool blinkState; private bool showCenterColor = true; private bool showLightLine; private Color onColor = UIColor.Green; private Color centerColor = UIColor.White; private Color offCenterColor = UIColor.White; private Color offColor = UIColor.Gray; [Category("MyControl")] [Description("控件要求的权限等级")] [Browsable(true)] [DefaultValue(PermissionLevel.操作工)] public PermissionLevel RequiredPermissionLevel { get => requiredPermissionLevel; set { requiredPermissionLevel = value; IsPermissionGot = currentPermissionLevel >= RequiredPermissionLevel; HandlePermissionChange(); } } private PermissionLevel requiredPermissionLevel = PermissionLevel.操作工; [Category("MyControl")] [Description("当前登录的权限")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public PermissionLevel CurrentPermissionLevel { get => currentPermissionLevel; set { currentPermissionLevel = value; IsPermissionGot = currentPermissionLevel >= RequiredPermissionLevel; HandlePermissionChange(); } } private PermissionLevel currentPermissionLevel = PermissionLevel.无权限; [Category("MyControl")] [Description("控件是否获得了权限")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public bool IsPermissionGot { get; private set; } protected virtual void HandlePermissionChange() { this.Enabled = IsPermissionGot; } [DefaultValue(UIShape.Circle)] [Description("显示形状:圆形,正方形")] [Category("SunnyUI")] public UIShape Shape { get { return sharpType; } set { if (sharpType != value) { sharpType = value; Invalidate(); } } } [Description("是否显示文字")] [Category("SunnyUI")] [DefaultValue(false)] public new bool ShowText { get { return base.ShowText; } set { base.ShowText = value; } } [DefaultValue(500)] [Description("显示间隔")] [Category("SunnyUI")] public int Interval { get { return interval; } set { interval = Math.Max(100, value); interval = Math.Min(interval, 10000); if (timer != null) { bool enabled = timer.Enabled; timer.Stop(); timer.Interval = interval; if (enabled) { timer.Start(); } } } } [DefaultValue(UILightState.On)] [Description("显示状态")] [Category("SunnyUI")] public UILightState State { get { return state; } set { state = value; timer?.Stop(); if (state == UILightState.Blink) { if (timer == null) { timer = new Timer { Interval = interval }; timer.Tick += Timer_Tick; } blinkState = true; timer.Start(); } Invalidate(); } } [DefaultValue(true)] [Description("是否显示中心颜色")] [Category("SunnyUI")] public bool ShowCenterColor { get { return showCenterColor; } set { showCenterColor = value; Invalidate(); } } [DefaultValue(false)] [Description("显示灯光亮线")] [Category("SunnyUI")] public bool ShowLightLine { get { return showLightLine; } set { showLightLine = value; Invalidate(); } } [DefaultValue(typeof(Color), "110, 190, 40")] [Description("打开状态颜色")] [Category("SunnyUI")] public Color OnColor { get { return onColor; } set { onColor = value; Invalidate(); } } [DefaultValue(typeof(Color), "White")] [Description("中心颜色")] [Category("SunnyUI")] [Browsable(false)] public Color CenterColor { get { return centerColor; } set { centerColor = value; Invalidate(); } } [DefaultValue(typeof(Color), "White")] [Description("中心颜色")] [Category("SunnyUI")] public Color OnCenterColor { get { return CenterColor; } set { CenterColor = value; } } [DefaultValue(typeof(Color), "White")] [Description("中心颜色")] [Category("SunnyUI")] public Color OffCenterColor { get { return offCenterColor; } set { offCenterColor = value; Invalidate(); } } [DefaultValue(typeof(Color), "140, 140, 140")] [Description("关闭状态颜色")] [Category("SunnyUI")] public Color OffColor { get { return offColor; } set { offColor = value; Invalidate(); } } [DefaultValue(false)] [Description("Text属性是否从图案左侧开始输出")] [Category("SunnyUI")] public bool TextCoverSymbol { get { return textCoverSymbol; } set { textCoverSymbol = value; Invalidate(); } } [Description("文字对齐方向")] [Category("SunnyUI")] public ContentAlignment MyTextAlign { get { return base.TextAlign; } set { //if (textAlign != value) //{ // textAlign = value; // Invalidate(); //} base.TextAlign = value; } } //private ContentAlignment textAlign; private bool textCoverSymbol; public MyUiLight() { SetStyleFlags(supportTransparent: true, selectable: false); base.ShowRect = false; base.ShowText = false; base.Radius = (base.Width = (base.Height = 35)); } protected override void OnPaintFore(Graphics g, GraphicsPath path) { if(!textCoverSymbol) g.DrawString(Text, Font, ForeColor, base.ClientRectangle, base.TextAlign,offsetX:base.Height); else g.DrawString(Text, Font, ForeColor, base.ClientRectangle, base.TextAlign); } private void Timer_Tick(object sender, EventArgs e) { blinkState = !blinkState; Invalidate(); } protected override void OnPaintFill(Graphics g, GraphicsPath path) { int num = Math.Min(base.Width, base.Height); Color color = ((State == UILightState.On) ? OnColor : ((State != UILightState.Off) ? (blinkState ? onColor : offColor) : OffColor)); Color color2 = ((State == UILightState.On) ? OnCenterColor : ((State != UILightState.Off) ? (blinkState ? OnCenterColor : offCenterColor) : offCenterColor)); if (Shape == UIShape.Circle) { if (base.Radius != num) { base.Radius = num; } using (GraphicsPath graphicsPath = new GraphicsPath()) { graphicsPath.AddEllipse(2, 2, num - 4, num - 4); g.Smooth(); if (ShowCenterColor) { Color[] surroundColors = new Color[1] { color }; using (GraphicsPath path2 = base.ClientRectangle.CreateTrueRoundedRectanglePath(base.Height)) { using (PathGradientBrush pathGradientBrush = new PathGradientBrush(path2)) { pathGradientBrush.CenterPoint = new PointF((float)num / 2f, (float)num / 2f); pathGradientBrush.CenterColor = color2; pathGradientBrush.SurroundColors = surroundColors; g.FillPath(pathGradientBrush, graphicsPath); } } } else { g.FillPath(color, graphicsPath); } } if (ShowLightLine) { int num2 = (num - 4) / 5; g.DrawArc(color2, num2, num2, num - num2 * 2, num - num2 * 2, 45, -155); } } if (Shape != UIShape.Square) { return; } if (base.Radius != 0) { base.Radius = 0; } g.FillRoundRectangle(color, 2, 2, num - 4, num - 4, 5); if (!ShowCenterColor) { return; } using (GraphicsPath graphicsPath2 = new GraphicsPath()) { Point[] points = new Point[4] { new Point(3, 3), new Point(num - 3, 3), new Point(num - 3, num - 3), new Point(3, num - 3) }; graphicsPath2.AddLines(points); g.Smooth(); Color[] surroundColors2 = new Color[1] { color }; using (PathGradientBrush pathGradientBrush2 = new PathGradientBrush(path)) { pathGradientBrush2.CenterPoint = new PointF((float)num / 2f, (float)num / 2f); pathGradientBrush2.CenterColor = color2; pathGradientBrush2.SurroundColors = surroundColors2; g.FillPath(pathGradientBrush2, graphicsPath2); } } } }