• 全部
...

[分享]外部exe窗体嵌入winform

失落的神庙 2013-01-11 12:00:43
加精
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Diagnostics;
  9. using System.Runtime.InteropServices;
  10. using System.IO;
  11. using System.Drawing.Design;

  12. namespace HCDL
  13. {
  14. public class ShowForm
  15. {
  16. //-------------------
  17. //Action<object, EventArgs> appIdleAction = null;
  18. EventHandler appIdleEvent = null;

  19. Control ParentCon = null;

  20. string strGUID = "";

  21. public ShowForm(Control C,string Titlestr)
  22. {
  23. appIdleEvent = new EventHandler(Application_Idle);
  24. ParentCon = C;
  25. strGUID = Titlestr;
  26. }

  27. /// <summary>
  28. /// 将属性<code>AppFilename</code>指向的应用程序打开并嵌入此容器
  29. /// </summary>
  30. public IntPtr Start(string FileNameStr)
  31. {
  32. if (m_AppProcess != null)
  33. {
  34. Stop();
  35. }
  36. try
  37. {
  38. ProcessStartInfo info = new ProcessStartInfo(FileNameStr);
  39. info.UseShellExecute = true;
  40. info.WindowStyle = ProcessWindowStyle.Minimized;
  41. m_AppProcess = System.Diagnostics.Process.Start(info);
  42. m_AppProcess.WaitForInputIdle();
  43. Application.Idle += appIdleEvent;
  44. }
  45. catch
  46. {
  47. if (m_AppProcess != null)
  48. {
  49. if (!m_AppProcess.HasExited)
  50. m_AppProcess.Kill();
  51. m_AppProcess = null;
  52. }
  53. }
  54. return m_AppProcess.Handle;
  55. }
  56. /// <summary>
  57. /// 确保应用程序嵌入此容器
  58. /// </summary>
  59. /// <param name="sender"></param>
  60. /// <param name="e"></param>
  61. void Application_Idle(object sender, EventArgs e)
  62. {
  63. if (this.m_AppProcess == null || this.m_AppProcess.HasExited)
  64. {
  65. this.m_AppProcess = null;
  66. Application.Idle -= appIdleEvent;
  67. return;
  68. }
  69. if (m_AppProcess.MainWindowHandle == IntPtr.Zero) return;
  70. Application.Idle -= appIdleEvent;
  71. EmbedProcess(m_AppProcess, ParentCon);
  72. }
  73. /// <summary>
  74. /// 应用程序结束运行时要清除这里的标识
  75. /// </summary>
  76. /// <param name="sender"></param>
  77. /// <param name="e"></param>
  78. void m_AppProcess_Exited(object sender, EventArgs e)
  79. {
  80. m_AppProcess = null;
  81. }
  82. /// <summary>
  83. /// 将属性<code>AppFilename</code>指向的应用程序关闭
  84. /// </summary>
  85. public void Stop()
  86. {
  87. if (m_AppProcess != null)// && m_AppProcess.MainWindowHandle != IntPtr.Zero)
  88. {
  89. try
  90. {
  91. if (!m_AppProcess.HasExited)
  92. m_AppProcess.Kill();
  93. }
  94. catch (Exception)
  95. {
  96. }
  97. m_AppProcess = null;
  98. }
  99. }


  100. #region 属性
  101. /// <summary>
  102. /// application process
  103. /// </summary>
  104. Process m_AppProcess = null;

  105. /// <summary>
  106. /// 标识内嵌程序是否已经启动
  107. /// </summary>
  108. public bool IsStarted { get { return (this.m_AppProcess != null); } }

  109. #endregion 属性

  110. #region Win32 API
  111. [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
  112. CharSet = CharSet.Unicode, ExactSpelling = true,
  113. CallingConvention = CallingConvention.StdCall)]
  114. private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);

  115. [DllImport("user32.dll", SetLastError = true)]
  116. private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

  117. [DllImport("user32.dll", SetLastError = true)]
  118. private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

  119. [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
  120. private static extern long GetWindowLong(IntPtr hwnd, int nIndex);

  121. public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
  122. {
  123. if (IntPtr.Size == 4)
  124. {
  125. return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
  126. }
  127. return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
  128. }
  129. [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
  130. public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);
  131. [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
  132. public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);

  133. [DllImport("user32.dll", SetLastError = true)]
  134. private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);

  135. [DllImport("user32.dll", SetLastError = true)]
  136. private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

  137. [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
  138. private static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam);

  139. [DllImport("user32.dll", SetLastError = true)]
  140. private static extern IntPtr GetParent(IntPtr hwnd);

  141. [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
  142. static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);



  143. private const int SWP_NOOWNERZORDER = 0x200;
  144. private const int SWP_NOREDRAW = 0x8;
  145. private const int SWP_NOZORDER = 0x4;
  146. private const int SWP_SHOWWINDOW = 0x0040;
  147. private const int WS_EX_MDICHILD = 0x40;
  148. private const int SWP_FRAMECHANGED = 0x20;
  149. private const int SWP_NOACTIVATE = 0x10;
  150. private const int SWP_ASYNCWINDOWPOS = 0x4000;
  151. private const int SWP_NOMOVE = 0x2;
  152. private const int SWP_NOSIZE = 0x1;
  153. private const int GWL_STYLE = (-16);
  154. private const int WS_VISIBLE = 0x10000000;
  155. private const int WM_CLOSE = 0x10;
  156. private const int WS_CHILD = 0x40000000;

  157. private const int SW_HIDE = 0; //{隐藏, 并且任务栏也没有最小化图标}
  158. private const int SW_SHOWNORMAL = 1; //{用最近的大小和位置显示, 激活}
  159. private const int SW_NORMAL = 1; //{同 SW_SHOWNORMAL}
  160. private const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}
  161. private const int SW_SHOWMAXIMIZED = 3; //{最大化, 激活}
  162. private const int SW_MAXIMIZE = 3; //{同 SW_SHOWMAXIMIZED}
  163. private const int SW_SHOWNOACTIVATE = 4; //{用最近的大小和位置显示, 不激活}
  164. private const int SW_SHOW = 5; //{同 SW_SHOWNORMAL}
  165. private const int SW_MINIMIZE = 6; //{最小化, 不激活}
  166. private const int SW_SHOWMINNOACTIVE = 7; //{同 SW_MINIMIZE}
  167. private const int SW_SHOWNA = 8; //{同 SW_SHOWNOACTIVATE}
  168. private const int SW_RESTORE = 9; //{同 SW_SHOWNORMAL}
  169. private const int SW_SHOWDEFAULT = 10; //{同 SW_SHOWNORMAL}
  170. private const int SW_MAX = 10; //{同 SW_SHOWNORMAL}

  171. #endregion Win32 API

  172. /// <summary>
  173. /// 将指定的程序嵌入指定的控件
  174. /// </summary>
  175. private void EmbedProcess(Process app, Control control)
  176. {
  177. // Get the main handle
  178. if (app == null || app.MainWindowHandle == IntPtr.Zero || control == null) return;
  179. try
  180. {
  181. // Put it into this form
  182. SetParent(app.MainWindowHandle, control.Handle);
  183. }
  184. catch (Exception)
  185. { }
  186. try
  187. {
  188. // Remove border and whatnot
  189. SetWindowLong(new HandleRef(this, app.MainWindowHandle), GWL_STYLE, WS_VISIBLE);
  190. SendMessage(app.MainWindowHandle, WM_SETTEXT, IntPtr.Zero, strGUID);
  191. }
  192. catch (Exception)
  193. { }
  194. try
  195. {
  196. // Move the window to overlay it on this window
  197. MoveWindow(app.MainWindowHandle, -20, -20, control.Width, control.Height-10, true);
  198. }
  199. catch (Exception)
  200. { }
  201. }

  202. [DllImport("User32.dll", EntryPoint = "SendMessage")]
  203. private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

  204. const int WM_SETTEXT = 0x000C;
  205. }
  206. }







PS:以前是3.5版本。现在改成2.0也能用的了

用法


  1. ShowForm Sf = new ShowForm(this, "仅供内部使用" + System.Guid.NewGuid().ToString());
  2. ProxHandle= Sf.Start(Application.StartupPath + "\\ProxyThorn.exe");





第三方exe



winform效果图
...全文
给本帖投票
94264 130 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
130 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞天凤凰601 2015-09-15
  • 打赏
  • 举报
回复
这个东西好呀,谢谢!!!
sowinwork 2015-09-08
  • 打赏
  • 举报
回复
备用。。。。。。。。。
baidu_31130099 2015-09-07
  • 打赏
  • 举报
回复
要怎么把cmd命令窗口给嵌入进去啊
Jack_calt 2015-02-01
  • 打赏
  • 举报
回复
看起来很牛逼的样子
A971319231 2014-12-12
  • 打赏
  • 举报
回复 1
那个嵌进去的exe的关闭按钮和最小化按钮都不见了。。怎么解决?
A971319231 2014-12-10
  • 打赏
  • 举报
回复
请问一下楼主的可以再窗体里面拖动另外一个窗体吗?我的嵌进去的窗体的关闭按钮都不见了
a3212b12 2014-09-26
  • 打赏
  • 举报
回复
学习下,厉害。。。
zyj_604 2014-08-30
  • 打赏
  • 举报
回复
不错,标记一下。说不定以后能用到
失落的神庙 2014-08-27
  • 打赏
  • 举报
回复
引用 104 楼 tranlynn 的回复:
强大,要是源码能下载就好了。
引用 112 楼 liaodm 的回复:
这个程序,谁有联系到楼主,求楼主联系方式, 楼主如有看到回贴,烦请联系一下本人,有尝请教。77190062@qq.com
引用 111 楼 Gi_gi 的回复:
[quote=引用 31 楼 gupiao175 的回复:] 不好意思,没有注意看。有提供实例代码的。呵呵!
实例代码在哪里,我怎么没找到??[/quote] 96楼实例
  • 打赏
  • 举报
回复
Proxhandle 是什么东东?
磨砻淬砺 2014-08-19
  • 打赏
  • 举报
回复
好东西,记录下,以后慢慢学习。
d1070193993 2014-07-05
  • 打赏
  • 举报
回复
mark以后肯定有用
Ki1381 2014-07-04
  • 打赏
  • 举报
回复
牛叉,必须马克
xbl002 2013-12-19
  • 打赏
  • 举报
回复
标记下,以后也许会用到
sanweixianshi 2013-12-19
  • 打赏
  • 举报
回复
哈哈,学习,收藏一下!
riverswan 2013-12-18
  • 打赏
  • 举报
回复
真强 ......
txian001 2013-12-16
  • 打赏
  • 举报
回复
嵌入不了wps软件。做一个试试。
vanfy1983 2013-11-27
  • 打赏
  • 举报
回复
好帖子,感谢大家的热烈讨论,受益匪浅!
hao65717016 2013-11-22
  • 打赏
  • 举报
回复
楼主,有没有WPF版本的代码
chutao 2013-11-14
  • 打赏
  • 举报
回复
Good,收藏
加载更多回复(110)

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧

手机看
关注公众号

关注公众号

客服 返回
顶部