怎么只能运行一个程序
如何做到只能运行一个程序?并且在第二次打开程序的时候将第一次打开的程序窗口激活
我做的是个TXT编辑器
我想如果我直接打开一个文本文件(之前已经打开一个程序了)但是这个文本文件是在第一次打开的程序窗口中显示的
怎么弄啊?
[解决办法]
同时只有一个窗体实例,再次打开激活已打开窗体的范例
[解决办法]
- C# code
[DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("User32.dll", EntryPoint = "FindWindow")] private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); //添加api //获取当前运行的实例 public static Process GetRunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes) { if (process.Id != current.Id) if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) return process; } return null; } public static void HandleRunningInstance(Process instance) { if (instance != null && instance.MainWindowHandle.ToInt32() != 0) { string windowTile = instance.MainWindowTitle.Trim(); if (windowTile == "系统标题") ShowWindowAsync(instance.MainWindowHandle, 3); else ShowWindowAsync(instance.MainWindowHandle, 1); SetForegroundWindow(instance.MainWindowHandle); } else { IntPtr hwnd = FindWindow(null, "系统标题"); if (hwnd.ToInt32() == 0) { hwnd = FindWindow(null, "系统标题"); ShowWindowAsync(hwnd, 1); SetForegroundWindow(hwnd); } else { ShowWindowAsync(hwnd, 3); SetForegroundWindow(hwnd); } } }
[解决办法]
http://topic.csdn.net/u/20091118/18/9f3f7c0a-20fa-4006-9ccd-823734b10353.html