using System;
using System.Runtime.InteropServices;
namespace JKLib
{
/**////
/// 一个程序只能启动一次实现
///
public class SingleInstance
{
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[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 IsIconic(IntPtr hWnd);
/**////
/// 显示窗体命令值
///
private const int SW_RESTORE = 9;
/**////
/// 窗体名称
///
private string WinTitle;
/**////
/// 一个程序只能启动一次实现
///
/// 程序名
public SingleInstance(string _WinTitle)
{
WinTitle = _WinTitle;
}
private IntPtr hWnd = (System.IntPtr)null;
/**////
/// 是否只有一个窗口
///
public bool IsSingleInstance
{
get
{
hWnd = FindWindow(null,WinTitle);
return hWnd == (System.IntPtr)null;
}
}
/**////
/// 使当前程序进程处于活动状态
///
public void RaiseOtherProcess()
{
if(hWnd == (System.IntPtr)null) return;
else
{
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd,SW_RESTORE);
}
SetForegroundWindow(hWnd);
return;
}
}
}
}
using System.Runtime.InteropServices;
namespace JKLib
{
/**////
/// 一个程序只能启动一次实现
///
public class SingleInstance
{
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[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 IsIconic(IntPtr hWnd);
/**////
/// 显示窗体命令值
///
private const int SW_RESTORE = 9;
/**////
/// 窗体名称
///
private string WinTitle;
/**////
/// 一个程序只能启动一次实现
///
/// 程序名
public SingleInstance(string _WinTitle)
{
WinTitle = _WinTitle;
}
private IntPtr hWnd = (System.IntPtr)null;
/**////
/// 是否只有一个窗口
///
public bool IsSingleInstance
{
get
{
hWnd = FindWindow(null,WinTitle);
return hWnd == (System.IntPtr)null;
}
}
/**////
/// 使当前程序进程处于活动状态
///
public void RaiseOtherProcess()
{
if(hWnd == (System.IntPtr)null) return;
else
{
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd,SW_RESTORE);
}
SetForegroundWindow(hWnd);
return;
}
}
}
}
原文网址: http://free56.cn/post/3.html