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开源控件PhotoView的使用
整体来说,它是一个更高级的ImageView,支持缩放,多点触控缩放,滚动和滑动,单机,长按等事件: PhotoView的git托管地址:https://github.com/chrisbanes/P ...
- linux命令:文件属性
Linux 文件的属性主要包括:节点.种类.权限模式.链接数量.所归属的用户和用户组.文件大小.最近访问或修改的时间等内容. 命令: ls -lih 输出: [root@localhost test] ...
- C#在Dictionary中使用枚举作为键
Enum类型没有实现IEquatable接口,Dictionary中使用Enum作为键时,将发生装箱,使效率降低. 此时可用Dictionary中一个接收IEqualityComparer<T& ...
- Xml 序列化
1 XML序列化只能序列化对象的公有属性,并且要求对象有一个无参的构造方法,否者无法反序列化. 2 [Serializable]和[NonSerialized]特性对XML序列化无效!所以使用XML序 ...
- sqlite实现oracle的rownum功能
SELECT (SELECT COUNT(*) FROM [table] AS t2 WHERE t2.name <= t1.name) AS rowNum, id, name FROM [ta ...
- 日志:using the Connector/J connection property 'autoReconnect=true' to avoid this problem
com.mysql.jdbc.CommunicationsException: The last packet successfully received from the server was581 ...
- Redis中struct运用
c#操作缓存例如redis比较推荐ServiceStack 在redis中运用key-value存储数据,但是遇到结构体该如何处理,是类可通过get<type>(key)获得,那struc ...
- Deactivate .NET refector
8.5版本用注册机注册时手快成Standed版本,搞错......,能否Deactivated,发现要联网..... 接下来查找.net reflector 在位置%UserProfile%\AppD ...
- REDIS 字典数据结构
对于REDIS来讲 其实就是一个字典结构,key ---->value 就是一个典型的字典结构 [当然 对于vaule来讲的话,有不同的内存组织结构 这是后话] 试想一个这样的存储场景: ...
- B - I Hate It
#include<cstdio> #include<string.h> using namespace std; int ans; ; ]; struct Node{ int ...