c#顯示和隱藏另外一個進程


 1         /// <summary>
 2         /// 0-關閉窗口 1-正常大小顯示 2最小化窗口 3-最大化窗口
 3         /// </summary>
 4         /// <param name="hwnd"></param>
 5         /// <param name="nCmdShow"></param>
 6         /// <returns></returns>
 7         [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
 8         public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
 9 
10         /// <summary>
11         /// 顯示程序
12         /// </summary>
13         /// <param name="hwnd">窗口句柄</param>
14         /// <param name="nCmdshow">0-關閉窗口 1-正常大小顯示 2最小化窗口 3-最大化窗口</param>
15         /// <returns></returns>
16         public int DisPlayWindow(IntPtr hwnd, int nCmdshow)
17         {
18             return ShowWindow(hwnd, nCmdshow);
19         }
20 
21         public Process Run(string exeName)
22         {
23             return Process.Start(exeName);
24         }
25 
26         IntPtr hWnd = IntPtr.Zero;
27         Process process=null;
28         bool IsMini = true;
29         int nCmdshow = 1;
30         private void button1_Click(object sender, EventArgs e)
31         {
32             //獲取句柄
33             if (process == null)
34             {
35                 string exeName = Application.StartupPath + @"\CCD\CameraVision.exe";
36                 //創建啟動進程信息
37                 process = Run(exeName);
38             }
39             else
40             {
41                 if (IsMini)
42                 {
43                     DisPlayWindow(process.MainWindowHandle, 1);
44                     IsMini = false;
45                 }
46                 else
47                 {
48                     DisPlayWindow(process.MainWindowHandle, 2);
49                     IsMini = true;
50                 }
51             }
52         }

這里實現的是點擊一個CCD按鈕,打開CCD窗口,再點擊CCD按鈕,隱藏CCD窗口,測試發現,句柄不能通過這種方式獲取:IntPtr hWnd=process.MainWindowHandle;而是直接用process.MainWindowHandle來表示這個進程的句柄,才可以實現。

這里還有一些其他的方法:

        /// <summary>
        /// 0-隱藏窗口 1-正常大小顯示
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="_value"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern IntPtr ShowWindow(IntPtr hWnd, int _value);

        /// <summary>
        /// 獲取窗口句柄
        /// </summary>
        /// <param name="lp1"></param>
        /// <param name="lp2"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lp1, string lp2);

        /// <summary>
        /// 獲取當前窗口的句柄
        /// </summary>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();

/下面是通過進程的名字(CameraVision.exe)來獲取一個進程的句柄:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM