GlobalEventsHandler.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System.Windows.Forms;
  2. using UAS_MES.CustomControl.ButtonUtil;
  3. using UAS_MES.CustomControl.ClickPicBox;
  4. using UAS_MES.CustomControl.TextBoxWithIcon;
  5. using UAS_MES.Entity;
  6. namespace UAS_MES.PublicMethod
  7. {
  8. /// <summary>
  9. /// Author zhangz 2016/12/30
  10. /// 全局事件处理,通过句柄获取触发事件的控件
  11. /// 结合捕获的全局的键盘事件来验证用户进行的操作
  12. /// </summary>
  13. class GlobalEventsHandler : IMessageFilter
  14. {
  15. public static Keys keycode;
  16. //鼠标左键点击
  17. public const int WM_LBUTTONDOWN = 0x0201;
  18. //键盘敲击事件
  19. public const int WM_KEYDOWN = 0x100;
  20. private MessageBoxButtons messButton = MessageBoxButtons.OK;
  21. private MessageBoxIcon messIcon = MessageBoxIcon.Exclamation;
  22. string Type = "";
  23. string Type1 = "";
  24. public bool PreFilterMessage(ref Message m)
  25. {
  26. //通过句柄去获取具体触发事件的控件
  27. Control c = Control.FromHandle(m.HWnd);
  28. //可进行数据操作的控件需要进行权限的判断,结合WM_LBUTTONDOWN对按钮进行判断
  29. if (c != null && m.Msg == WM_LBUTTONDOWN && (c is NormalButton || c is DeleteButton || c is Button || c is ClickPicBox || c is SearchTextBox))
  30. {
  31. //获取控件的类型
  32. return CheckControlPower(c);
  33. }
  34. //集合WM_KEYDOWN对输入框进行判断
  35. else if (c != null && m.Msg == WM_KEYDOWN && (c is EnterTextBox || c is TextBox))
  36. {
  37. string ControlType = c.GetType().ToString();
  38. switch (ControlType.Substring(ControlType.LastIndexOf(".")))
  39. {
  40. case "EnterTextBox": break;
  41. default: break;
  42. }
  43. //在TextBox或者EnterBox中按下了回车键
  44. if (keycode == Keys.Return)
  45. {
  46. keycode = Keys.None;
  47. return CheckControlPower(c);
  48. }
  49. else
  50. return false;
  51. }
  52. else
  53. return false;
  54. }
  55. /// <summary>
  56. /// 返回True表示消息已处理,不再往后传递
  57. /// 返回False表示消息未处理,后续程序处理此事件
  58. /// </summary>
  59. /// <param name="c"></param>
  60. /// <returns></returns>
  61. private bool CheckControlPower(Control c)
  62. {
  63. //弹出的小窗体不做校验
  64. if (c.FindForm().Tag.ToString() == "ShowDialogWindow")
  65. return false;
  66. //获取控件的类型
  67. string ControlType = c.GetType().ToString();
  68. switch (ControlType.Substring(ControlType.LastIndexOf(".") + 1))
  69. {
  70. case "NormalButton":
  71. LogManager.DoLog(c.FindForm().Text + "窗体" + "点击按钮【" + c.Text + "】");
  72. //判断是否拥有全部权限
  73. if ((c as NormalButton).AllPower != null)
  74. {
  75. Type = "IFALL";
  76. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  77. return false;
  78. }
  79. if ((c as NormalButton).Power != null)
  80. {
  81. switch ((c as NormalButton).Power.ToString().ToUpper())
  82. {
  83. case "IFREAD":
  84. Type = "IFREAD";
  85. Type1 = "读取";
  86. break;
  87. case "IFWRITE":
  88. Type = "IFWRITE";
  89. Type1 = "修改";
  90. break;
  91. case "IFDELETE":
  92. Type = "IFDELETE";
  93. Type1 = "删除";
  94. break;
  95. case "IFSPECIAL":
  96. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  97. return false;
  98. else
  99. {
  100. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  101. return true;
  102. }
  103. default:
  104. break;
  105. }
  106. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  107. return false;
  108. else
  109. {
  110. MessageBox.Show("没有" + Type1 + "数据的权限", "提示", messButton, messIcon);
  111. return true;
  112. }
  113. }
  114. break;
  115. case "ClickPicBox":
  116. if ((c as ClickPicBox).AllPower != null)
  117. {
  118. Type = "IFALL";
  119. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  120. return false;
  121. }
  122. if ((c as ClickPicBox).Power != null && (c as ClickPicBox).Power.ToString() != "")
  123. {
  124. LogManager.DoLog(c.FindForm().Text + "窗体" + "点击按钮【" + c.Text + "】");
  125. switch ((c as ClickPicBox).Power.ToString().ToUpper())
  126. {
  127. case "IFREAD":
  128. Type = "IFREAD";
  129. Type1 = "读取";
  130. break;
  131. case "IFWRITE":
  132. Type = "IFWRITE";
  133. Type1 = "修改";
  134. break;
  135. case "IFDELETE":
  136. Type = "IFDELETE";
  137. Type1 = "删除";
  138. break;
  139. case "IFSPECIAL":
  140. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  141. return false;
  142. else
  143. {
  144. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  145. return true;
  146. }
  147. default: break;
  148. }
  149. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  150. return false;
  151. else
  152. {
  153. MessageBox.Show("没有" + Type1 + "数据的权限", "提示", messButton, messIcon);
  154. return true;
  155. }
  156. }
  157. break;
  158. case "SearchTextBox":
  159. if ((c as SearchTextBox).AllPower != null)
  160. {
  161. Type = "IFALL";
  162. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  163. return false;
  164. }
  165. if ((c as SearchTextBox).Power != null)
  166. {
  167. LogManager.DoLog(c.FindForm().Text + "窗体" + "输入框【" + c.Text + "】");
  168. switch ((c as SearchTextBox).Power.ToString().ToUpper())
  169. {
  170. case "IFREAD":
  171. Type = "IFREAD";
  172. Type1 = "读取";
  173. break;
  174. case "IFWRITE":
  175. Type = "IFWRITE";
  176. Type1 = "修改";
  177. break;
  178. case "IFDELETE":
  179. Type = "IFDELETE";
  180. Type1 = "删除";
  181. break;
  182. case "IFSPECIAL":
  183. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  184. return false;
  185. else
  186. {
  187. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  188. return true;
  189. }
  190. default: break;
  191. }
  192. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  193. {
  194. return false;
  195. }
  196. else
  197. {
  198. MessageBox.Show("没有" + Type1 + "数据的权限", "提示", messButton, messIcon);
  199. return true;
  200. }
  201. }
  202. break;
  203. case "DeleteButton":
  204. if ((c as DeleteButton).AllPower != null)
  205. {
  206. Type = "IFALL";
  207. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  208. return false;
  209. }
  210. if ((c as DeleteButton).Power != null)
  211. {
  212. LogManager.DoLog(c.FindForm().Text + "窗体" + "点击按钮【" + c.Text + "】");
  213. switch ((c as DeleteButton).Power.ToString().ToUpper())
  214. {
  215. case "IFREAD":
  216. Type = "IFREAD";
  217. Type1 = "读取";
  218. break;
  219. case "IFWRITE":
  220. Type = "IFWRITE";
  221. Type1 = "修改";
  222. break;
  223. case "IFDELETE":
  224. Type = "IFDELETE";
  225. Type1 = "删除";
  226. break;
  227. case "IFSPECIAL":
  228. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  229. return false;
  230. else
  231. {
  232. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  233. return true;
  234. }
  235. default: break;
  236. }
  237. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  238. return false;
  239. else
  240. {
  241. MessageBox.Show("没有" + Type1 + "数据的权限", "提示", messButton, messIcon);
  242. return true;
  243. }
  244. }
  245. break;
  246. case "Button":
  247. if (c.Tag != null)
  248. {
  249. LogManager.DoLog(c.FindForm().Text + "窗体" + "点击按钮【" + c.Text + "】");
  250. switch (c.Tag.ToString().ToUpper())
  251. {
  252. case "IFREAD":
  253. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFREAD"])
  254. return false;
  255. else
  256. {
  257. MessageBox.Show("没有读取数据的权限", "提示", messButton, messIcon);
  258. return true;
  259. }
  260. case "IFWRITE":
  261. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFWRITE"])
  262. return false;
  263. else
  264. {
  265. MessageBox.Show("没有修改数据的权限", "提示", messButton, messIcon);
  266. return true;
  267. }
  268. case "IFDELETE":
  269. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFDELETE"])
  270. return false;
  271. else
  272. {
  273. MessageBox.Show("没有删除数据的权限", "提示", messButton, messIcon);
  274. return true;
  275. }
  276. case "IFSPECIAL":
  277. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  278. return false;
  279. else
  280. {
  281. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  282. return true;
  283. }
  284. default: break;
  285. }
  286. }
  287. break;
  288. case "EnterTextBox":
  289. if ((c as EnterTextBox).AllPower != null)
  290. {
  291. Type = "IFALL";
  292. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  293. return false;
  294. }
  295. if ((c as EnterTextBox).Power != null)
  296. {
  297. LogManager.DoLog(c.FindForm().Text + "窗体" + "输入框【" + c.Text + "】");
  298. switch ((c as EnterTextBox).Power.ToString().ToUpper())
  299. {
  300. case "IFREAD":
  301. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFREAD"])
  302. return false;
  303. else
  304. {
  305. MessageBox.Show("没有读取数据的权限", "提示", messButton, messIcon);
  306. return true;
  307. }
  308. case "IFWRITE":
  309. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFWRITE"])
  310. return false;
  311. else
  312. {
  313. MessageBox.Show("没有修改数据的权限", "提示", messButton, messIcon);
  314. return true;
  315. }
  316. case "IFDELETE":
  317. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFDELETE"])
  318. return false;
  319. else
  320. {
  321. MessageBox.Show("没有删除数据的权限", "提示", messButton, messIcon);
  322. return true;
  323. }
  324. case "IFSPECIAL":
  325. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  326. return false;
  327. else
  328. {
  329. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  330. return true;
  331. }
  332. default: break;
  333. }
  334. }
  335. break;
  336. default: break;
  337. }
  338. return false;
  339. }
  340. }
  341. }