我的需求是这个程序只能运行一次,在第二次运行时要检测是否已经运行,如果已经运行则将先前运行的实例弹出到前面。现在的问题是这个程序使用了NotifyIcon控件最小化到了系统托盘区,现在的代码却无法恢复了。如果是正常的窗口(即不是最小化到系统托盘的)可以实现这个需求。代码如下: [DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
private const int SW_RESTORE = 9;
private const int SW_SHOWNORMAL = 10; private static Process OnlyPrograme2()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes)
{
if (process.Id == current.Id)
{
/*string str1 = Assembly.GetExecutingAssembly().Location;
string str2 = current.MainModule.FileName;
string str3 = process.MainModule.FileName;
MessageBox.Show("Assembly:" + str1
+ "\ncurrent:" + str2
+ "\nprocess:" + str3);*/ //if (Assembly.GetExecutingAssembly().Location.Replace("/", "//")
// == current.MainModule.FileName)
//{
IntPtr hWnd = process.MainWindowHandle;
if (IsIconic(hWnd))
ShowWindowAsync(hWnd, SW_RESTORE);
ShowWindowAsync(hWnd, SW_RESTORE);
ShowWindow(hWnd, SW_SHOWNORMAL);
SetForegroundWindow(hWnd);
return process;
//}
}
}
return null;
} private void FormMain_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
private const int SW_RESTORE = 9;
private const int SW_SHOWNORMAL = 10; private static Process OnlyPrograme2()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes)
{
if (process.Id == current.Id)
{
/*string str1 = Assembly.GetExecutingAssembly().Location;
string str2 = current.MainModule.FileName;
string str3 = process.MainModule.FileName;
MessageBox.Show("Assembly:" + str1
+ "\ncurrent:" + str2
+ "\nprocess:" + str3);*/ //if (Assembly.GetExecutingAssembly().Location.Replace("/", "//")
// == current.MainModule.FileName)
//{
IntPtr hWnd = process.MainWindowHandle;
if (IsIconic(hWnd))
ShowWindowAsync(hWnd, SW_RESTORE);
ShowWindowAsync(hWnd, SW_RESTORE);
ShowWindow(hWnd, SW_SHOWNORMAL);
SetForegroundWindow(hWnd);
return process;
//}
}
}
return null;
} private void FormMain_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// 尝试创建一个命名事件
bool createNew;
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyStartEvent", out createNew); // 如果该命名事件已经存在(存在有前一个运行实例),则
if (!createNew)
{
// 先写一些数据到注册表中,以便传递给前一个运行实例
Registry.SetValue(@"HKEY_CURRENT_USER\Software\MyMy", "", "Your system " + DateTime.Now.ToLongTimeString()); // 发事件通知
ProgramStarted.Set(); // 等一小会以便前一个运行实例接到通知后恢复显示(也可以采用等待事件通知的方式)
Thread.Sleep(200); // 将焦点转移到前一个实例
foreach (Process p in Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName))
{
if (p.MainWindowHandle != IntPtr.Zero)
{
SetForegroundWindow(p.MainWindowHandle);
}
} // 就此退出第二个进程
return;
} Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
} public static EventWaitHandle ProgramStarted; [System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
}
这段代码并不使其恢复啊