GlobalEventsHandler.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. using System.Windows.Forms;
  2. using UAS_MES_NEW.CustomControl.ButtonUtil;
  3. using UAS_MES_NEW.CustomControl.ClickPicBox;
  4. using UAS_MES_NEW.CustomControl.CustomCheckBox;
  5. using UAS_MES_NEW.CustomControl.TextBoxWithIcon;
  6. using UAS_MES_NEW.Entity;
  7. namespace UAS_MES_NEW.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 "IFREAD":
  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.dh.getFieldDataByCondition("master", "MA_FUNCTION", "ma_user='" + SystemInf.CurrentDB + "'").ToString() == "万年MES系统(正式)")
  98. {
  99. if (c.Text == "封箱" || c.Text == "封栈板" || c.Text == "封大箱")
  100. {
  101. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  102. return false;
  103. else
  104. {
  105. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  106. return true;
  107. }
  108. }
  109. else {
  110. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  111. return false;
  112. else
  113. {
  114. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  115. return true;
  116. }
  117. }
  118. }
  119. else
  120. {
  121. if (!(c.Text == "封箱" || c.Text == "封栈板" || c.Text == "封大箱"))
  122. {
  123. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  124. return false;
  125. else
  126. {
  127. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  128. return true;
  129. }
  130. }
  131. else {
  132. return false;
  133. }
  134. }
  135. default:
  136. break;
  137. }
  138. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  139. return false;
  140. else
  141. {
  142. MessageBox.Show("没有" + Type1 + "数据的权限", "提示", messButton, messIcon);
  143. return true;
  144. }
  145. }
  146. break;
  147. case "ClickPicBox":
  148. if ((c as ClickPicBox).AllPower != null)
  149. {
  150. Type = "IFALL";
  151. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  152. return false;
  153. }
  154. if ((c as ClickPicBox).Power != null && (c as ClickPicBox).Power.ToString() != "")
  155. {
  156. LogManager.DoLog(c.FindForm().Text + "窗体" + "点击按钮【" + c.Text + "】");
  157. switch ((c as ClickPicBox).Power.ToString().ToUpper())
  158. {
  159. case "IFREAD":
  160. Type = "IFREAD";
  161. Type1 = "读取";
  162. break;
  163. case "IFWRITE":
  164. Type = "IFWRITE";
  165. Type1 = "修改";
  166. break;
  167. case "IFDELETE":
  168. Type = "IFDELETE";
  169. Type1 = "删除";
  170. break;
  171. case "IFSPECIAL":
  172. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  173. return false;
  174. else
  175. {
  176. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  177. return true;
  178. }
  179. default: break;
  180. }
  181. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  182. return false;
  183. else
  184. {
  185. MessageBox.Show("没有" + Type1 + "数据的权限", "提示", messButton, messIcon);
  186. return true;
  187. }
  188. }
  189. break;
  190. case "SearchTextBox":
  191. if ((c as SearchTextBox).AllPower != null)
  192. {
  193. Type = "IFALL";
  194. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  195. return false;
  196. }
  197. if ((c as SearchTextBox).Power != null)
  198. {
  199. LogManager.DoLog(c.FindForm().Text + "窗体" + "输入框【" + c.Text + "】");
  200. switch ((c as SearchTextBox).Power.ToString().ToUpper())
  201. {
  202. case "IFREAD":
  203. Type = "IFREAD";
  204. Type1 = "读取";
  205. break;
  206. case "IFWRITE":
  207. Type = "IFWRITE";
  208. Type1 = "修改";
  209. break;
  210. case "IFDELETE":
  211. Type = "IFDELETE";
  212. Type1 = "删除";
  213. break;
  214. case "IFSPECIAL":
  215. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  216. return false;
  217. else
  218. {
  219. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  220. return true;
  221. }
  222. default: break;
  223. }
  224. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  225. {
  226. return false;
  227. }
  228. else
  229. {
  230. MessageBox.Show("没有" + Type1 + "数据的权限", "提示", messButton, messIcon);
  231. return true;
  232. }
  233. }
  234. break;
  235. case "DeleteButton":
  236. if ((c as DeleteButton).AllPower != null)
  237. {
  238. Type = "IFALL";
  239. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  240. return false;
  241. }
  242. if ((c as DeleteButton).Power != null)
  243. {
  244. LogManager.DoLog(c.FindForm().Text + "窗体" + "点击按钮【" + c.Text + "】");
  245. switch ((c as DeleteButton).Power.ToString().ToUpper())
  246. {
  247. case "IFREAD":
  248. Type = "IFREAD";
  249. Type1 = "读取";
  250. break;
  251. case "IFWRITE":
  252. Type = "IFWRITE";
  253. Type1 = "修改";
  254. break;
  255. case "IFDELETE":
  256. Type = "IFDELETE";
  257. Type1 = "删除";
  258. break;
  259. case "IFSPECIAL":
  260. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  261. return false;
  262. else
  263. {
  264. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  265. return true;
  266. }
  267. default: break;
  268. }
  269. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  270. return false;
  271. else
  272. {
  273. MessageBox.Show("没有" + Type1 + "数据的权限", "提示", messButton, messIcon);
  274. return true;
  275. }
  276. }
  277. break;
  278. case "Button":
  279. if (c.Tag != null)
  280. {
  281. LogManager.DoLog(c.FindForm().Text + "窗体" + "点击按钮【" + c.Text + "】");
  282. switch (c.Tag.ToString().ToUpper())
  283. {
  284. case "IFREAD":
  285. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFREAD"])
  286. return false;
  287. else
  288. {
  289. MessageBox.Show("没有读取数据的权限", "提示", messButton, messIcon);
  290. return true;
  291. }
  292. case "IFWRITE":
  293. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFWRITE"])
  294. return false;
  295. else
  296. {
  297. MessageBox.Show("没有修改数据的权限", "提示", messButton, messIcon);
  298. return true;
  299. }
  300. case "IFDELETE":
  301. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFDELETE"])
  302. return false;
  303. else
  304. {
  305. MessageBox.Show("没有删除数据的权限", "提示", messButton, messIcon);
  306. return true;
  307. }
  308. case "IFSPECIAL":
  309. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  310. return false;
  311. else
  312. {
  313. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  314. return true;
  315. }
  316. default: break;
  317. }
  318. }
  319. break;
  320. case "EnterTextBox":
  321. if ((c as EnterTextBox).AllPower != null)
  322. {
  323. Type = "IFALL";
  324. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  325. return false;
  326. }
  327. if ((c as EnterTextBox).Power != null)
  328. {
  329. LogManager.DoLog(c.FindForm().Text + "窗体" + "输入框【" + c.Text + "】");
  330. switch ((c as EnterTextBox).Power.ToString().ToUpper())
  331. {
  332. case "IFREAD":
  333. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFREAD"])
  334. return false;
  335. else
  336. {
  337. MessageBox.Show("没有读取数据的权限", "提示", messButton, messIcon);
  338. return true;
  339. }
  340. case "IFWRITE":
  341. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFWRITE"])
  342. return false;
  343. else
  344. {
  345. MessageBox.Show("没有修改数据的权限", "提示", messButton, messIcon);
  346. return true;
  347. }
  348. case "IFDELETE":
  349. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFDELETE"])
  350. return false;
  351. else
  352. {
  353. MessageBox.Show("没有删除数据的权限", "提示", messButton, messIcon);
  354. return true;
  355. }
  356. case "IFSPECIAL":
  357. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  358. return false;
  359. else
  360. {
  361. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  362. return true;
  363. }
  364. default: break;
  365. }
  366. }
  367. break;
  368. case "CustomCheckBox":
  369. if ((c as CustomCheckBox).AllPower != null)
  370. {
  371. Type = "IFALL";
  372. if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
  373. return false;
  374. }
  375. if ((c as CustomCheckBox).Power != null)
  376. {
  377. LogManager.DoLog(c.FindForm().Text + "窗体" + "输入框【" + c.Text + "】");
  378. switch ((c as CustomCheckBox).Power.ToString().ToUpper())
  379. {
  380. case "IFREAD":
  381. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFREAD"])
  382. return false;
  383. else
  384. {
  385. MessageBox.Show("没有读取数据的权限", "提示", messButton, messIcon);
  386. return true;
  387. }
  388. case "IFWRITE":
  389. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFWRITE"])
  390. return false;
  391. else
  392. {
  393. MessageBox.Show("没有修改数据的权限", "提示", messButton, messIcon);
  394. return true;
  395. }
  396. case "IFDELETE":
  397. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFDELETE"])
  398. return false;
  399. else
  400. {
  401. MessageBox.Show("没有删除数据的权限", "提示", messButton, messIcon);
  402. return true;
  403. }
  404. case "IFSPECIAL":
  405. if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
  406. return false;
  407. else
  408. {
  409. MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
  410. return true;
  411. }
  412. default: break;
  413. }
  414. }
  415. break;
  416. default: break;
  417. }
  418. return false;
  419. }
  420. }
  421. }