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)来获取一个进程的句柄:

c#显示和隐藏另外一个进程

 

上一篇:进程 1.进程


下一篇:12月5日