MyUiButton.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Model;
  2. using Permission;
  3. using PlcCom;
  4. using Sunny.UI;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Drawing;
  9. using System.Drawing.Design;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. public class MyUiButton : UISymbolButton,IPermissionControl, IActionableControl
  15. {
  16. [Category("MyControl")]
  17. [Description("是否需要用户二次确认")]
  18. public bool NeedUserEnsure { get; set; }
  19. [Category("MyControl")]
  20. [Description("二次确认时的信息提示")]
  21. [DefaultValue(null)]
  22. public string EnsureTip { get; set; }
  23. [Category("MyControl")]
  24. [Description("控件要求的权限等级")]
  25. [Browsable(true)]
  26. [DefaultValue(PermissionLevel.操作工)]
  27. public PermissionLevel RequiredPermissionLevel { get => requiredPermissionLevel; set { requiredPermissionLevel = value; IsPermissionGot = currentPermissionLevel >= requiredPermissionLevel; HandlePermissionChange(); } }
  28. private PermissionLevel requiredPermissionLevel = PermissionLevel.操作工;
  29. [Category("MyControl")]
  30. [Description("当前登录的权限")]
  31. [Browsable(false)]
  32. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  33. public PermissionLevel CurrentPermissionLevel { get => currentPermissionLevel; set { currentPermissionLevel = value; IsPermissionGot = currentPermissionLevel >= requiredPermissionLevel; HandlePermissionChange(); } }
  34. private PermissionLevel currentPermissionLevel = PermissionLevel.无权限;
  35. [Category("MyControl")]
  36. [Description("控件是否获得了权限")]
  37. [Browsable(false)]
  38. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  39. public bool IsPermissionGot { get; private set; }
  40. protected virtual void HandlePermissionChange()
  41. {
  42. this.Enabled = IsPermissionGot;
  43. }
  44. public delegate void OptionSelectHandler(object sender, DialogResult dialogResult);
  45. [Category("MyControl")]
  46. [Description("二次确认时用户选择事件")]
  47. [Browsable(true)]
  48. public event OptionSelectHandler OptionSelect;
  49. public MyUiButton()
  50. {
  51. }
  52. protected sealed override void OnMouseDown(MouseEventArgs e)
  53. {
  54. OnCustomMouseDown(e);
  55. base.OnMouseDown(e);
  56. }
  57. protected sealed override void OnMouseUp(MouseEventArgs e)
  58. {
  59. OnCustomMouseUp(e);
  60. base.OnMouseUp(e);
  61. }
  62. protected virtual void OnCustomMouseDown(MouseEventArgs e)
  63. {
  64. }
  65. protected virtual void OnCustomMouseUp(MouseEventArgs e)
  66. {
  67. }
  68. protected bool IfContinue()
  69. {
  70. if (NeedUserEnsure)
  71. {
  72. string msg;
  73. if (string.IsNullOrEmpty(EnsureTip))
  74. {
  75. if(string.IsNullOrEmpty(Text))
  76. {
  77. msg = $"是否操作:{Name}?";
  78. }
  79. else
  80. {
  81. msg = $"是否操作:{Text}?";
  82. }
  83. }
  84. else
  85. {
  86. msg = EnsureTip;
  87. }
  88. var dr = MessageBox.Show(msg, "操作确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  89. OptionSelect?.Invoke(this, dr);
  90. return dr == DialogResult.Yes;
  91. }
  92. else
  93. {
  94. return true;
  95. }
  96. }
  97. }