C#将exe运行程序嵌入到自己的winform窗体中
以下例子是将Word打开,然后将它嵌入到winform窗体中,效果如下图:
C将exe运行程序嵌入到自己的winform窗体中 - kingmax_res - iSport
注意:该方法只适用于com的exe(如word,Excel之类),.net的编的exe就不能用这用方法嵌入到窗体中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WindowsTest
{
public partial class Form2 : Form
{
Process process = null;
IntPtr appWin;
private string exeName = "";
[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); [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);
//private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr 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, long wParam, long lParam);
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;
public string ExeName
{
get
{
return exeName;
}
set
{
exeName = value;
}
} public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.exeName = @"D:\Program Files\Microsoft Office\OFFICE11\WINWORD.exe";
try
{
// Start the process
process = System.Diagnostics.Process.Start(this.exeName);
// Wait for process to be created and enter idle condition
process.WaitForInputIdle();
// Get the main handle
appWin = process.MainWindowHandle;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Error");
}
// Put it into this form
SetParent(appWin, this.Handle);
// Remove border and whatnot
// SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);
// Move the window to overlay it on this window
MoveWindow(appWin, 0, 0, this.Width, this.Height, true);
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
process.Kill();
}
catch { }
}
private void Form2_Resize(object sender, EventArgs e)
{
if (this.appWin != IntPtr.Zero)
{
MoveWindow(appWin, 0, 0, this.Width, this.Height, true);
}
//base.OnResize(e);
}
}
}
C#将exe运行程序嵌入到自己的winform窗体中的更多相关文章
- [工作札记]03: 微软Winform窗体中ListView、DataGridView等控件的Bug,会导致程序编译失败,影响范围:到最新的.net4.7.2都有
工作中,我们发现了微软.net WinForm的一个Bug,会导致窗体设计器自动生成的代码失效,这个Bug从.net4.5到最新的.net4.7.2都存在,一直没有解决.最初是我在教学工作中发现的,后 ...
- Deepin 15.9系统直接运行exe运行程序
以下为你介绍在深度Deepin 15.9 Linux操作系统下直接运行exe文件的方法,此方法基于deepin-wine实现,经测试,一些exe文件是可以正常打开的,但部分可能会出现无法使用的情况,但 ...
- winform窗体程序运行后怎样隐藏?
运行winform窗体,我们是怎样隐藏的呢? 例子: 1)创建简单winform窗体 2)编写隐藏窗体程序的代码 3)效果演示 1)创建一个简单的winform窗体MainForm,
- VS2017编译项目出现提示al.exe运行失败的解决方法
VS2013中编译一切正常,用VS2017打开项目,某个类库出现al.exe运行失败的解决方法,事件查看器中这样描述 “C:\Program Files (x86)\Microsoft SDKs\Wi ...
- 把演讲人的桌面、头像、声音合成后推送到 指定的直播流平台上; 录制电脑桌面、摄像头头像、声音保存为本地视频; 适用于讲课老师、医生等演讲内容保存为视频; 提供PPT嵌入Winform/WPF解决方案,Winform/WPF 中嵌入 office ppt 解决方案
提供PPT嵌入Winform/WPF解决方案,Winform/WPF 中嵌入 office ppt 解决方案 Winform/WPF 中嵌入 office ppt(powerpoint)解决方案示: ...
- exe程序嵌入Winform窗体
1.新建winform程序,添加一个Panel控件和一个button控件,winform窗体命名为:Mainform: 2.新建一个类文件,方便引用,命名为:exetowinform: 3.Mainf ...
- 开发系统时候运行程序突然报出“WebDev.WebServer40.exe已停止工作”的错误
已经解决,问题描述:在开发系统时候运行程序突然报出“WebDev.WebServer40.exe已停止工作”的错误,程序调试运行,发现程序在打开数据库时候报错,也就是Connection.Open() ...
- 以不同用户身份运行程序,/savecred只需要输入一次密码(GetTokenByName取得EXPLORER.EXE的令牌,然后调用CreateProcessAsUser,而且使用LoadUserProfile解决另存文件的问题)good
http://blog.sina.com.cn/s/blog_65977dde0100s7tm.html ----------------------------------------------- ...
- 程序编译运行和exe运行之文件位置的区别
如图: 文件输入输出 1.程序编译运行 输入文件和输出文件与.c同位置 2.exe运行 输入文件和输出文件与.exe同位置
随机推荐
- 【汇总】Android 常用方法整理
1.解决ActionBar OverFlow按钮不显示.(在oncreate中调用即可) private void setOverflowShowingAlways() { try { ViewCon ...
- NOIP 考前 KMP练习
BZOJ 1461 && BZOJ 1729 KMP+BIT 一看就是字符串匹配但是不同的是要按照每个字符的排名情况. 首先对于数字x的排名,那么要判断x前小于x的数的个数,和x前小于 ...
- Kruskal算法
1.基本思想:设无向连通网为G=(V, E),令G的最小生成树为T=(U, TE),其初态为U=V,TE={ },然后,按照边的权值由小到大的顺序,考察G的边集E中的各条边.若被考察的边的两个顶点属于 ...
- 库函数API和C语言汇编语言混合式编程
C语言代码内嵌汇编的方法: 在C语言文件中以如下格式加入汇编代码 __asm__( “汇编语句模板” :输出部分 :输入部分 :“破坏描述部分” ) asm可以由__asm__代替,为其别名. 可加上 ...
- Linux 路线 推荐
1.<Linux程序设计>- 靠它来入门,然后装一个linux体系,练习shell(party)和linuxC,把基础打牢: 2. <深入理解Linux内核>和<Linu ...
- C#获得网卡信息 NetworkInterface IPInterfaceProperties
System.Net.NetworkInformation下的 1:NetworkInterface类,提供网络适配器的配置和统计信息. 可以通过它检测本机配置了多少网卡,哪些网络连接可用,获得网卡的 ...
- Telnet
http://blog.sina.com.cn/s/blog_607072980102uy06.html
- Twitter API 申请key
最近听了一下coursera的python课(https://www.coursera.org/learn/python-network-data/home/welcome),讲的挺简单也挺有意思.其 ...
- mysqldump-info
其实很多东西都能在info里面找到非常详细的说明,只是,我们太忙了,只想要一个答案,而无心去看而已,所以呢,就把用得到的都看一下来记录吧. 命令模式:mysqldump [options] [db_n ...
- kindeditor本地上传报错,只限初学者
困扰了我三天的问题,话说百度真的害死人啊,百度上有说路劲错了的,有说包没导的,有说还要改plugins里面的文件的!其实这个都不用动,也有说服务器问题的,还有说缓存的,还有说是ecplise的,反正我 ...