UProcess.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.ComponentModel;
  7. using System.IO;
  8. using System.Diagnostics;
  9. using System.Runtime.InteropServices;
  10. namespace US.Browser.IE
  11. {
  12. /// <summary>
  13. /// 嵌入引用程序区域
  14. /// </summary>
  15. [ToolboxBitmap(typeof(UProcess), "UProcess.bmp")] //设置嵌入程序图标
  16. public class UProcess : Panel
  17. {
  18. private static List<UProcess> uap = new List<UProcess>(); //所有的应用窗体管理
  19. private IContainer comp = null; //容器
  20. private Process upe = null; //启动的应用程序
  21. private string name; //程序路径
  22. /// <summary>
  23. /// 应用程序路径
  24. /// </summary>
  25. public string filename
  26. {
  27. get { return name; }
  28. set
  29. {
  30. if (!value.ToLower().EndsWith(".exe")) { throw new Exception("只能启动exe一用程序"); }
  31. else if (!File.Exists(value)) { throw new Exception("路径不存在"); }
  32. name = value;
  33. }
  34. }
  35. /// <summary>
  36. /// 启动程序
  37. /// </summary>
  38. /// <returns></returns>
  39. public static UProcess init(string name)
  40. {
  41. UProcess up = new UProcess();
  42. up.filename = name; return up;
  43. }
  44. /// <summary>
  45. /// 初始化
  46. /// </summary>
  47. public UProcess()
  48. {
  49. comp = new Container(); //初始化容器
  50. }
  51. /// <summary>
  52. /// 启动程序
  53. /// </summary>
  54. public void Start()
  55. {
  56. if (upe == null && name != null)
  57. {
  58. ProcessStartInfo _ps = new ProcessStartInfo(name); //启动应用程序参数
  59. upe = Process.Start(_ps);//启动引用程序
  60. upe.WaitForInputIdle(); Application.Idle += new EventHandler(Application_Idle);
  61. }
  62. }
  63. /// <summary>
  64. /// 应用程序加载
  65. /// </summary>
  66. /// <param name="sender"></param>
  67. /// <param name="e"></param>
  68. void Application_Idle(object sender, EventArgs e)
  69. {
  70. if (this.upe == null || this.upe.HasExited)
  71. {
  72. Application.Idle -= Application_Idle; this.Dispose(); return;
  73. }
  74. else if (this.upe.MainWindowHandle == IntPtr.Zero) { return; }
  75. else //添加到制定容器中
  76. {
  77. Application.Idle -= Application_Idle;
  78. Add(this.upe, this); uap.Add(this);
  79. this.upe.EnableRaisingEvents = true;
  80. this.upe.Exited += new EventHandler(upe_Exited);
  81. }
  82. }
  83. /// <summary>
  84. /// 结束进程触发
  85. /// </summary>
  86. /// <param name="sender"></param>
  87. /// <param name="e"></param>
  88. void upe_Exited(object sender, EventArgs e)
  89. {
  90. this.comp.Dispose(); CloseP();
  91. }
  92. /// <summary>
  93. /// 添加到制定容器中
  94. /// </summary>
  95. /// <param name="app"></param>
  96. /// <param name="con"></param>
  97. private bool Add(Process app, Control con)
  98. {
  99. try
  100. {
  101. int uem = BLL.WindowHWND.SetParent(app.MainWindowHandle, con.Handle);//嵌入到父层
  102. BLL.WindowHWND.SetWindowLong(new HandleRef(this, app.MainWindowHandle), BLL.WindowHWND.GWL_STYLE, BLL.WindowHWND.WS_VISIBLE);//显示
  103. BLL.WindowHWND.MoveWindow(app.MainWindowHandle, 0, 0, con.Width, con.Height, true); //移动到指定的位置
  104. return (uem != 0);
  105. }
  106. catch (Exception) { return false; }
  107. }
  108. /// <summary>
  109. /// 结束程序
  110. /// </summary>
  111. public void CloseP()
  112. {
  113. if (this.upe != null)
  114. {
  115. try
  116. {
  117. if (!this.upe.HasExited) { this.upe.Kill(); this.upe = null; }
  118. uap.Remove(this); this.Dispose();
  119. }
  120. catch (Exception) { }
  121. }
  122. }
  123. /// <summary>
  124. /// 应用程序关闭
  125. /// </summary>
  126. /// <param name="e"></param>
  127. protected override void OnHandleDestroyed(EventArgs e)
  128. {
  129. CloseP(); base.OnHandleDestroyed(e);
  130. }
  131. /// <summary>
  132. /// 大小变化使用
  133. /// </summary>
  134. /// <param name="eventargs"></param>
  135. protected override void OnResize(EventArgs eventargs)
  136. {
  137. if (this.upe != null)
  138. {
  139. BLL.WindowHWND.MoveWindow(this.upe.MainWindowHandle, 0, 0, this.Width, this.Height, true);
  140. }
  141. base.OnResize(eventargs);
  142. }
  143. /// <summary>
  144. /// 重新绘制页面
  145. /// </summary>
  146. /// <param name="e"></param>
  147. protected override void OnSizeChanged(EventArgs e)
  148. {
  149. this.Invalidate();
  150. base.OnSizeChanged(e);
  151. }
  152. }
  153. }