/// <summary> 双击某快捷键进入编辑 /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CurrentNode_dataGridView_DoubleClick(object sender, EventArgs e) { if (((MouseEventArgs)e).Button != MouseButtons.Left) { return; } DataGridViewSelectedRowCollection SelectedRowArrayTemp = CurrentNode_dataGridView.SelectedRows; if (SelectedRowArrayTemp.Count == 0) { return; } DataGridViewRow SelectedRowTemp = SelectedRowArrayTemp[0]; string CurrentID = SelectedRowTemp.Cells["ShortCutKeyID"].Value.ToString(); tShortCutKey CurrentEntity = tShortCutKeyDataSource.Where(T1 => T1.ShortCutKeyID == CurrentID).FirstOrDefault(); if (CurrentEntity == null) { return; } frmShortcutKey_Edit frm = new frmShortcutKey_Edit(CurrentEntity); if (frm.ShowDialog() == DialogResult.OK) { new ShortcutKeyManageService().UpdateShortcutKey(frm.CurrentEntity); InitKeyList(); } }
/// <summary> 用户选择一个节点后 /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void otTreeView1_AfterSelect(object sender, TreeViewEventArgs e) { if (otTreeView1.SelectedNode == null) { return; } if (otTreeView1.SelectedNode.Name == "BaseNode") { SelectedEntity = new tShortCutKey() { ShortCutKeyID = "", ShortCutKeyName = "根节点" }; } else { tShortCutKey tShortCutKeyTemp = EntityList.Where(T1 => T1.ShortCutKeyID == otTreeView1.SelectedNode.Name).FirstOrDefault(); if (tShortCutKeyTemp == null) { return; } SelectedEntity = tShortCutKeyTemp; } this.DialogResult = DialogResult.OK; this.Close(); }
/// <summary> 删除快捷键以及他的子节点 /// </summary> /// <param name="DeleteData"></param> /// <returns></returns> public bool DeleteShortCutKey(tShortCutKey DeleteData) { if (string.IsNullOrEmpty(DeleteData.ShortCutKeyID)) { return(false); } using (OfficeToolsDb db = new OfficeToolsDb()) { var tShortCutKeyIdList = db.tShortCutKey.Select(T1 => new { ID = T1.ShortCutKeyID, ParentID = T1.ParentID }).ToList(); //取出该表的所有ID和父ID List <dynamic> tIdList = tShortCutKeyIdList.Select(T1 => (dynamic)T1).ToList(); //将匿名对象转换成Dynamic方便后面传递给递归方法 List <string> DeliteIdList = GetChidrenId(tIdList, DeleteData.ShortCutKeyID); //递归无限向下取所有子节点ID DeliteIdList.Add(DeleteData.ShortCutKeyID); //自己本身也要被删除 List <tShortCutKey> DeleteEntityList = DeliteIdList.Select(T1 => new tShortCutKey() { ShortCutKeyID = T1 }).ToList(); //将Dynamic集合转换为tShortCutKey方便EF删除 #region 开始删除 foreach (tShortCutKey Deleteitem in DeleteEntityList) { DbEntityEntry Status = db.Entry(Deleteitem); Status.State = EntityState.Deleted; } db.SaveChanges(); #endregion 开始删除 } return(true); }
public bool UpdateShortcutKey(tShortCutKey UpdateEntity) { int ChangeLineCount = 0; using (OfficeToolsDb db = new OfficeToolsDb()) { db.Entry <tShortCutKey>(UpdateEntity).State = EntityState.Modified; ChangeLineCount = db.SaveChanges(); } return(0 < ChangeLineCount); }
/// <summary> 当编辑节点名称后 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ShortcutKey_otTreeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { TreeNode CurrentNode = ShortcutKey_otTreeView.SelectedNode; if (CurrentNode.Text == e.Label || string.IsNullOrEmpty(e.Label)) { return; //如果内容没有发生改变就返回 } tShortCutKey tShortCutKeyTemp = tShortCutKeyDataSource.Where(T1 => T1.ShortCutKeyID == CurrentNode.Name).FirstOrDefault(); tShortCutKeyTemp.ShortCutKeyName = e.Label ?? ""; ShortcutKey_otTreeView_AfterSelect(null, null); //发生了改变就把名称同步到右边 new ShortcutKeyManageService().UpdateShortcutKey(tShortCutKeyTemp); //并保存这个名称 }
/// <summary> 删除快捷键 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Remove_button_Click(object sender, EventArgs e) { TreeNode CurrentNode = ShortcutKey_otTreeView.SelectedNode; if (CurrentNode == null) { return; } tShortCutKey RemoveData = new tShortCutKey() { ShortCutKeyID = CurrentNode.Name }; if (new ShortcutKeyManageService().DeleteShortCutKey(RemoveData)) { InitKeyList(); } }
/// <summary> /// 添加一个快捷键 /// </summary> public void AddtShortcutKey(tShortCutKey Param) { Task.Delay(100).ContinueWith(task => { Invoke(new Action(() => { Param.ShortCutKeyImg = UniversalMethod.ByteArayToHex(OfficeTools.Resource.Image.Image.ShortcutKeyDefualtImage.ToImage().ToByteArray()); frmShortcutKey_Edit frm = new frmShortcutKey_Edit(Param); string NewID = ""; if (frm.ShowDialog() == DialogResult.OK) { NewID = new ShortcutKeyManageService().AddShortCutKey(frm.CurrentEntity); if (!string.IsNullOrEmpty(NewID)) { InitKeyList(); } } })); }); }
/// <summary> 在某个节点下添加一个快捷键,成功返回它的ID,失败返回空 /// </summary> /// <param name="AddData">节点</param> /// <returns>新的快捷键的ID</returns> public string AddShortCutKey(tShortCutKey AddData) { int ChangeCount = 0; using (OfficeToolsDb db = new OfficeToolsDb()) { if (AddData.ShortCutKeyID == null) { AddData.ShortCutKeyID = DBTools.GetID(); } db.tShortCutKey.Add(AddData); ChangeCount = db.SaveChanges(); } if (ChangeCount <= 0) { return(null); } return(AddData.ShortCutKeyID); }
/// <summary>删除按钮被单击 /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeleteCurrentNode_button_Click(object sender, EventArgs e) { String StrID = CurrentNode_dataGridView.GetSelectedRowValue("ShortCutKeyID"); if (string.IsNullOrEmpty(StrID)) { return; } int IntSelectedRow = CurrentNode_dataGridView.SelectedRows[0].Index; tShortCutKey RemoveData = new tShortCutKey() { ShortCutKeyID = StrID }; if (new ShortcutKeyManageService().DeleteShortCutKey(RemoveData)) { InitKeyList(); } CurrentNode_dataGridView.SetSelecetedRowByRowIndex(IntSelectedRow); }
public List <tShortCutKey> GettShortCutKeyList(tShortCutKey WhereEntity = null) { List <tShortCutKey> ResultList = new List <tShortCutKey>(); using (OfficeToolsDb db = new OfficeToolsDb()) { IQueryable <tShortCutKey> tShortCutKeyIQ = db.tShortCutKey; if (WhereEntity == null) { ResultList = tShortCutKeyIQ.ToList(); return(ResultList); } if (WhereEntity.ShortCutKeyID != null) { tShortCutKeyIQ = tShortCutKeyIQ.Where(T1 => T1.ShortCutKeyID == WhereEntity.ShortCutKeyID); } ResultList = tShortCutKeyIQ.ToList(); } return(ResultList); }
/// <summary> 以当前选中的快捷键为基础添加一个快捷键 /// /// </summary> /// <param name="IsChidren">[true:添加的快捷键是当前选中的快捷键的子节点;false:添加的选中快捷键的同级快捷键]</param> private string AddtShortcutKey(bool IsChidren = true) { tShortCutKey AddData = new tShortCutKey() { ShortCutKeyName = "新增快捷键", ParentID = "", ShortCutKey = "", ShortCutKeyPath = "", ShortCutKeyType = 0, ShortCutKeyImg = new byte[0] }; AddData.ShortCutKeyImg = OfficeTools.Resource.Image.Image.ShortcutKeyDefualtImage.ToImage().ToByteArray(); TreeNode CurrentNode = ShortcutKey_otTreeView.SelectedNode; if (CurrentNode != null) { tShortCutKey CurrentEntity = tShortCutKeyDataSource.Where(T1 => T1.ShortCutKeyID == CurrentNode.Name).FirstOrDefault(); if (CurrentEntity != null) { AddData.ParentID = CurrentEntity.ParentID; if (IsChidren) { AddData.ParentID = CurrentEntity.ShortCutKeyID; } } } int KeuNum = GetDefaultKey(AddData.ParentID); if (KeuNum != -1) { AddData.ShortCutKey = KeuNum.ToString(); } string NewID = new ShortcutKeyManageService().AddShortCutKey(AddData); if (!string.IsNullOrEmpty(NewID)) { InitKeyList(); } return(NewID); }
public frmShortcutKey_Edit(tShortCutKey entity) : this() { CurrentEntity = entity; }
/// <summary> 将文件拖拽到窗口后 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_DragDrop(object sender, DragEventArgs e) { System.Array ArrayTemp = ((System.Array)e.Data.GetData(DataFormats.FileDrop)); if (ArrayTemp == null) { return; //不支持的格式直接返回 } List <string> FilePathList = new List <string>(); for (int i = 0; i < ArrayTemp.Length; i++) { FilePathList.Add(ArrayTemp.GetValue(i).ToString()); } #region 获取父ID,使新添加的记录与当前选中节点处于同一级别 string StrParentId = ""; TreeNode CurrentNode = ShortcutKey_otTreeView.SelectedNode; if (CurrentNode != null) { tShortCutKey CurrentEntity = tShortCutKeyDataSource.Where(T1 => T1.ShortCutKeyID == CurrentNode.Name).FirstOrDefault(); if (CurrentEntity != null) { StrParentId = CurrentEntity.ShortCutKeyID; } } #endregion 获取父ID,使新添加的记录与当前选中节点处于同一级别 foreach (string PathItem in FilePathList) { string StrFileName = Path.GetFileNameWithoutExtension(PathItem);//获取快捷键名称 int KeyNum = GetDefaultKey(StrParentId); string StrKeyNum = ""; if (KeyNum != -1) { StrKeyNum = KeyNum.ToString(); //获取默认键编号 } int FileType = 0; //设置打开类型 byte[] ImageByteArray = UniversalMethod.GetImageByFileName(PathItem).ToByteArray(); //获取文件图标 #region 获取快捷方式真实路径 string StrPath = PathItem; string StrExtension = Path.GetExtension(PathItem); if (!string.IsNullOrEmpty(StrExtension))//文件 { if (StrExtension == ".lnk") { WshShell ShellTemp = new WshShell(); IWshShortcut IWshShortcutTemp = (IWshShortcut)ShellTemp.CreateShortcut(PathItem); StrPath = IWshShortcutTemp.TargetPath; StrExtension = Path.GetExtension(StrPath); if (string.IsNullOrEmpty(StrExtension)) { FileType = 1; } } } else//文件夹 { FileType = 1; } Regex RegexTemp = new Regex(@"https{0,1}\:\/\/"); if (RegexTemp.Match(StrPath).Success)//是否是网址 { FileType = 2; } #endregion 获取快捷方式真实路径 #region 实例化AddData tShortCutKey AddData = new tShortCutKey() { ShortCutKeyName = StrFileName, ParentID = StrParentId, ShortCutKey = StrKeyNum, ShortCutKeyPath = StrPath, ShortCutKeyType = (byte)FileType, ShortCutKeyImg = ImageByteArray }; #endregion 实例化AddData string NewID = new ShortcutKeyManageService().AddShortCutKey(AddData); tShortCutKeyDataSource.Add(AddData); } InitKeyList(); }
/// <summary> 运行当前节点的某个快捷键 /// 运行当前节点的某个快捷键 /// </summary> /// <param name="KeyNum">当前节点的键代码</param> private bool RunShortcutKeyContens(string KeyNum, int LongPressParam) { Runing = true; try { var ForegHandle = WindowsAPI.GetForegroundWindow(); WindowsAPI.GetWindowThreadProcessId(ForegHandle, out int ProgramID); var ProcessObj = Process.GetProcessById(ProgramID); var ProgramInfo = GetForeProgramInfo(); var ProgName = ProgramInfo?.Name ?? ""; var ForeImage = ProgramInfo?.Img; var ForeImgHex = ProgramInfo?.ImgHex ?? ""; vShortCutKey CurrentEntity = GetVShortCutKey(KeyNum, LongPressParam, ProgName); if (CurrentEntity == null) { #region 添加新的快捷键 tShortCutKey AddData = new tShortCutKey() { ShortCutKeyName = "新增快捷键", TargetProgramImg = ForeImgHex, TargetProgramName = ProgName, ParentID = CurrentShortcutKeyID, ShortCutKey = KeyNum, LongPress = LongPressParam, ShortCutKeyPath = "", ShortCutKeyType = 0, ShortCutKeyImg = "" }; ((frmShortcutKeyManage)ParentForm).AddtShortcutKey(AddData); return(false); #endregion 添加新的快捷键 } if (CurrentEntity.OwningChidrenNode) { ShowKey(CurrentEntity.ShortCutKeyID); return(true); } if (string.IsNullOrEmpty(CurrentEntity.ShortCutKeyPath)) { return(false); } try { ControlButton.IsRunProgram = true; switch (CurrentEntity.ShortCutKeyType) { case 0: //程序 case 1: // List <string> FilePathList = CurrentEntity.ShortCutKeyPath.Split(' ').ToList(); Regex PathRegex = new Regex(@".*?(\.exe)"); //获取程序路径 Regex ParamRegex = new Regex(@"(?<=.*?(\.exe) ).*"); //获取参数 string PathString = CurrentEntity.ShortCutKeyPath; //程序路径文本 string ParamString = ""; //参数文本 Match PathResultMatch = PathRegex.Match(PathString); if (PathResultMatch.Success) //尾缀为.exe才获取参数,没有的话就视为不带参数的文件夹直接打开 { ParamString = ParamRegex.Match(PathString).Value; PathString = PathResultMatch.Value; } UniversalMethod.RunProgram(PathString, ParamString); //启动程序 break; case 2: //系统组合键 UniversalMethod.KeyBoardDown(GetSysShortcutKey(CurrentEntity.ShortCutKeyPath)); break; } } catch { return(false); } } catch (Exception EX) { throw EX; } finally { Runing = false; } return(true); }