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 { /// /// Author zhangz 2016/12/30 /// 全局事件处理,通过句柄获取触发事件的控件 /// 结合捕获的全局的键盘事件来验证用户进行的操作 /// 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; } /// /// 返回True表示消息已处理,不再往后传递 /// 返回False表示消息未处理,后续程序处理此事件 /// /// /// 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; } } }