GlobalEventsHandler.cs 18 KB

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