1.新建winform程序,添加一个Panel控件和一个button控件,winform窗体命名为:Mainform;

2.新建一个类文件,方便引用,命名为:exetowinform;

3.Mainform中cs代码如下:

  exetowinform fr = null;private void button1_Click(object sender, EventArgs e){            OpenFileDialog Oppf = new OpenFileDialog();Oppf.ShowDialog();if (Oppf.FileName != ""){panel1.Controls.Clear();fr = new exetowinform(panel1, "");fr.Start(Oppf.FileName);}}

4.exetowinform类文件代码如下:

 public class exetowinform{EventHandler appIdleEvent = null;Control ParentCon = null;string strGUID = "";public exetowinform(Control C, string Titlestr){appIdleEvent = new EventHandler(Application_Idle);ParentCon = C;strGUID = Titlestr;}/// <summary>/// 将属性<code>AppFilename</code>指向的应用程序打开并嵌入此容器/// </summary>public IntPtr Start(string FileNameStr){if (m_AppProcess != null){Stop();}try{ProcessStartInfo info = new ProcessStartInfo(FileNameStr);info.UseShellExecute = true;info.WindowStyle = ProcessWindowStyle.Minimized;m_AppProcess = System.Diagnostics.Process.Start(info);m_AppProcess.WaitForInputIdle();Application.Idle += appIdleEvent;}catch{if (m_AppProcess != null){if (!m_AppProcess.HasExited)m_AppProcess.Kill();m_AppProcess = null;}}return m_AppProcess.Handle;}/// <summary>/// 确保应用程序嵌入此容器/// </summary>/// <param name="sender"></param>/// <param name="e"></param>void Application_Idle(object sender, EventArgs e){if (this.m_AppProcess == null || this.m_AppProcess.HasExited){this.m_AppProcess = null;Application.Idle -= appIdleEvent;return;}while (m_AppProcess.MainWindowHandle == IntPtr.Zero){Thread.Sleep(100);m_AppProcess.Refresh();}Application.Idle -= appIdleEvent;EmbedProcess(m_AppProcess, ParentCon);}/// <summary>/// 应用程序结束运行时要清除这里的标识/// </summary>/// <param name="sender"></param>/// <param name="e"></param>void m_AppProcess_Exited(object sender, EventArgs e){m_AppProcess = null;}/// <summary>/// 将属性<code>AppFilename</code>指向的应用程序关闭/// </summary>public void Stop(){if (m_AppProcess != null)// && m_AppProcess.MainWindowHandle != IntPtr.Zero){try{if (!m_AppProcess.HasExited)m_AppProcess.Kill();}catch (Exception){}m_AppProcess = null;}}#region 属性/// <summary>/// application process/// </summary>Process m_AppProcess = null;/// <summary>/// 标识内嵌程序是否已经启动/// </summary>public bool IsStarted { get { return (this.m_AppProcess != null); } }#endregion 属性#region Win32 API[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,CharSet = CharSet.Unicode, ExactSpelling = true,CallingConvention = CallingConvention.StdCall)]private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);[DllImport("user32.dll", SetLastError = true)]private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("user32.dll", SetLastError = true)]private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]private static extern long GetWindowLong(IntPtr hwnd, int nIndex);public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong){if (IntPtr.Size == 4){return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);}return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);}[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);[DllImport("user32.dll", SetLastError = true)]private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);[DllImport("user32.dll", SetLastError = true)]private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]private static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam);[DllImport("user32.dll", SetLastError = true)]private static extern IntPtr GetParent(IntPtr hwnd);[DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);private const int SWP_NOOWNERZORDER = 0x200;private const int SWP_NOREDRAW = 0x8;private const int SWP_NOZORDER = 0x4;private const int SWP_SHOWWINDOW = 0x0040;private const int WS_EX_MDICHILD = 0x40;private const int SWP_FRAMECHANGED = 0x20;private const int SWP_NOACTIVATE = 0x10;private const int SWP_ASYNCWINDOWPOS = 0x4000;private const int SWP_NOMOVE = 0x2;private const int SWP_NOSIZE = 0x1;private const int GWL_STYLE = (-16);private const int WS_VISIBLE = 0x10000000;private const int WM_CLOSE = 0x10;private const int WS_CHILD = 0x40000000;private const int SW_HIDE = 0; //{隐藏, 并且任务栏也没有最小化图标}private const int SW_SHOWNORMAL = 1; //{用最近的大小和位置显示, 激活}private const int SW_NORMAL = 1; //{同 SW_SHOWNORMAL}private const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}private const int SW_SHOWMAXIMIZED = 3; //{最大化, 激活}private const int SW_MAXIMIZE = 3; //{同 SW_SHOWMAXIMIZED}private const int SW_SHOWNOACTIVATE = 4; //{用最近的大小和位置显示, 不激活}private const int SW_SHOW = 5; //{同 SW_SHOWNORMAL}private const int SW_MINIMIZE = 6; //{最小化, 不激活}private const int SW_SHOWMINNOACTIVE = 7; //{同 SW_MINIMIZE}private const int SW_SHOWNA = 8; //{同 SW_SHOWNOACTIVATE}private const int SW_RESTORE = 9; //{同 SW_SHOWNORMAL}private const int SW_SHOWDEFAULT = 10; //{同 SW_SHOWNORMAL}private const int SW_MAX = 10; //{同 SW_SHOWNORMAL}#endregion Win32 API/// <summary>/// 将指定的程序嵌入指定的控件/// </summary>private void EmbedProcess(Process app, Control control){// Get the main handleif (app == null || app.MainWindowHandle == IntPtr.Zero || control == null) return;try{// Put it into this formSetParent(app.MainWindowHandle, control.Handle);}catch (Exception){ }try{// Remove border and whatnot               SetWindowLong(new HandleRef(this, app.MainWindowHandle), GWL_STYLE, WS_VISIBLE);SendMessage(app.MainWindowHandle, WM_SETTEXT, IntPtr.Zero, strGUID);}catch (Exception){ }try{// Move the window to overlay it on this windowMoveWindow(app.MainWindowHandle, 0, 0, control.Width, control.Height, true);}catch (Exception){ }}[DllImport("User32.dll", EntryPoint = "SendMessage")]private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);const int WM_SETTEXT = 0x000C;}

5.最后结果如图:

exe程序嵌入Winform窗体相关推荐

  1. exe程序嵌入Winform窗体(转载)

    1.新建winform程序,添加一个Panel控件和一个button控件,winform窗体命名为:Mainform: 2.新建一个类文件,方便引用,命名为:exetowinform: 3.Mainf ...

  2. C# 将外部exe程序 嵌入到自己的窗体界面

    将别人开发的exe程序,放到自己的窗体里面来运行. 1.基本功能实现 首先,在自己的窗体后面加上代码: [DllImport("User32.dll", EntryPoint = ...

  3. C#在控制台工程中嵌入winform窗体

     关注公众号 风色年代(itfantasycc) 300G .Net 资料等你拿! 用C#编写一些后台小工具时,我们往往希望既能使用winform界面组件,又能有一个控制台窗口用于显示日志信息,可以用 ...

  4. *srv.exe蠕虫病毒打开exe程序弹浏览器窗体的解决方案

    – 问题描述 系统电脑中了蠕虫病毒后, 1.exe文件运行后,同目录下会出现一个原名 srv.exe的文件 2.exe文件运行后会把浏览器打开 解决方案: 手动修改文件权限,如下:1)删除*srv.e ...

  5. CATIA嵌入Winform

    不同于常规程序嵌入Winform,CATIA启动时先出现dos界面,然后才是CATIA主程序界面,所以需要使用EnumWindows枚举窗体,找到CATIA主窗口界面. 主要代码: /// <s ...

  6. c# 火狐浏览器怎么嵌入窗体中_C#WinForm窗体内Panel容器中嵌入子窗体、程序主窗体设计例子...

    C#WinForm父级窗体内Panel容器中嵌入子窗体.程序主窗体设计例子 在项目开发中经常遇到父级窗体嵌入子窗体所以写了一个例子程序,顺便大概划分了下界面模块和配色,不足之处还望指点 主窗体窗体采用 ...

  7. 把控制台程序嵌入到 WinForm 中执行

    我们经常有一些用控制台实现的简单应用,这种应用一般都是一步一步"向导"式执行,在每一步上收集用户的输入,最后得到程序执行的结果.但有些用户可能不喜欢用键盘操作的命令行界面,还是愿意 ...

  8. C# Winform 窗体美化(九、嵌入窗体)

    九.嵌入窗体 还是关于 Winform 窗体的一些操作问题,这次是研究了一个嵌入窗体,这次学习纯属偶然,项目中确实没遇到过这种需求.就是把别人的程序嵌入到自己的程序中,就像这样: 这里我嵌入了测试显示 ...

  9. 怎样在Winform窗体中嵌入Web浏览器

    背景 项目当中需要在Winform窗体中嵌入网页,虽然微软自带了WebBrowser控件,但是她是以IE模式运行的,兼容性实在太差,找了一圈发现有个叫CefSharp的家伙还不错,于是就拿来玩了一下. ...

最新文章

  1. 华为总裁任正非谈企业管理:正确的方向来自于妥协
  2. fiddler自动响应AutoResponder之正则匹配Rule Editor
  3. linux编译cmake
  4. 嵩天python笔记_嵩天Python学习笔记-05
  5. Jumpserver跳板机
  6. python实现项目的复制_python实现复制大量文件功能
  7. c语言求符合给定条件的整数集,中国大学MOOC-翁恺-C语言程序设计习题集(二)...
  8. vmplayer虚拟机文件夹共享不生效解决方法
  9. C++11常见编译与链接错误解决总结
  10. AHU算法课-DP动态规划
  11. 决策树分析例题经典案例_决策树例题经典案例280_决策树在产品满意因素分析中的应用...
  12. 卓有成效的管理者(笔记)——要事优先
  13. 87键键盘insert键使用方法
  14. 老司机都懂的x件事,一般人我不告诉他
  15. 解决索尼WH-1000XM3使用蓝牙连接电脑无法使用麦克风的问题
  16. 中秋节的月亮怎么拍?不用手机和相机,程序员照样能拍出大片的感觉
  17. NoSQL从小白到码神 火推08
  18. Prometheus -Grafana部署及部署告警
  19. 宇宙最强,meltdown论文中英文对照版(三)
  20. 公众号滑动图代码_公众号怎么制作图片滑动的效果?怎么做可以上下滑动的长图?...

热门文章

  1. 北京“新城卓越”黑中介租房感受
  2. CentOS编译安装Qt(Qt可使用静态编译编译器)
  3. 帝国cmssitemap.php,帝国cms百度sitemap插件
  4. 基于51单片机及PCF8591芯片的ADC电压检测
  5. Airtest+Poco多设备并发自动化游戏测试框架(遇到的问题)
  6. 一台计算机只能注册一台sql,局域网中的一台电脑为啥连接不到另一台电脑中的SQL远程数据库...
  7. 【独行秀才】macOS Big Sur 11.5 正式版(20G71)原版镜像
  8. Django MVT简单实例
  9. 天宇,snapseed,第四课,风光照片调整
  10. 复杂因子秒级计算,文谛资产是这样做到的...