外部exe窗体嵌入winform
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing.Design; namespace HCDL
{
public class ShowForm
{
//-------------------
//Action<object, EventArgs> appIdleAction = null;
EventHandler appIdleEvent = null; Control ParentCon = null; string strGUID = ""; public ShowForm(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;
}
if (m_AppProcess.MainWindowHandle == IntPtr.Zero) return;
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 == )
{
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 = (-);
private const int WS_VISIBLE = 0x10000000;
private const int WM_CLOSE = 0x10;
private const int WS_CHILD = 0x40000000; private const int SW_HIDE = ; //{隐藏, 并且任务栏也没有最小化图标}
private const int SW_SHOWNORMAL = ; //{用最近的大小和位置显示, 激活}
private const int SW_NORMAL = ; //{同 SW_SHOWNORMAL}
private const int SW_SHOWMINIMIZED = ; //{最小化, 激活}
private const int SW_SHOWMAXIMIZED = ; //{最大化, 激活}
private const int SW_MAXIMIZE = ; //{同 SW_SHOWMAXIMIZED}
private const int SW_SHOWNOACTIVATE = ; //{用最近的大小和位置显示, 不激活}
private const int SW_SHOW = ; //{同 SW_SHOWNORMAL}
private const int SW_MINIMIZE = ; //{最小化, 不激活}
private const int SW_SHOWMINNOACTIVE = ; //{同 SW_MINIMIZE}
private const int SW_SHOWNA = ; //{同 SW_SHOWNOACTIVATE}
private const int SW_RESTORE = ; //{同 SW_SHOWNORMAL}
private const int SW_SHOWDEFAULT = ; //{同 SW_SHOWNORMAL}
private const int SW_MAX = ; //{同 SW_SHOWNORMAL} #endregion Win32 API /// <summary>
/// 将指定的程序嵌入指定的控件
/// </summary>
private void EmbedProcess(Process app, Control control)
{
// Get the main handle
if (app == null || app.MainWindowHandle == IntPtr.Zero || control == null) return;
try
{
// Put it into this form
SetParent(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 window
MoveWindow(app.MainWindowHandle, -, -, 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;
}
}
外部exe窗体嵌入winform
外部exe窗体嵌入winform的更多相关文章
- C# 将外部exe程序 嵌入到自己的窗体界面
将别人开发的exe程序,放到自己的窗体里面来运行. 1.基本功能实现 首先,在自己的窗体后面加上代码: [DllImport("User32.dll", EntryPoint = ...
- exe程序嵌入Winform窗体
1.新建winform程序,添加一个Panel控件和一个button控件,winform窗体命名为:Mainform: 2.新建一个类文件,方便引用,命名为:exetowinform: 3.Mainf ...
- wpf里窗体嵌入winform控件被覆盖问题
问题1:嵌套Winform控件(ZedGraph)在WPF的ScrollViewer控件上,出现滚动条,无论如何设置该Winform控件都在顶层,滚动滚动条会覆盖其他WPF控件. 解决办法:在Sc ...
- C# 以嵌入到窗体的方式打开外部exe
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using S ...
- C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部【转载】
这是最近在做的一个项目中提到的需求,把一个现有的窗体应用程序界面嵌入到自己开发的窗体中来,看起来就像自己开发的一样(实际上……跟自己开发的还是有一点点区别的,就是内嵌程序和宿主程序的窗口激活状态问题) ...
- 【转】C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部
PS:文末的附件已更新,这次我放到博客园里面了,不会弹出广告,放心下载,O(∩_∩)O谢谢! 这是最近在做的一个项目中提到的需求,把一个现有的窗体应用程序界面嵌入到自己开发的窗体中来,看起来就像自己开 ...
- C# winform启动外部exe后,如何完全阻断父界面接收事件,扩展waitforexit
公司的系统搭载了好多奇奇怪怪的exe,以前启动exe后,系统还能接着操作.但是后面又提出额外的需求,说是打开外部exe之后,启动exe的父界面要完全不能进行任何操作.当然按常人所想再加一句waitfo ...
- C# ASP.NET Webservice调用外部exe无效的解决方法
最近用asp.net做webservice,其中有个功能是调用执行外部的exe(类似cmd中执行),但执行Process.Start之后就没有结果,同样代码在winform下正常,折腾两天终于找到解决 ...
- 如何用DELPHI编程修改外部EXE文件的版本信
右击里面有修改 点开直接修改就可以了吧. DELPHI 里程序的版本信息怎么是灰色的,无法更改 耐心读以下说明,应该能解决你的问题,如果不能解决,请Hi我~ 如何给自己的dll文件添加版本信息呢? 首 ...
随机推荐
- iOS afnetworking最新版报错 没有AFHTTPRequestOperationManager类了
今天开了一个小项目 用的是pod 然后 安装好 Afnetworking之后 发现 AFHTTPRequestOperationManager 这个类没有了 ,百度之后 发现 原来 ...
- Oracle 10g 默认安装带来的用户名/密码
ORACLE数据库创建的时候,创建了一系列默认的用户和表空间 Oracle 10g 默认安装带来的用户名/密码 Username Password Description See Also CTXSY ...
- 线段树(单点更新)HDU1166、HDU1742
在上一篇博文里面,我提到了我不会线段树,现在就努力地学习啊! 今天AC一题感觉都很累,可能是状态不佳,在做HDU1166这题目时候,RE了无数次. 原因是:我的宏定义写错了,我已经不是第一犯这种错误了 ...
- main(int argc , char *argv[])
#include <unistd.h>#include <stdlib.h>#include <stdio.h> int main(int argc, char * ...
- PHP curl 采集内容之规则 及图片下载方法2
<?phpheader("Content-type:text/html; charset=utf-8");/*$pattern = '/xxx(.*)yyyy/isU'; / ...
- PHP — php精粹-编写高效的php代码 --- API
1.数据格式 (1)json 示例代码: $jsonData = '[{"title":"The Magic Flute","time":1 ...
- 数位dp入门 hdu2089 不要62
数位dp入门 hdu2089 不要62 题意: 给定一个区间[n,m] (0< n ≤ m<1000000),找出不含4和'62'的数的个数 (ps:开始以为直接暴力可以..貌似可以,但是 ...
- Django 数据库查询优化
Django数据层提供各种途径优化数据的访问,一个项目大量优化工作一般是放在后期来做,早期的优化是“万恶之源”,这是前人总结的经验,不无道理.如果事先理解Django的优化技巧,开发过程中稍稍留意,后 ...
- bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形 容斥
1914: [Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 272 Sol ...
- Google DNS劫持背后的技术分析
0×00 背景 最近世界真是越来越不太平了,尤其是对于大部分普通人而言.昨天又传来噩耗,根据网络监测公司BGPMon,Google的公开DNS服务器 IP 8.8.8.8被劫持到了委内瑞拉和巴西超过2 ...