using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
namespace GTranslate
{
//static class Program
//{
// /// <summary>
// /// The main entry point for the application.
// /// </summary>
// //[STAThread]
// //static void Main()
// //{
// // Application.EnableVisualStyles();
// // Application.SetCompatibleTextRenderingDefault(false);
// // Application.Run(new Main());
// //}
// private const int WS_SHOWSTATE = 9;//用当前的大小和位置显示一个窗口,不改变活动窗口
// [STAThread]
// static void Main()
// {
// bool createdNew;
// //系统能够识别有名称的互斥,因此可以使用它禁止应用程序启动两次
// //第二个参数可以设置为产品的名称:Application.ProductName
// //每次启动应用程序,都会验证名称为SingletonWinAppMutex的互斥是否存在
// Mutex mutex = new Mutex(false, "GTranslate", out createdNew);
// //如果已运行,则在前端显示
// //createdNew == false,说明程序已运行
// if (!createdNew)
// {
// Process instance = GetExistProcess();
// if (instance != null)
// {
// //MessageBox.Show("程序已经运行,进程名字:"+instance.ProcessName, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
// SetForegroud(instance);
// Application.Exit();
// //Environment.Exit(0);
// return;
// }
// }
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new Main());
// }
// /*
// * 查看程序是否已经运行
// */
// private static Process GetExistProcess()
// {
// Process currentProcess = Process.GetCurrentProcess();
// foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
// {
// //MessageBox.Show(currentProcess.ProcessName.ToString());
// if ((process.Id != currentProcess.Id) && (Assembly.GetExecutingAssembly().Location == currentProcess.MainModule.FileName))
// {
// return process;
// }
// }
// return null;
// }
// private static void SetForegroud(Process instance)
// {
// IntPtr mainFormHandle = instance.MainWindowHandle;
// if (mainFormHandle != IntPtr.Zero)
// {
// //PostMessage(mainFormHandle);
// //instance.CloseMainWindow();
// ShowWindowAsync(mainFormHandle, WS_SHOWSTATE);
// ShowWindow(mainFormHandle, WS_SHOWSTATE);
// //Control.FromHandle(mainFormHandle).Show();
// SwitchToThisWindow(instance.MainWindowHandle, true);
// MessageBox.Show(mainFormHandle.ToString() + instance.MainWindowTitle);
// SetForegroundWindow(mainFormHandle);
// }
// }
// [DllImport("User32.dll")]
// private static extern bool SetForegroundWindow(IntPtr hWnd);
// [DllImport("User32.dll")]
// private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
// [DllImport("user32.dll ", SetLastError = true)]
// static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
// [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
// private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
//}
static class Program
{
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(int hWnd, int cmdShow);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32")]
static extern int SetForegroundWindow(int hwnd);
private const int SW_NORMAL = 1; //正常弹出窗体
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process currentProcess = Process.GetCurrentProcess();
//判断程序是否已经启动
Mutex mutex = new Mutex(false, "GTranslate");
bool Running = !mutex.WaitOne(0, false);
if (!Running)
{
Application.Run(new Main());
}
else
{
//MessageBox.Show("程序已经运行" + Process.GetProcessesByName(currentProcess.ProcessName)[0].MainWindowTitle, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
int WINDOW_HANDLER = FindWindow(null, "Google Translate V 1.0 BY: Z.A.CHEN"); //程序标题或类名二选1
if (WINDOW_HANDLER > 0)
{
//MessageBox.Show(WINDOW_HANDLER.ToString());
ShowWindowAsync(WINDOW_HANDLER, SW_NORMAL);
SetForegroundWindow(WINDOW_HANDLER);
}
Environment.Exit(0);
}
}
}
}
注释掉的程序为不成功的代码。
为了实现比较好的效果,做好还是采用发消息的方法来实现。