2015年3月6日 星期五

[C#]避免重覆開啟程式

資料來源 http://jiunway.blogspot.tw/2010/07/c_15.html

我是開一個只有一個Form1的,把程式寫在Program.cs中

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Reflection;  
 using System.Runtime.InteropServices;  
 using System.Text;  
 using System.Threading.Tasks;  
 using System.Windows.Forms;  
 namespace 禁止多開  
 {  
   static class Program  
   {  
     static readonly int SW_SHOWNORMAL = 9;  
     //==========================================================  
     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
     static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);  
     //==========================================================  
     private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);  
     //==========================================================  
     [DllImport("user32.dll")]  
     [return: MarshalAs(UnmanagedType.Bool)]  
     static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);  
     //==========================================================  
     [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]  
     static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);  
     //==========================================================  
     [DllImport("user32.dll")]  
     [return: MarshalAs(UnmanagedType.Bool)]  
     static extern bool IsWindowVisible(IntPtr hWnd);  
     //==========================================================  
     [DllImport("user32.dll")]  
     [return: MarshalAs(UnmanagedType.Bool)]  
     static extern bool IsWindow(IntPtr hWnd);  
     //==========================================================  
     [DllImport("user32.dll")]  
     static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);  
     // For Windows Mobile, replace user32.dll with coredll.dll  
     [DllImport("user32.dll")]  
     [return: MarshalAs(UnmanagedType.Bool)]  
     static extern bool SetForegroundWindow(IntPtr hWnd);  
     //==========================================================  
     [DllImport("user32.dll")]  
     public static extern IntPtr GetParent(IntPtr hWnd);  
     //==========================================================  
     /// <summary>  
     /// 應用程式的主要進入點。  
     /// </summary>  
     [STAThread]  
     static void Main()  
     {  
       //取得執行階段應用程式GUID  
       Assembly assembly = Assembly.GetExecutingAssembly();  
       var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0];  
       var id = attribute.Value;  
       string appGuid = id;  
       //判斷全域"Global\\" + appGUID是否已存在  
       bool result;  
       using (var mutex = new System.Threading.Mutex(false, "Global\\" + appGuid, out result))  
       {  
         //MessageBox.Show(appGuid + " " + result);  
         //已存在  
         if (!result)  
         {  
           //呼叫原始視窗起來  
           EnumWindowsProc CallOriginalFunction = new EnumWindowsProc(EnumProc);  
           EnumWindows(CallOriginalFunction, IntPtr.Zero);  
         }  
         else  
         {  
           //不存在  
           Application.EnableVisualStyles();  
           Application.SetCompatibleTextRenderingDefault(false);  
           Application.Run(new Form1());  
         }  
       }  
     }  
     public static bool EnumProc(IntPtr hWnd, IntPtr lParam)  
     {  
       StringBuilder WindowsText = new StringBuilder(255);  
       StringBuilder ClassName = new StringBuilder(255);  
       if (GetParent(hWnd) == IntPtr.Zero)  
       {  
         if (IsWindow(hWnd))  
         {  
           GetWindowText(hWnd, WindowsText, 255);  
           if (WindowsText.Length != 0)  
           {  
             if (WindowsText.ToString().Contains("禁止多開example"))  
             {  
               ShowWindow(hWnd, SW_SHOWNORMAL);  
               SetForegroundWindow(hWnd);  
             }  
           }  
         }  
       }  
       return true;  
     }  
   }  
 }  

這樣子當我已運行一個程式時,它會去找到與你設定相同(包含)title的應用程式叫起來。例如本例就會去捉禁止多開example ( 也就是俺設定Form1的text)



今天如果我把要呼叫的title改成未命名,並且開了一個新的記事本

















沒有留言:

張貼留言