123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- using System.Windows.Forms;
- using UAS_MES_NEW.CustomControl.ButtonUtil;
- using UAS_MES_NEW.CustomControl.ClickPicBox;
- using UAS_MES_NEW.CustomControl.CustomCheckBox;
- using UAS_MES_NEW.CustomControl.TextBoxWithIcon;
- using UAS_MES_NEW.Entity;
- namespace UAS_MES_NEW.PublicMethod
- {
- /// <summary>
- /// Author zhangz 2016/12/30
- /// 全局事件处理,通过句柄获取触发事件的控件
- /// 结合捕获的全局的键盘事件来验证用户进行的操作
- /// </summary>
- class GlobalEventsHandler : IMessageFilter
- {
- public static Keys keycode;
- //鼠标左键点击
- public const int WM_LBUTTONDOWN = 0x0201;
- //键盘敲击事件
- public const int WM_KEYDOWN = 0x100;
- private MessageBoxButtons messButton = MessageBoxButtons.OK;
- private MessageBoxIcon messIcon = MessageBoxIcon.Exclamation;
- string Type = "";
- string Type1 = "";
- public bool PreFilterMessage(ref Message m)
- {
- //通过句柄去获取具体触发事件的控件
- Control c = Control.FromHandle(m.HWnd);
- //可进行数据操作的控件需要进行权限的判断,结合WM_LBUTTONDOWN对按钮进行判断
- 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))
- {
- //获取控件的类型
- return CheckControlPower(c);
- }
- //集合WM_KEYDOWN对输入框进行判断
- else if (c != null && m.Msg == WM_KEYDOWN && (c is EnterTextBox || c is TextBox))
- {
- string ControlType = c.GetType().ToString();
- switch (ControlType.Substring(ControlType.LastIndexOf(".")))
- {
- case "EnterTextBox": break;
- default: break;
- }
- //在TextBox或者EnterBox中按下了回车键
- if (keycode == Keys.Return)
- {
- keycode = Keys.None;
- return CheckControlPower(c);
- }
- else
- return false;
- }
- else
- return false;
- }
- /// <summary>
- /// 返回True表示消息已处理,不再往后传递
- /// 返回False表示消息未处理,后续程序处理此事件
- /// </summary>
- /// <param name="c"></param>
- /// <returns></returns>
- private bool CheckControlPower(Control c)
- {
- //弹出的小窗体不做校验
- if (c.FindForm().Tag.ToString() == "ShowDialogWindow")
- return false;
- //获取控件的类型
- string ControlType = c.GetType().ToString();
- switch (ControlType.Substring(ControlType.LastIndexOf(".") + 1))
- {
- case "NormalButton":
- LogManager.DoLog(c.FindForm().Text + "窗体" + "点击按钮【" + c.Text + "】");
- //判断是否拥有全部权限
- if ((c as NormalButton).AllPower != null)
- {
- Type = "IFALL";
- if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
- return false;
- }
- if ((c as NormalButton).Power != null)
- {
- switch ((c as NormalButton).Power.ToString().ToUpper())
- {
- case "IFREAD":
- Type = "IFREAD";
- Type1 = "读取";
- break;
- case "IFWRITE":
- Type = "IFWRITE";
- Type1 = "修改";
- break;
- case "IFDELETE":
- Type = "IFDELETE";
- Type1 = "删除";
- break;
- case "IFSPECIAL":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
- return false;
- else
- {
- MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
- return true;
- }
- default:
- break;
- }
- if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
- return false;
- else
- {
- MessageBox.Show("没有" + Type1 + "数据的权限", "提示", messButton, messIcon);
- return true;
- }
- }
- break;
- case "ClickPicBox":
- if ((c as ClickPicBox).AllPower != null)
- {
- Type = "IFALL";
- if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
- return false;
- }
- if ((c as ClickPicBox).Power != null && (c as ClickPicBox).Power.ToString() != "")
- {
- LogManager.DoLog(c.FindForm().Text + "窗体" + "点击按钮【" + c.Text + "】");
- switch ((c as ClickPicBox).Power.ToString().ToUpper())
- {
- case "IFREAD":
- Type = "IFREAD";
- Type1 = "读取";
- break;
- case "IFWRITE":
- Type = "IFWRITE";
- Type1 = "修改";
- break;
- case "IFDELETE":
- Type = "IFDELETE";
- Type1 = "删除";
- break;
- case "IFSPECIAL":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
- return false;
- else
- {
- MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
- return true;
- }
- default: break;
- }
- if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
- return false;
- else
- {
- MessageBox.Show("没有" + Type1 + "数据的权限", "提示", messButton, messIcon);
- return true;
- }
- }
- break;
- case "SearchTextBox":
- if ((c as SearchTextBox).AllPower != null)
- {
- Type = "IFALL";
- if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
- return false;
- }
- if ((c as SearchTextBox).Power != null)
- {
- LogManager.DoLog(c.FindForm().Text + "窗体" + "输入框【" + c.Text + "】");
- switch ((c as SearchTextBox).Power.ToString().ToUpper())
- {
- case "IFREAD":
- Type = "IFREAD";
- Type1 = "读取";
- break;
- case "IFWRITE":
- Type = "IFWRITE";
- Type1 = "修改";
- break;
- case "IFDELETE":
- Type = "IFDELETE";
- Type1 = "删除";
- break;
- case "IFSPECIAL":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
- return false;
- else
- {
- MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
- return true;
- }
- default: break;
- }
- if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
- {
- return false;
- }
- else
- {
- MessageBox.Show("没有" + Type1 + "数据的权限", "提示", messButton, messIcon);
- return true;
- }
- }
- break;
- case "DeleteButton":
- if ((c as DeleteButton).AllPower != null)
- {
- Type = "IFALL";
- if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
- return false;
- }
- if ((c as DeleteButton).Power != null)
- {
- LogManager.DoLog(c.FindForm().Text + "窗体" + "点击按钮【" + c.Text + "】");
- switch ((c as DeleteButton).Power.ToString().ToUpper())
- {
- case "IFREAD":
- Type = "IFREAD";
- Type1 = "读取";
- break;
- case "IFWRITE":
- Type = "IFWRITE";
- Type1 = "修改";
- break;
- case "IFDELETE":
- Type = "IFDELETE";
- Type1 = "删除";
- break;
- case "IFSPECIAL":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
- return false;
- else
- {
- MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
- return true;
- }
- default: break;
- }
- if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
- return false;
- else
- {
- MessageBox.Show("没有" + Type1 + "数据的权限", "提示", messButton, messIcon);
- return true;
- }
- }
- break;
- case "Button":
- if (c.Tag != null)
- {
- LogManager.DoLog(c.FindForm().Text + "窗体" + "点击按钮【" + c.Text + "】");
- switch (c.Tag.ToString().ToUpper())
- {
- case "IFREAD":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFREAD"])
- return false;
- else
- {
- MessageBox.Show("没有读取数据的权限", "提示", messButton, messIcon);
- return true;
- }
- case "IFWRITE":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFWRITE"])
- return false;
- else
- {
- MessageBox.Show("没有修改数据的权限", "提示", messButton, messIcon);
- return true;
- }
- case "IFDELETE":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFDELETE"])
- return false;
- else
- {
- MessageBox.Show("没有删除数据的权限", "提示", messButton, messIcon);
- return true;
- }
- case "IFSPECIAL":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
- return false;
- else
- {
- MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
- return true;
- }
- default: break;
- }
- }
- break;
- case "EnterTextBox":
- if ((c as EnterTextBox).AllPower != null)
- {
- Type = "IFALL";
- if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
- return false;
- }
- if ((c as EnterTextBox).Power != null)
- {
- LogManager.DoLog(c.FindForm().Text + "窗体" + "输入框【" + c.Text + "】");
- switch ((c as EnterTextBox).Power.ToString().ToUpper())
- {
- case "IFREAD":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFREAD"])
- return false;
- else
- {
- MessageBox.Show("没有读取数据的权限", "提示", messButton, messIcon);
- return true;
- }
- case "IFWRITE":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFWRITE"])
- return false;
- else
- {
- MessageBox.Show("没有修改数据的权限", "提示", messButton, messIcon);
- return true;
- }
- case "IFDELETE":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFDELETE"])
- return false;
- else
- {
- MessageBox.Show("没有删除数据的权限", "提示", messButton, messIcon);
- return true;
- }
- case "IFSPECIAL":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
- return false;
- else
- {
- MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
- return true;
- }
- default: break;
- }
- }
- break;
- case "CustomCheckBox":
- if ((c as CustomCheckBox).AllPower != null)
- {
- Type = "IFALL";
- if (SystemInf.Caller[c.FindForm().Tag.ToString()][Type])
- return false;
- }
- if ((c as CustomCheckBox).Power != null)
- {
- LogManager.DoLog(c.FindForm().Text + "窗体" + "输入框【" + c.Text + "】");
- switch ((c as CustomCheckBox).Power.ToString().ToUpper())
- {
- case "IFREAD":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFREAD"])
- return false;
- else
- {
- MessageBox.Show("没有读取数据的权限", "提示", messButton, messIcon);
- return true;
- }
- case "IFWRITE":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFWRITE"])
- return false;
- else
- {
- MessageBox.Show("没有修改数据的权限", "提示", messButton, messIcon);
- return true;
- }
- case "IFDELETE":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFDELETE"])
- return false;
- else
- {
- MessageBox.Show("没有删除数据的权限", "提示", messButton, messIcon);
- return true;
- }
- case "IFSPECIAL":
- if (SystemInf.Caller[c.FindForm().Tag.ToString()]["IFSPECIAL"])
- return false;
- else
- {
- MessageBox.Show("此操作需要特殊权限", "提示", messButton, messIcon);
- return true;
- }
- default: break;
- }
- }
- break;
- default: break;
- }
- return false;
- }
- }
- }
|