/// <summary> /// 将窗体置于最顶层而不获取焦点 /// </summary> /// <param name="b">true最顶层,false非顶层</param> /// <param name="fAll">要操作的窗口的集合</param> public static void TopFormNoFocus(Boolean isTop, Form[] formArr) { // 判断传入的窗体集合为null或大小为0 if (formArr == null || 0.Equals(formArr.Length)) { return; } // 遍历 foreach (Form form in formArr) { // 判断窗体不为null或窗体没有被释放 if (form != null && !form.IsDisposed) { // 判断是否要设置为顶层 if (isTop) { // 设为顶层 WindowsApiUtils.setFormTopNoFocus(true, form); } else { // 判断当前前台窗口是否为要设置的窗口 if (form.TopLevel && !WindowsApiUtils.GetForegroundWindow().Equals(form.Handle)) { // 将窗口设置为非顶层 WindowsApiUtils.setFormTopNoFocus(false, form); } } } } }
/// <summary> /// 用记事本打开文本 /// </summary> /// <param name="text"></param> public static void TurnOnNotepad(string text) { Process process = Process.Start("notepad.exe");//打开记事本 // 循环判断打开的记事本的窗口句柄不为0 while (process.MainWindowHandle.Equals(IntPtr.Zero)) { process.Refresh(); } // 得到文本框主窗体中的类名为Edit的窗口句柄 IntPtr vHandle = WindowsApiUtils.FindWindowEx(process.MainWindowHandle, IntPtr.Zero, "Edit", null); // 向指定窗口句柄发送设置文本的消息 WindowsApiUtils.SendMessage(vHandle, 0x000C, 0, text); }
/// <summary> /// 右键菜单的关闭绑定事件,实现鼠标不在其范围内关闭,在其范围内不关闭 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void ToolStripMoveOutClosing(ToolStripDropDown tool) { if (tool == null) { return; } tool.Closing += new ToolStripDropDownClosingEventHandler((object sender, ToolStripDropDownClosingEventArgs e) => { ToolStripDropDown menu = (ToolStripDropDown)sender; Point mousePoint; //初始化一个接受鼠标位置的Point WindowsApiUtils.GetCursorPos(out mousePoint); //赋值 if (menu.ClientRectangle.Contains(mousePoint.X - menu.Left, mousePoint.Y - menu.Top)) { e.Cancel = true;//阻止关闭 } else { e.Cancel = false;//关闭 } }); }
/// <summary> /// 将窗体置于最顶层而不获取焦点 /// </summary> /// <param name="b">true最顶层,false非顶层</param> /// <param name="f">要操作的窗口</param> public static void TopFormNoFocus(Boolean isTop, Form form) { if (form == null || form.IsDisposed) { return; } if (form != null) { if (isTop) { WindowsApiUtils.setFormTopNoFocus(true, form); } else { if (!WindowsApiUtils.GetForegroundWindow().Equals(form.Handle)) { WindowsApiUtils.setFormTopNoFocus(false, form); } } } }
/// <summary> /// 监听文件变化并弹出提示框提示重新加载或另存为 /// </summary> /// <param name="filepath"></param> /// <param name="t"></param> public static void monitorFileTextShowMess(string filepath, TextBox t) { Dictionary <Type, object> data = new Dictionary <Type, object>(); data.Add(typeof(TextBox), t); // 监听文件变化 try { FileSystemWatcher wat = null; string[] pathArr = GetPathArr(filepath); Encoding encoding = Encoding.Default; if (!"txt".Equals(pathArr[2].ToLower())) { encoding = GetType(filepath); } // 判断文本框的Tag中是否纯在一个监听,存在就销毁他 if (TextBoxUtils.GetTextTagToMap(t).ContainsKey(TextBoxTagKey.TEXTBOX_TAG_KEY_FILEMONITOR)) { object obj = TextBoxUtils.GetTextTagToMap(t)[TextBoxTagKey.TEXTBOX_TAG_KEY_FILEMONITOR]; if (obj.GetType().Equals(typeof(FileSystemWatcher))) { wat = (FileSystemWatcher)TextBoxUtils.GetTextTagToMap(t)[TextBoxTagKey.TEXTBOX_TAG_KEY_FILEMONITOR]; wat.EnableRaisingEvents = false; wat.Dispose(); } } // 获取一个新的文件监听 wat = fileMonitor(pathArr[0], pathArr[1] + "." + pathArr[2], NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.Size , null, null , delegate(object sender, FileSystemEventArgs e){ FileSystemWatcher watcher = (FileSystemWatcher)sender; // 闪烁窗体 Form f = t.FindForm(); if (f.InvokeRequired) { f.Invoke(new EventHandler(delegate { System.Media.SystemSounds.Asterisk.Play(); WindowsApiUtils.FlashWindow(f.Handle, 400, 3); })); } // 弹出对话框 ControlsUtils.ShowAskMessBox("文件内容已经更改,是否要重新加载文件", "提示" , delegate { if (t.InvokeRequired) { t.Invoke(new EventHandler(delegate { // 获取内容 string text = FileRead.Read(filepath, encoding); t.Text = text; })); } }, null); watcher.EnableRaisingEvents = false; } , delegate { // 弹出对话框 MessageBox.Show("文件在磁盘上已经被删除或重命名!"); }); // 加入到文本框的tag数据中 TextBoxUtils.TextBoxAddTag(t, TextBoxTagKey.TEXTBOX_TAG_KEY_FILEMONITOR, wat); } catch { MessageBox.Show("监听文件状态时发生异常"); } }
/// <summary> /// 通过窗体标题名获取进程中的窗体句柄 /// </summary> /// <param name="name">窗体标题名</param> /// <returns></returns> public static IntPtr GetProcessFormByName(string headName) { IntPtr intPtr = WindowsApiUtils.FindWindow(null, headName); return(intPtr); }