EmbedPanel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Runtime.InteropServices;
  13. namespace Y.Skin.YoPanel
  14. {
  15. public partial class EmbedPanel : Panel
  16. {
  17. Action<object, EventArgs> appIdleAction = null;
  18. EventHandler appIdleEvent = null;
  19. public EmbedPanel()
  20. {
  21. InitializeComponent();
  22. appIdleAction = new Action<object, EventArgs>(Application_Idle);
  23. appIdleEvent = new EventHandler(appIdleAction);
  24. }
  25. public EmbedPanel(IContainer container)
  26. {
  27. container.Add(this);
  28. InitializeComponent();
  29. appIdleAction = new Action<object, EventArgs>(Application_Idle);
  30. appIdleEvent = new EventHandler(appIdleAction);
  31. }
  32. /// <summary>
  33. /// 将属性<code>AppFilename</code>指向的应用程序打开并嵌入此容器
  34. /// </summary>
  35. public void Start()
  36. {
  37. if (m_AppProcess != null)
  38. {
  39. Stop();
  40. }
  41. try
  42. {
  43. ProcessStartInfo info = new ProcessStartInfo(this.m_AppFilename);
  44. info.UseShellExecute = true;
  45. info.WindowStyle = ProcessWindowStyle.Minimized;
  46. //info.WindowStyle = ProcessWindowStyle.Hidden;
  47. m_AppProcess = Process.Start(info);
  48. // Wait for process to be created and enter idle condition
  49. m_AppProcess.WaitForInputIdle();
  50. //todo:下面这两句会引发 NullReferenceException 异常,不知道怎么回事
  51. //m_AppProcess.Exited += new EventHandler(m_AppProcess_Exited);
  52. //m_AppProcess.EnableRaisingEvents = true;
  53. Application.Idle += appIdleEvent;
  54. }
  55. catch (Exception ex)
  56. {
  57. MessageBox.Show(this, string.Format("{1}{0}{2}{0}{3}"
  58. , Environment.NewLine
  59. , "*" + ex.ToString()
  60. , "*StackTrace:" + ex.StackTrace
  61. , "*Source:" + ex.Source
  62. ), "内嵌程序加载失败");
  63. if (m_AppProcess != null)
  64. {
  65. if (!m_AppProcess.HasExited)
  66. m_AppProcess.Kill();
  67. m_AppProcess = null;
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// 确保应用程序嵌入此容器
  73. /// </summary>
  74. /// <param name="sender"></param>
  75. /// <param name="e"></param>
  76. void Application_Idle(object sender, EventArgs e)
  77. {
  78. if (this.m_AppProcess == null || this.m_AppProcess.HasExited)
  79. {
  80. this.m_AppProcess = null;
  81. Application.Idle -= appIdleEvent;
  82. return;
  83. }
  84. if (m_AppProcess.MainWindowHandle == IntPtr.Zero) return;
  85. Application.Idle -= appIdleEvent;
  86. EmbedProcess(m_AppProcess, this);
  87. //ShowWindow(m_AppProcess.MainWindowHandle, SW_SHOWNORMAL);
  88. //var parent = GetParent(m_AppProcess.MainWindowHandle);//你妹,不管用,全是0
  89. //if (parent == this.Handle)
  90. //{
  91. // Application.Idle -= appIdleEvent;
  92. //}
  93. }
  94. /// <summary>
  95. /// 应用程序结束运行时要清除这里的标识
  96. /// </summary>
  97. /// <param name="sender"></param>
  98. /// <param name="e"></param>
  99. void m_AppProcess_Exited(object sender, EventArgs e)
  100. {
  101. m_AppProcess = null;
  102. }
  103. //public void Start(string appFilename)
  104. //{
  105. // this.AppFilename = AppFilename;
  106. // Start();
  107. //}
  108. /// <summary>
  109. /// 将属性<code>AppFilename</code>指向的应用程序关闭
  110. /// </summary>
  111. public void Stop()
  112. {
  113. if (m_AppProcess != null)// && m_AppProcess.MainWindowHandle != IntPtr.Zero)
  114. {
  115. try
  116. {
  117. if (!m_AppProcess.HasExited)
  118. m_AppProcess.Kill();
  119. }
  120. catch (Exception)
  121. {
  122. }
  123. m_AppProcess = null;
  124. }
  125. }
  126. protected override void OnHandleDestroyed(EventArgs e)
  127. {
  128. Stop();
  129. base.OnHandleDestroyed(e);
  130. }
  131. protected override void OnResize(EventArgs eventargs)
  132. {
  133. if (m_AppProcess != null)
  134. {
  135. MoveWindow(m_AppProcess.MainWindowHandle, 0, 0, this.Width, this.Height, true);
  136. }
  137. base.OnResize(eventargs);
  138. }
  139. protected override void OnSizeChanged(EventArgs e)
  140. {
  141. this.Invalidate();
  142. base.OnSizeChanged(e);
  143. }
  144. #region 属性
  145. /// <summary>
  146. /// application process
  147. /// </summary>
  148. Process m_AppProcess = null;
  149. public Process AppProcess
  150. {
  151. get { return this.m_AppProcess; }
  152. set { this.m_AppProcess = value; }
  153. }
  154. /// <summary>
  155. /// 要嵌入的程序文件名
  156. /// </summary>
  157. private string m_AppFilename = "";
  158. /// <summary>
  159. /// 要嵌入的程序文件名
  160. /// </summary>
  161. [Category("Data")]
  162. [Description("要嵌入的程序文件名")]
  163. [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  164. //[Editor(typeof(AppFilenameEditor), typeof(UITypeEditor))]
  165. public string AppFilename
  166. {
  167. get
  168. {
  169. return m_AppFilename;
  170. }
  171. set
  172. {
  173. if (value == null || value == m_AppFilename) return;
  174. var self = Application.ExecutablePath;
  175. if (value.ToLower() == self.ToLower())
  176. {
  177. MessageBox.Show("不能自己嵌入自己!", "SmileWei.EmbeddedApp");
  178. return;
  179. }
  180. if (!value.ToLower().EndsWith(".exe"))
  181. {
  182. MessageBox.Show("要嵌入的文件扩展名不是exe!", "SmileWei.EmbeddedApp");
  183. }
  184. if (!File.Exists(value))
  185. {
  186. MessageBox.Show("要嵌入的程序不存在!", "SmileWei.EmbeddedApp");
  187. return;
  188. }
  189. m_AppFilename = value;
  190. }
  191. }
  192. /// <summary>
  193. /// 标识内嵌程序是否已经启动
  194. /// </summary>
  195. public bool IsStarted { get { return (this.m_AppProcess != null); } }
  196. #endregion 属性
  197. #region Win32 API
  198. [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
  199. CharSet = CharSet.Unicode, ExactSpelling = true,
  200. CallingConvention = CallingConvention.StdCall)]
  201. private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);
  202. [DllImport("user32.dll", SetLastError = true)]
  203. private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  204. [DllImport("user32.dll", SetLastError = true)]
  205. public static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
  206. [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
  207. private static extern long GetWindowLong(IntPtr hwnd, int nIndex);
  208. public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
  209. {
  210. if (IntPtr.Size == 4)
  211. {
  212. return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
  213. }
  214. return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
  215. }
  216. [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
  217. public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);
  218. [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
  219. public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);
  220. [DllImport("user32.dll", SetLastError = true)]
  221. private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);
  222. [DllImport("user32.dll", SetLastError = true)]
  223. private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
  224. [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
  225. private static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam);
  226. [DllImport("user32.dll", SetLastError = true)]
  227. private static extern IntPtr GetParent(IntPtr hwnd);
  228. ///// <summary>
  229. ///// ShellExecute(IntPtr.Zero, "Open", "C:/Program Files/TTPlayer/TTPlayer.exe", "", "", 1);
  230. ///// </summary>
  231. ///// <param name="hwnd"></param>
  232. ///// <param name="lpOperation"></param>
  233. ///// <param name="lpFile"></param>
  234. ///// <param name="lpParameters"></param>
  235. ///// <param name="lpDirectory"></param>
  236. ///// <param name="nShowCmd"></param>
  237. ///// <returns></returns>
  238. //[DllImport("shell32.dll", EntryPoint = "ShellExecute")]
  239. //public static extern int ShellExecute(
  240. //IntPtr hwnd,
  241. //string lpOperation,
  242. //string lpFile,
  243. //string lpParameters,
  244. //string lpDirectory,
  245. //int nShowCmd
  246. //);
  247. //[DllImport("kernel32.dll")]
  248. //public static extern int OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  249. [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
  250. static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  251. private const int SWP_NOOWNERZORDER = 0x200;
  252. private const int SWP_NOREDRAW = 0x8;
  253. private const int SWP_NOZORDER = 0x4;
  254. private const int SWP_SHOWWINDOW = 0x0040;
  255. private const int WS_EX_MDICHILD = 0x40;
  256. private const int SWP_FRAMECHANGED = 0x20;
  257. private const int SWP_NOACTIVATE = 0x10;
  258. private const int SWP_ASYNCWINDOWPOS = 0x4000;
  259. private const int SWP_NOMOVE = 0x2;
  260. private const int SWP_NOSIZE = 0x1;
  261. private const int GWL_STYLE = (-16);
  262. private const int WS_VISIBLE = 0x10000000;
  263. private const int WM_CLOSE = 0x10;
  264. private const int WS_CHILD = 0x40000000;
  265. private const int SW_HIDE = 0; //{隐藏, 并且任务栏也没有最小化图标}
  266. private const int SW_SHOWNORMAL = 1; //{用最近的大小和位置显示, 激活}
  267. private const int SW_NORMAL = 1; //{同 SW_SHOWNORMAL}
  268. private const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}
  269. private const int SW_SHOWMAXIMIZED = 3; //{最大化, 激活}
  270. private const int SW_MAXIMIZE = 3; //{同 SW_SHOWMAXIMIZED}
  271. private const int SW_SHOWNOACTIVATE = 4; //{用最近的大小和位置显示, 不激活}
  272. private const int SW_SHOW = 5; //{同 SW_SHOWNORMAL}
  273. private const int SW_MINIMIZE = 6; //{最小化, 不激活}
  274. private const int SW_SHOWMINNOACTIVE = 7; //{同 SW_MINIMIZE}
  275. private const int SW_SHOWNA = 8; //{同 SW_SHOWNOACTIVATE}
  276. private const int SW_RESTORE = 9; //{同 SW_SHOWNORMAL}
  277. private const int SW_SHOWDEFAULT = 10; //{同 SW_SHOWNORMAL}
  278. private const int SW_MAX = 10; //{同 SW_SHOWNORMAL}
  279. //const int PROCESS_ALL_ACCESS = 0x1F0FFF;
  280. //const int PROCESS_VM_READ = 0x0010;
  281. //const int PROCESS_VM_WRITE = 0x0020;
  282. #endregion Win32 API
  283. public void EmbedAgain()
  284. {
  285. EmbedProcess(m_AppProcess, this);
  286. MessageBox.Show("完毕");
  287. }
  288. /// <summary>
  289. /// 将指定的程序嵌入指定的控件
  290. /// </summary>
  291. private void EmbedProcess(Process app, Control control)
  292. {
  293. // Get the main handle
  294. if (app == null || app.MainWindowHandle == IntPtr.Zero || control == null) return;
  295. try
  296. {
  297. // Put it into this form
  298. SetParent(app.MainWindowHandle, control.Handle);
  299. }
  300. catch (Exception)
  301. { }
  302. try
  303. {
  304. // Remove border and whatnot
  305. SetWindowLong(new HandleRef(this, app.MainWindowHandle), GWL_STYLE, WS_VISIBLE);
  306. }
  307. catch (Exception)
  308. { }
  309. try
  310. {
  311. // Move the window to overlay it on this window
  312. MoveWindow(app.MainWindowHandle, 0, 0, control.Width, control.Height, true);
  313. }
  314. catch (Exception)
  315. { }
  316. }
  317. }
  318. }