GlobalEventsHandler.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Data;
  2. using System.Windows.Forms;
  3. using UAS_LabelMachine;
  4. using UAS_LabelMachine.Entity;
  5. using UAS_LabelMachine.PublicMethod;
  6. namespace UAS_Labeling.PublicMethod
  7. {
  8. /// <summary>
  9. /// Author zhangz 2016/12/30
  10. /// 全局事件处理,通过句柄获取触发事件的控件
  11. /// 结合捕获的全局的键盘事件来验证用户进行的操作
  12. /// </summary>
  13. class GlobalEventsHandler : IMessageFilter
  14. {
  15. DataHelper dh;
  16. //鼠标左键点击
  17. public const int WM_LBUTTONDOWN = 0x0201;
  18. //键盘敲击事件
  19. public const int WM_KEYDOWN = 0x100;
  20. public bool PreFilterMessage(ref Message m)
  21. {
  22. //通过句柄去获取具体触发事件的控件
  23. Control c = Control.FromHandle(m.HWnd);
  24. //可进行数据操作的控件需要进行权限的判断,结合WM_LBUTTONDOWN对按钮进行判断
  25. if (c != null && m.Msg == WM_LBUTTONDOWN && c is Button)
  26. {
  27. LogManager.DoLog("点击【" + c.Text+"】");
  28. return CheckPower(c);
  29. }
  30. else
  31. return false;
  32. }
  33. /// <summary>
  34. /// 返回True表示消息已处理,不再往后传递
  35. /// 返回False表示消息未处理,后续程序处理此事件
  36. /// </summary>
  37. /// <param name="c"></param>
  38. /// <returns></returns>
  39. private bool CheckPower(Control c)
  40. {
  41. dh = new DataHelper();
  42. if (User.UserAccountType != "admin")
  43. {
  44. if (dh.getRowCount("labelprintpower") > 0)
  45. {
  46. string sql = " select LPP_CLEANDETAIL,LPP_GENCODE,LPP_LABELMATAIN,LPP_SAVEDETAIL,LPP_COLLECTSTRATEGY,LPP_ALLCOLLECT from labelprintpower left join employee on em_code = lpp_emcode where lpp_emcode = '" + User.UserCode + "'";
  47. DataTable dt = (DataTable)dh.ExecuteSql(sql, "select");
  48. if (dt.Rows.Count > 0)
  49. {
  50. switch (c.Text)
  51. {
  52. case "采集策略设置":
  53. if (dt.Rows[0]["LPP_COLLECTSTRATEGY"].ToString() != "1")
  54. {
  55. MessageBox.Show("没有" + c.Text + "的权限");
  56. return true;
  57. }
  58. break;
  59. case "生成条码":
  60. if (dt.Rows[0]["LPP_GENCODE"].ToString() != "1")
  61. {
  62. MessageBox.Show("没有" + c.Text + "的权限");
  63. return true;
  64. }
  65. break;
  66. case "标签维护":
  67. if (dt.Rows[0]["LPP_LABELMATAIN"].ToString() != "1")
  68. {
  69. MessageBox.Show("没有" + c.Text + "的权限");
  70. return true;
  71. }
  72. break;
  73. case "保存明细":
  74. if (dt.Rows[0]["LPP_SAVEDETAIL"].ToString() != "1")
  75. {
  76. MessageBox.Show("没有" + c.Text + "的权限");
  77. return true;
  78. }
  79. break;
  80. case "清除明细":
  81. if (dt.Rows[0]["LPP_CLEANDETAIL"].ToString() != "1")
  82. {
  83. MessageBox.Show("没有" + c.Text + "的权限");
  84. return true;
  85. }
  86. break;
  87. case "全部已采":
  88. if (dt.Rows[0]["LPP_ALLCOLLECT"].ToString() != "1")
  89. {
  90. MessageBox.Show("没有" + c.Text + "的权限");
  91. return true;
  92. }
  93. break;
  94. default:
  95. break;
  96. }
  97. }
  98. else {
  99. MessageBox.Show("尚未配置权限,请与管理员联系");
  100. return true;
  101. }
  102. }
  103. }
  104. return false;
  105. }
  106. }
  107. }