MyUiLight.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using Model;
  2. using Permission;
  3. using Sunny.UI;
  4. using System;
  5. using System.ComponentModel;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Windows.Forms;
  9. [ToolboxItem(true)]
  10. [Description("提示灯控件")]
  11. public class MyUiLight : UIControl, IPermissionControl
  12. {
  13. private Timer timer;
  14. private UIShape sharpType;
  15. private int interval = 500;
  16. private UILightState state;
  17. private bool blinkState;
  18. private bool showCenterColor = true;
  19. private bool showLightLine;
  20. private Color onColor = UIColor.Green;
  21. private Color centerColor = UIColor.White;
  22. private Color offCenterColor = UIColor.White;
  23. private Color offColor = UIColor.Gray;
  24. [Category("MyControl")]
  25. [Description("控件要求的权限等级")]
  26. [Browsable(true)]
  27. [DefaultValue(PermissionLevel.操作工)]
  28. public PermissionLevel RequiredPermissionLevel { get => requiredPermissionLevel; set { requiredPermissionLevel = value; IsPermissionGot = currentPermissionLevel >= RequiredPermissionLevel; HandlePermissionChange(); } }
  29. private PermissionLevel requiredPermissionLevel = PermissionLevel.操作工;
  30. [Category("MyControl")]
  31. [Description("当前登录的权限")]
  32. [Browsable(false)]
  33. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  34. public PermissionLevel CurrentPermissionLevel { get => currentPermissionLevel; set { currentPermissionLevel = value; IsPermissionGot = currentPermissionLevel >= RequiredPermissionLevel; HandlePermissionChange(); } }
  35. private PermissionLevel currentPermissionLevel = PermissionLevel.无权限;
  36. [Category("MyControl")]
  37. [Description("控件是否获得了权限")]
  38. [Browsable(false)]
  39. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  40. public bool IsPermissionGot { get; private set; }
  41. protected virtual void HandlePermissionChange()
  42. {
  43. this.Enabled = IsPermissionGot;
  44. }
  45. [DefaultValue(UIShape.Circle)]
  46. [Description("显示形状:圆形,正方形")]
  47. [Category("SunnyUI")]
  48. public UIShape Shape
  49. {
  50. get
  51. {
  52. return sharpType;
  53. }
  54. set
  55. {
  56. if (sharpType != value)
  57. {
  58. sharpType = value;
  59. Invalidate();
  60. }
  61. }
  62. }
  63. [Description("是否显示文字")]
  64. [Category("SunnyUI")]
  65. [DefaultValue(false)]
  66. public new bool ShowText
  67. {
  68. get
  69. {
  70. return base.ShowText;
  71. }
  72. set
  73. {
  74. base.ShowText = value;
  75. }
  76. }
  77. [DefaultValue(500)]
  78. [Description("显示间隔")]
  79. [Category("SunnyUI")]
  80. public int Interval
  81. {
  82. get
  83. {
  84. return interval;
  85. }
  86. set
  87. {
  88. interval = Math.Max(100, value);
  89. interval = Math.Min(interval, 10000);
  90. if (timer != null)
  91. {
  92. bool enabled = timer.Enabled;
  93. timer.Stop();
  94. timer.Interval = interval;
  95. if (enabled)
  96. {
  97. timer.Start();
  98. }
  99. }
  100. }
  101. }
  102. [DefaultValue(UILightState.On)]
  103. [Description("显示状态")]
  104. [Category("SunnyUI")]
  105. public UILightState State
  106. {
  107. get
  108. {
  109. return state;
  110. }
  111. set
  112. {
  113. state = value;
  114. timer?.Stop();
  115. if (state == UILightState.Blink)
  116. {
  117. if (timer == null)
  118. {
  119. timer = new Timer
  120. {
  121. Interval = interval
  122. };
  123. timer.Tick += Timer_Tick;
  124. }
  125. blinkState = true;
  126. timer.Start();
  127. }
  128. Invalidate();
  129. }
  130. }
  131. [DefaultValue(true)]
  132. [Description("是否显示中心颜色")]
  133. [Category("SunnyUI")]
  134. public bool ShowCenterColor
  135. {
  136. get
  137. {
  138. return showCenterColor;
  139. }
  140. set
  141. {
  142. showCenterColor = value;
  143. Invalidate();
  144. }
  145. }
  146. [DefaultValue(false)]
  147. [Description("显示灯光亮线")]
  148. [Category("SunnyUI")]
  149. public bool ShowLightLine
  150. {
  151. get
  152. {
  153. return showLightLine;
  154. }
  155. set
  156. {
  157. showLightLine = value;
  158. Invalidate();
  159. }
  160. }
  161. [DefaultValue(typeof(Color), "110, 190, 40")]
  162. [Description("打开状态颜色")]
  163. [Category("SunnyUI")]
  164. public Color OnColor
  165. {
  166. get
  167. {
  168. return onColor;
  169. }
  170. set
  171. {
  172. onColor = value;
  173. Invalidate();
  174. }
  175. }
  176. [DefaultValue(typeof(Color), "White")]
  177. [Description("中心颜色")]
  178. [Category("SunnyUI")]
  179. [Browsable(false)]
  180. public Color CenterColor
  181. {
  182. get
  183. {
  184. return centerColor;
  185. }
  186. set
  187. {
  188. centerColor = value;
  189. Invalidate();
  190. }
  191. }
  192. [DefaultValue(typeof(Color), "White")]
  193. [Description("中心颜色")]
  194. [Category("SunnyUI")]
  195. public Color OnCenterColor
  196. {
  197. get
  198. {
  199. return CenterColor;
  200. }
  201. set
  202. {
  203. CenterColor = value;
  204. }
  205. }
  206. [DefaultValue(typeof(Color), "White")]
  207. [Description("中心颜色")]
  208. [Category("SunnyUI")]
  209. public Color OffCenterColor
  210. {
  211. get
  212. {
  213. return offCenterColor;
  214. }
  215. set
  216. {
  217. offCenterColor = value;
  218. Invalidate();
  219. }
  220. }
  221. [DefaultValue(typeof(Color), "140, 140, 140")]
  222. [Description("关闭状态颜色")]
  223. [Category("SunnyUI")]
  224. public Color OffColor
  225. {
  226. get
  227. {
  228. return offColor;
  229. }
  230. set
  231. {
  232. offColor = value;
  233. Invalidate();
  234. }
  235. }
  236. [DefaultValue(false)]
  237. [Description("Text属性是否从图案左侧开始输出")]
  238. [Category("SunnyUI")]
  239. public bool TextCoverSymbol
  240. {
  241. get
  242. {
  243. return textCoverSymbol;
  244. }
  245. set
  246. {
  247. textCoverSymbol = value;
  248. Invalidate();
  249. }
  250. }
  251. [Description("文字对齐方向")]
  252. [Category("SunnyUI")]
  253. public ContentAlignment MyTextAlign
  254. {
  255. get
  256. {
  257. return base.TextAlign;
  258. }
  259. set
  260. {
  261. //if (textAlign != value)
  262. //{
  263. // textAlign = value;
  264. // Invalidate();
  265. //}
  266. base.TextAlign = value;
  267. }
  268. }
  269. //private ContentAlignment textAlign;
  270. private bool textCoverSymbol;
  271. public MyUiLight()
  272. {
  273. SetStyleFlags(supportTransparent: true, selectable: false);
  274. base.ShowRect = false;
  275. base.ShowText = false;
  276. base.Radius = (base.Width = (base.Height = 35));
  277. }
  278. protected override void OnPaintFore(Graphics g, GraphicsPath path)
  279. {
  280. if(!textCoverSymbol)
  281. g.DrawString(Text, Font, ForeColor, base.ClientRectangle, base.TextAlign,offsetX:base.Height);
  282. else
  283. g.DrawString(Text, Font, ForeColor, base.ClientRectangle, base.TextAlign);
  284. }
  285. private void Timer_Tick(object sender, EventArgs e)
  286. {
  287. blinkState = !blinkState;
  288. Invalidate();
  289. }
  290. protected override void OnPaintFill(Graphics g, GraphicsPath path)
  291. {
  292. int num = Math.Min(base.Width, base.Height);
  293. Color color = ((State == UILightState.On) ? OnColor : ((State != UILightState.Off) ? (blinkState ? onColor : offColor) : OffColor));
  294. Color color2 = ((State == UILightState.On) ? OnCenterColor : ((State != UILightState.Off) ? (blinkState ? OnCenterColor : offCenterColor) : offCenterColor));
  295. if (Shape == UIShape.Circle)
  296. {
  297. if (base.Radius != num)
  298. {
  299. base.Radius = num;
  300. }
  301. using (GraphicsPath graphicsPath = new GraphicsPath())
  302. {
  303. graphicsPath.AddEllipse(2, 2, num - 4, num - 4);
  304. g.Smooth();
  305. if (ShowCenterColor)
  306. {
  307. Color[] surroundColors = new Color[1] { color };
  308. using (GraphicsPath path2 = base.ClientRectangle.CreateTrueRoundedRectanglePath(base.Height))
  309. {
  310. using (PathGradientBrush pathGradientBrush = new PathGradientBrush(path2))
  311. {
  312. pathGradientBrush.CenterPoint = new PointF((float)num / 2f, (float)num / 2f);
  313. pathGradientBrush.CenterColor = color2;
  314. pathGradientBrush.SurroundColors = surroundColors;
  315. g.FillPath(pathGradientBrush, graphicsPath);
  316. }
  317. }
  318. }
  319. else
  320. {
  321. g.FillPath(color, graphicsPath);
  322. }
  323. }
  324. if (ShowLightLine)
  325. {
  326. int num2 = (num - 4) / 5;
  327. g.DrawArc(color2, num2, num2, num - num2 * 2, num - num2 * 2, 45, -155);
  328. }
  329. }
  330. if (Shape != UIShape.Square)
  331. {
  332. return;
  333. }
  334. if (base.Radius != 0)
  335. {
  336. base.Radius = 0;
  337. }
  338. g.FillRoundRectangle(color, 2, 2, num - 4, num - 4, 5);
  339. if (!ShowCenterColor)
  340. {
  341. return;
  342. }
  343. using (GraphicsPath graphicsPath2 = new GraphicsPath())
  344. {
  345. Point[] points = new Point[4]
  346. {
  347. new Point(3, 3),
  348. new Point(num - 3, 3),
  349. new Point(num - 3, num - 3),
  350. new Point(3, num - 3)
  351. };
  352. graphicsPath2.AddLines(points);
  353. g.Smooth();
  354. Color[] surroundColors2 = new Color[1] { color };
  355. using (PathGradientBrush pathGradientBrush2 = new PathGradientBrush(path))
  356. {
  357. pathGradientBrush2.CenterPoint = new PointF((float)num / 2f, (float)num / 2f);
  358. pathGradientBrush2.CenterColor = color2;
  359. pathGradientBrush2.SurroundColors = surroundColors2;
  360. g.FillPath(pathGradientBrush2, graphicsPath2);
  361. }
  362. }
  363. }
  364. }