Winform容器加载并嵌入外部exe程序

  进来博客园很久了,但是却一直没怎么写博客园,都是在学习借鉴各位大神的宝贵经验。

  今天有时间也顺便整理一下,近短时间的一些学习总结吧。最近做的一个项目需要用到Winform加载嵌入本机其他exe程序,在进行了相关方法性能比对后,对最后使用的方法进行了封装。其他也不多说,下面直接上代码。

  

  1 using System;
  2 using System.Diagnostics;
  3 using System.IO;
  4 using System.Runtime.InteropServices;
  5 using System.Threading;
  6 using System.Windows.Forms;
  7 
  8 namespace SunDLL
  9 {
 10     public class WinfromUtil
 11     {
 12 
 13         /// <summary>
 14         /// 获取窗体的句柄函数
 15         /// </summary>
 16         /// <param name="lpClassName">窗口类名</param>
 17         /// <param name="lpWindowName">窗口标题名</param>
 18         /// <returns>返回句柄</returns>
 19         [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
 20         public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 21 
 22         [DllImport("User32.dll ", EntryPoint = "SetParent")]
 23         private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
 24 
 25         [DllImport("user32.dll ", EntryPoint = "ShowWindow")]
 26         public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
 27 
 28 
 29         /// <summary>
 30         /// 【函数类型:工具类对外接口】
 31         /// 【函数功能:加载外部exe程序到容器】
 32         /// 【创建作者:LZHRyan】
 33         /// 【创建时间:2018-4-5 13:52:24】
 34         /// </summary>
 35         /// <param name="exePath">exe程序路径</param>
 36         /// <param name="param">exe初始化参数</param>
 37         /// <param name="parentControl">依赖容器</param>
 38         /// <param name="formTitle">主窗体标题</param>
 39         /// <param name="canReLoad">是否允许重复运行程序</param>
 40         public static void ExeLoader(string exePath,string param,Control parentControl,string formTitle,bool canReLoad)
 41         {
 42             if(exePath.Contains(".exe") || exePath.Contains(".EXE"))
 43             {
 44                 if (File.Exists(exePath))
 45                 {
 46                     try
 47                     {
 48                         Process process = new Process();
 49                         ProcessStartInfo startInfo = new ProcessStartInfo(exePath, param);
 50                         startInfo.FileName = exePath;
 51                         startInfo.UseShellExecute = false;
 52                         startInfo.RedirectStandardOutput = false;
 53                         startInfo.WindowStyle = ProcessWindowStyle.Minimized;
 54                         
 55                         process.StartInfo = startInfo;
 56                         IntPtr mainHandle = FindWindow(null, formTitle);
 57                         process.Start();
 58                         int count = 0;
 59                         while (count < 20 * 30 && mainHandle == IntPtr.Zero)
 60                         {
 61                             count++;
 62                             Thread.Sleep(50);
 63                             mainHandle = FindWindow(null, formTitle);
 64                         }
 65                         //不允许重复启动,如果在这里限制了不能多启动相同进程,则调用的进程不能做相同的限制
 66                         if (!canReLoad && Process.GetProcessesByName(process.ProcessName).Length > 1)
 67                         {
 68                             //杀掉进程
 69                             process.Kill();
 70                             return;
 71                         }
 72                         else
 73                         {
 74                             SetParent(process.MainWindowHandle, parentControl.Handle);
 75                             ShowWindow(process.MainWindowHandle, 3);
 76                         }
 77                     }
 78                     catch(Exception ex)
 79                     {
 80                         MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
 81   
 82                     }
 83                     
 84                 }
 85                 else
 86                 {
 87                     string content = "未能找到文件:" + exePath;
 88                     string title = "错误提示";
 89                     MessageBox.Show(content, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
 90 
 91                 }
 92             }
 93             else
 94             {
 95                 string content = "文件格式不是可执行程序:" + exePath;
 96                 string title = "错误提示";
 97                 MessageBox.Show(content, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
 98 
 99             }
100         }
101     }
102 
103 }
View Code

 

posted @ 2018-04-06 17:55  TensinKiKi  阅读(2424)  评论(0)    收藏  举报