SingleApplication
    {
        private const int SW_RESTORE = 9;
        
private static string sTitle;
        
private static IntPtr windowHandle;

        
/// <summary>
        
/// Imports 
        
/// </summary>
        [DllImport("user32.Dll")]
        
private static extern int EnumWindows(EnumWinCallBack callBackFunc, int lParam);

        [DllImport(
"User32.Dll")]
        
private static extern void GetWindowText(int hWnd, StringBuilder str, int nMaxCount);

        [DllImport(
"user32.dll", EntryPoint = "SetForegroundWindow")]
        
private static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport(
"user32.dll")]
        
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

        
/// <summary>
        
/// EnumWindowCallBack
        
/// </summary>
        
/// <param name="hwnd"></param>
        
/// <param name="lParam"></param>
        
/// <returns></returns>
        private static bool EnumWindowCallBack(int hwnd, int lParam)
        {
            windowHandle 
= (IntPtr) hwnd;

            StringBuilder sbuilder 
= new StringBuilder(256);
            GetWindowText((
int) windowHandle, sbuilder, sbuilder.Capacity);
            
string strTitle = sbuilder.ToString();

            
if (strTitle == sTitle)
            {
                ShowWindow(windowHandle, SW_RESTORE);
                SetForegroundWindow(windowHandle);
                
return false;
            }
            
return true;
        } 
//EnumWindowCallBack

        
/// <summary>
        
/// Execute a form base application if another instance already running on
        
/// the system activate previous one
        
/// </summary>
        
/// <param name="frmMain">main form</param>
        
/// <returns>true if no previous instance is running</returns>
        public static bool Run(Form frmMain)
        {
            sTitle 
= frmMain.Text;

            
if (EnumWindows(EnumWindowCallBack, 0== 0)
            {
                
return false;
            }
            Application.Run(frmMain);
            
return true;
        }

        
public static bool Run(string frmText)
        {
            sTitle 
= frmText;

            
if (EnumWindows(EnumWindowCallBack, 0== 0)
            {
                
return false;
            }
            
return true;
        }

        
/// <summary>
        
/// 
        
/// </summary>
        
/// <returns></returns>
        public static bool Run()
        {
            Process pr 
= Process.GetCurrentProcess();
            
string strProcessName = pr.ProcessName;
            
if (Process.GetProcessesByName(strProcessName).Length > 1)
            {
                
return false;
            }
            
return true;
        }

        
#region Nested type: EnumWinCallBack

        
private delegate bool EnumWinCallBack(int hwnd, int lParam);

        
#endregion
    }

 

调用方式:

if (SingleApplication.Run("Form1"))//Form1为应用程序入口窗体
    Application.Run(new Form1());

//或者
//if (SingleApplication.Run(new Form1().Text))//Form1为应用程序入口窗体
    
//Application.Run(new Form1());

//或者
//SingleApplication.Run(new Form1())

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-31
  • 2022-02-20
  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-04
  • 2021-06-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
  • 2021-10-29
相关资源
相似解决方案