[DllImport("coredll.Dll")]
private static extern int ReleaseMutex(IntPtr hMutex);
[DllImport("coredll.dll", EntryPoint = "CreateMutex", SetLastError = true)]
public static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool InitialOwner, string MutexName);
const int ERROR_ALREADY_EXISTS = 0183;
[DllImport("coredll.dll", EntryPoint = "FindWindowW", SetLastError = true)]
public static extern IntPtr FindWindowCE(string lpClassName, string lpWindowName);
[DllImport("coredll.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
#region Main
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[MTAThread]
static void Main()
{
string strAppName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
IntPtr hMutex = CreateMutex(IntPtr.Zero, true, strAppName);
if (Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS)
{
ReleaseMutex(hMutex);
MessageBox.Show("The application is running!");
//通过Class找到对应的窗口句柄 #NETCF_AGL_BASE_ 为窗口的ClassName
IntPtr mainWin = FindWindowCE("#NETCF_AGL_BASE_", null);
//SetForegroundWindow函数将创建指定的窗口,并激活到前台窗口的线程
bool result = SetForegroundWindow(mainWin);
return;
}
else
{
if (AppEntry.Login())
Application.Run(AppEntry.frmTmsOrderSearch);
else
Application.Exit();
}
}
WinCE Form的Class可以通过VS自带工具查看
Visual Studio Remote Tools->远程监视