using System; using System.Collections.Generic; using System.Windows.Forms; using System.Diagnostics; using System.Runtime.InteropServices;namespace LogAnalyze { static class Program { [STAThread] static void Main() { bool createdNew; System.Threading.Mutex m = new System.Threading.Mutex(true, "YourAppName", out createdNew); if (!createdNew) { // see if we can find the other app and Bring it to front IntPtr hWnd = FindWindow(null,"LogAnalyze");// get HWND if (hWnd != IntPtr.Zero) { Program.WINDOWPLACEMENT placement = new Program.WINDOWPLACEMENT(); placement.length = Marshal.SizeOf(placement); GetWindowPlacement(hWnd, ref placement); if (placement.showCmd != SW_RESTORE) { placement.showCmd = SW_RESTORE; SetWindowPlacement(hWnd, ref placement); SetForegroundWindow(hWnd); } } return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new LogAnalyze());//你要运行的主窗体 // keep the mutex reference alive until the normal termination of the program GC.KeepAlive(m); } private const int SW_NORMAL = 1; // see WinUser.h for definitions private const int SW_RESTORE = 9; [DllImport("User32", EntryPoint = "FindWindow")] static extern IntPtr FindWindow(string className, string windowName); [DllImport("User32", EntryPoint = "SendMessage")] private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport("User32", EntryPoint = "SetForegroundWindow")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("User32", EntryPoint = "SetWindowPlacement")] private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl); [DllImport("User32", EntryPoint = "GetWindowPlacement")] private static extern bool GetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl); private struct POINTAPI { public int x; public int y; } private struct RECT { public int left; public int top; public int right; public int bottom; } private struct WINDOWPLACEMENT { public int length; public int flags; public int showCmd; public POINTAPI ptMinPosition; public POINTAPI ptMaxPosition; public RECT rcNormalPosition; }
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;namespace LogAnalyze
{
static class Program
{
[STAThread]
static void Main()
{
bool createdNew; System.Threading.Mutex m = new System.Threading.Mutex(true, "YourAppName", out createdNew); if (!createdNew)
{
// see if we can find the other app and Bring it to front
IntPtr hWnd = FindWindow(null,"LogAnalyze");// get HWND if (hWnd != IntPtr.Zero)
{
Program.WINDOWPLACEMENT placement = new Program.WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement); GetWindowPlacement(hWnd, ref placement); if (placement.showCmd != SW_RESTORE)
{
placement.showCmd = SW_RESTORE; SetWindowPlacement(hWnd, ref placement);
SetForegroundWindow(hWnd);
}
} return;
} Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.Run(new LogAnalyze());//你要运行的主窗体 // keep the mutex reference alive until the normal termination of the program
GC.KeepAlive(m);
} private const int SW_NORMAL = 1; // see WinUser.h for definitions
private const int SW_RESTORE = 9; [DllImport("User32", EntryPoint = "FindWindow")]
static extern IntPtr FindWindow(string className, string windowName); [DllImport("User32", EntryPoint = "SendMessage")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport("User32", EntryPoint = "SetForegroundWindow")]
private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("User32", EntryPoint = "SetWindowPlacement")]
private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl); [DllImport("User32", EntryPoint = "GetWindowPlacement")]
private static extern bool GetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl); private struct POINTAPI
{
public int x;
public int y;
} private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
} private struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public POINTAPI ptMinPosition;
public POINTAPI ptMaxPosition;
public RECT rcNormalPosition;
}
}
}看看这个对你是否有帮助