天天看點

C# 通過程序名/程序Id 操作視窗/程式

原文: C# 通過程序名/程序Id 操作視窗/程式

1. 判斷視窗是否存在

1     private bool IsWindowExist(IntPtr handle)
 2     {
 3         return (!(GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && IsWindowVisible(new HandleRef(this, handle)));
 4     }
 5 
 6     [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
 7     public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd);
 8 
 9     [DllImport("user32.dll", CharSet = CharSet.Auto)]
10     public static extern bool IsWindowVisible(HandleRef hWnd);      

2. 擷取視窗句柄

1     /// <summary>
 2     /// 擷取應用程式視窗句柄
 3     /// </summary>
 4     /// <param name="processId"></param>
 5     /// <returns></returns>
 6     private IntPtr GetWindowHandle(int processId)
 7     {
 8         var windowHandle = IntPtr.Zero;
 9         EnumThreadWindowsCallback windowsCallback = new EnumThreadWindowsCallback(FindMainWindow);
10         EnumWindows(windowsCallback, IntPtr.Zero);
11         //保持循環
12         GC.KeepAlive(windowsCallback);
13 
14         bool FindMainWindow(IntPtr handle, IntPtr extraParameter)
15         {
16             int num;
17             GetWindowThreadProcessId(new HandleRef(this, handle), out num);
18             if (num == processId && IsWindowExist(handle))
19             {
20                 windowHandle = handle;
21                 return true;
22             }
23             return false;
24         }
25 
26         return windowHandle;
27     }
28     public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
29 
30     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
31     public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
32 
33     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
34     public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);      

3. 關閉應用視窗

根據程序Id關閉應用視窗:

1     /// <summary>
 2     /// 關閉程式主視窗
 3     /// </summary>
 4     /// <param name="processId">程序ID</param>
 5     public void CloseWindow(int processId)
 6     {
 7         var mwh = GetWindowHandle(processId);
 8         SendMessage(mwh, MW_CLOSE, 0, 0);
 9     }
10     const int MW_CLOSE = 0x0010;
11 
12     [DllImport("User32.dll", EntryPoint = "SendMessage")]
13     private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);      

關閉所有此程序名的視窗:

1         public void CloseAllWindows(string processName)
2         {
3             Process[] processList = Process.GetProcessesByName(processName);
4             foreach (Process process in processList)
5             {
6                 CloseMainWindow(process.Id);
7             }
8         }      

當然,直接殺程序,是最快的方法:

1     /// <summary>
 2     /// 關閉所有程序
 3     /// </summary>
 4     /// <param name="processName"></param>
 5     /// <returns></returns>
 6     public static bool CloseAllProcesses(string processName)
 7     {
 8         try
 9         {
10             Process[] psEaiNotes = Process.GetProcessesByName(processName);
11             foreach (Process psEaiNote in psEaiNotes)
12             {
13                 psEaiNote.Kill();
14             }
15         }
16         catch
17         {
18             return false;
19         }
20 
21         return true;
22     }      

4. 重新開機程式

1     string appFileName = currentClientProcess.MainModule.FileName;
 2 
 3     Process newClient = new Process();
 4     newClient.StartInfo.FileName = appFileName;
 5     // 設定要啟動的程序的初始目錄
 6     newClient.StartInfo.WorkingDirectory = System.Windows.Forms.Application.ExecutablePath;
 7     
 8     //啟動程式
 9     currentClientProcess.Kill();
10     newClient.Start();      

視窗之間發送/接收消息的處理,請參考《

C# 跨程序通信

》