/// <summary> /// 循环卸载注册过的快捷键 /// </summary> private void Uninstall() { if (Resources.dicThisInt != null) { foreach (var item in Resources.dicThisInt) { SystemHotKey.UnregisterHotKey(Handle, item.Key); } } }
/// <summary> /// 注册快捷键 /// </summary> /// <param name="mainIntPtr"></param> public static void RegistrationKey(IntPtr mainIntPtr) { if (Resources.dicThisInt != null) { foreach (var item in Resources.dicThisInt) { String[] stringKey = item.Value.HotKey.Split('+'); SystemHotKey.KeyModifiers[] key = StringToKey(stringKey); SystemHotKey.RegisterHotKey(mainIntPtr, item.Key, (uint)key[0] | (uint)key[1], signKey(stringKey[2])); } } }
//重写WndProc()方法,通过监视系统消息,来调用过程 protected override void WndProc(ref Message m) //监视Windows消息 { const int WM_HOTKEY = 0x0312; //如果m.Msg的值为0x0312那么表示用户按下了热键 switch (m.Msg) { case WM_HOTKEY: SystemHotKey.ProcessHotkey(m); //按下热键时调用ProcessHotkey()函数 break; } base.WndProc(ref m); //将系统消息传递自父类的WndProc }
/// <summary> /// 确定关闭窗口 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button1_Click(object sender, EventArgs e) { if (textBox1.Text != null) { bool sign = SystemHotKey.RegisterHotKey(MianIntPtr, Resources.MaxNum, (uint)Resources.keysOne | (uint)Resources.keysTwo, Resources.keySan); if (sign) { Resources.dicThis[LnkName].HotKey = textBox1.Text; //对应程序名的 快捷键 修改 Resources.dicThis[LnkName].id = Resources.MaxNum; //对应程序名的 注册Id 修改 Resources.MaxNum++; //注册Id++ } Resources.Transformation(); } this.Close(); }
/// <summary> /// 获取路径文件,绑定列表数据,同步程序数据 /// </summary> /// <param name="flagReset">是否同步数据</param> private void BindingDgv(bool flagReset) { List <LnkModel> lnkList = new LnkPath().ListLnkModel(); if (lnkList == null) //有其它的文件 { DialogResult dr = MessageBox.Show("文件夹有非 lnk 文件!请修改路径或者删除非 lnk 文件。", "选择提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); if (dr == DialogResult.Cancel) { Application.Exit(); } if (dr == DialogResult.OK) { XmlFile.ChoiceLnkFile(); BindingDgv(false); } } foreach (LnkModel lnkModel in lnkList) { if (Resources.HandleDicThisInt(lnkModel.LnkName)) { if (Resources.HandleDicThis(lnkModel.LnkName)) { Resources.dicThis.Add(lnkModel.LnkName, lnkModel); } } } List <LnkModel> lnkListData = Resources.DicThisToLnkList(); if (flagReset) { List <LnkModel> lnkListString = new List <LnkModel>(); foreach (LnkModel item in lnkListData) //有的数据 { bool flag = true; foreach (var item2 in lnkList) //获取到的数据 { if (item.LnkName == item2.LnkName) { flag = false; } } if (flag) { lnkListString.Add(item); } } foreach (LnkModel item in lnkListString) { if (item.id == 0) //没有注册过 { Resources.dicThis.Remove(item.LnkName); } else { Resources.dicThisInt.Remove(item.id); SystemHotKey.UnregisterHotKey(Handle, item.id); } } dataGridView1.DataSource = lnkList; } if (lnkList.Count == 0) { try { Resources.dicThis.Clear(); Resources.dicThisInt.Clear(); } catch (Exception) { //throw; } lnkListData.Clear(); } dataGridView1.DataSource = lnkListData; //注册快捷键 Resources.RegistrationKey(Handle); }