using
System;
using System.Runtime.InteropServices;
namespace JKLib
{
/// <summary>
/// 一个程序只能启动一次实现
/// </summary>
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);
/// <summary>
/// 显示窗体命令值
/// </summary>
private const int SW_RESTORE = 9;
/// <summary>
/// 窗体名称
/// </summary>
private string WinTitle;
/// <summary>
/// 一个程序只能启动一次实现
/// </summary>
/// <param name="_WinTitle">程序名</param>
public SingleInstance(string _WinTitle)
{
WinTitle = _WinTitle;
}
private IntPtr hWnd = (System.IntPtr)null;
/// <summary>
/// 是否只有一个窗口
/// </summary>
public bool IsSingleInstance
{
get
{
hWnd = FindWindow(null,WinTitle);
return hWnd == (System.IntPtr)null;
}
}
/// <summary>
/// 使当前程序进程处于活动状态
/// </summary>
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
{
/// <summary>
/// 一个程序只能启动一次实现
/// </summary>
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);
/// <summary>
/// 显示窗体命令值
/// </summary>
private const int SW_RESTORE = 9;
/// <summary>
/// 窗体名称
/// </summary>
private string WinTitle;
/// <summary>
/// 一个程序只能启动一次实现
/// </summary>
/// <param name="_WinTitle">程序名</param>
public SingleInstance(string _WinTitle)
{
WinTitle = _WinTitle;
}
private IntPtr hWnd = (System.IntPtr)null;
/// <summary>
/// 是否只有一个窗口
/// </summary>
public bool IsSingleInstance
{
get
{
hWnd = FindWindow(null,WinTitle);
return hWnd == (System.IntPtr)null;
}
}
/// <summary>
/// 使当前程序进程处于活动状态
/// </summary>
public void RaiseOtherProcess()
{
if(hWnd == (System.IntPtr)null) return;
else
{
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd,SW_RESTORE);
}
SetForegroundWindow(hWnd);
return;
}
}
}
}
本文转自Jack Niu博客园博客,原文链接:http://www.cnblogs.com/skywind/archive/2006/08/08/470682.html,如需转载请自行联系原作者