Winform嵌入其它应用程序
Options:
using CommandLine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SunCreate.CombatPlatform.Client
{
public class Options
{
[Option("h", "handle", Required = true)]
public int Handle { get; set; } [Option("b", "browser")]
public Boolean IsBrowser { get; set; }
} }
ApplicationHost:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms; namespace SunCreate.CombatPlatform.Client
{
/// <summary>
///
/// </summary>
public class ApplicationHost : UserControl
{
#region PInvoke
[DllImport("user32.dll", SetLastError = true)]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [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 = "SetWindowLongA", SetLastError = true)]
private static extern long SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);
#endregion #region Const
private const int GWL_STYLE = -;
private const int WS_VISIBLE = 0x10000000;
#endregion #region Var
private Boolean _autoLoadProcess = true;
private Process _process;
private string _file;
private string _arguments;
#endregion #region Private Property
/// <summary>
/// Gets or sets the m_ process.
/// </summary>
/// <value>
/// The m_ process.
/// </value>
private Process m_Process
{
get
{
return _process;
}
set
{
if (_process == value)
return; if (value == null)
UnloadProcess(); _process = value;
}
}
#endregion #region Public Property
/// <summary>
/// Gets or sets the auto load process.
/// </summary>
/// <value>
/// The auto load process.
/// </value>
public Boolean AutoLoadProcess
{
get
{
return _autoLoadProcess;
}
set
{
_autoLoadProcess = value;
}
} /// <summary>
/// Gets or sets the hide application title bar.
/// </summary>
/// <value>
/// The hide application title bar.
/// </value>
public Boolean HideApplicationTitleBar { get; set; } /// <summary>
/// Gets or sets the file.
/// </summary>
/// <value>
/// The file.
/// </value>
public string File
{
get
{
return _file ?? string.Empty;
}
set
{
_file = value;
}
} /// <summary>
/// Gets or sets the arguments.
/// </summary>
/// <value>
/// The arguments.
/// </value>
public string Arguments
{
get
{
return _arguments ?? string.Empty;
}
set
{
_arguments = value;
}
} /// <summary>
/// Gets the main window handle.
/// </summary>
/// <value>
/// The main window handle.
/// </value>
public IntPtr MainWindowHandle
{
get
{
return m_Process == null ? IntPtr.Zero : m_Process.MainWindowHandle;
}
} /// <summary>
/// Gets the main window title.
/// </summary>
/// <value>
/// The main window title.
/// </value>
public string MainWindowTitle
{
get
{
return m_Process == null ? string.Empty : m_Process.MainWindowTitle;
}
}
#endregion #region Constructor & DeConstructor
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationHost" /> class.
/// </summary>
public ApplicationHost()
{
this.Load += ApplicationHost_Load;
this.ProcessLoaded += ApplicationHost_ProcessLoaded;
this.ProcessUnLoaded += ApplicationHost_ProcessUnLoaded;
} /// <summary>
/// Finalizes an instance of the <see cref="ApplicationHost" /> class.
/// </summary>
~ApplicationHost()
{
m_Process = null;
}
#endregion #region Event
public event EventHandler ProcessLoaded;
public event EventHandler ProcessUnLoaded;
#endregion #region Protected Method
/// <summary>
/// </summary>
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
m_Process = null;
}
base.Dispose(disposing);
} /// <summary>
/// Raises the <see cref="E:ProcessLoaded" /> event.
/// </summary>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
protected void OnProcessLoaded(EventArgs e)
{
if (ProcessLoaded == null)
return;
ProcessLoaded(this, e);
} /// <summary>
/// Raises the <see cref="E:ProcessUnLoaded" /> event.
/// </summary>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
protected void OnProcessUnLoaded(EventArgs e)
{
if (ProcessUnLoaded == null)
return;
ProcessUnLoaded(this, e);
}
#endregion #region Public Method
/// <summary>
/// Loads the process.
/// </summary>
public void LoadProcess()
{
if (m_Process != null)
{
var startInfo = m_Process.StartInfo;
if (startInfo.FileName != this.File || startInfo.Arguments != this.Arguments)
m_Process = null;
else
return;
} m_Process = new Process()
{
SynchronizingObject = this,
StartInfo = new ProcessStartInfo()
{
FileName = File,
Arguments = this.Arguments
}
}; m_Process.Start(); m_Process.WaitForInputIdle();
while (!m_Process.HasExited && m_Process.MainWindowHandle == IntPtr.Zero)
{
Application.DoEvents();
Thread.Sleep();
} m_Process.EnableRaisingEvents = true; m_Process.Exited += m_Process_Exited; var handle = m_Process.MainWindowHandle; if (HideApplicationTitleBar)
SetWindowLong(handle, GWL_STYLE, WS_VISIBLE); SetParent(handle, this.Handle); MoveWindow(handle, , , this.Width, this.Height, true); OnProcessLoaded(EventArgs.Empty);
} /// <summary>
/// Unloads the process.
/// </summary>
public void UnloadProcess()
{
if (m_Process == null)
return; if (m_Process.HasExited)
return; m_Process.CloseMainWindow();
m_Process.WaitForExit(); if (m_Process != null && !m_Process.HasExited)
m_Process.Kill(); OnProcessUnLoaded(EventArgs.Empty);
} /// <summary>
/// Reloads the process.
/// </summary>
public void ReloadProcess()
{
UnloadProcess();
LoadProcess();
}
#endregion #region Event Process
/// <summary>
/// Handles the Load event of the ApplicationHost control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
void ApplicationHost_Load(object sender, EventArgs e)
{
if (Process.GetCurrentProcess().ProcessName.Equals("devenv", StringComparison.CurrentCultureIgnoreCase))
return; if (AutoLoadProcess)
LoadProcess();
} /// <summary>
/// Handles the Resize event of the ApplicationHost control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
void ApplicationHost_Resize(object sender, EventArgs e)
{
var handle = m_Process.MainWindowHandle; if (handle != IntPtr.Zero)
MoveWindow(handle, , , this.Width, this.Height, true);
} /// <summary>
/// Handles the ProcessLoaded event of the ApplicationHost control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
void ApplicationHost_ProcessLoaded(object sender, EventArgs e)
{
this.Resize += ApplicationHost_Resize;
} /// <summary>
/// Handles the ProcessUnLoaded event of the ApplicationHost control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
void ApplicationHost_ProcessUnLoaded(object sender, EventArgs e)
{
this.Resize -= ApplicationHost_Resize;
} /// <summary>
/// Handles the Exited event of the m_Process control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
void m_Process_Exited(object sender, EventArgs e)
{
m_Process = null; OnProcessUnLoaded(EventArgs.Empty);
}
#endregion
}
}
代码:
private void ShowBrowser(string url)
{
if (dkPnl.Children.Count > )
{
WindowsFormsHost whst = dkPnl.Children[] as WindowsFormsHost;
whst.Dispose();
foreach (Process p in Process.GetProcessesByName("MyBrowser"))
{
p.Kill();
}
} var host = new ApplicationHost()
{
File = @"MyBrowser.exe",
Arguments = string.Empty,
HideApplicationTitleBar = true,
Dock = System.Windows.Forms.DockStyle.Fill,
BorderStyle = System.Windows.Forms.BorderStyle.None
};
host.ProcessLoaded += host_ProcessLoaded;
host.ProcessUnLoaded += host_ProcessUnLoaded; WindowsFormsHost windowsFormsHost = new WindowsFormsHost();
windowsFormsHost.Child = host;
dkPnl.Children.Add(windowsFormsHost);
} private Dictionary<IntPtr, ApplicationHost> _hostPool;
private Dictionary<IntPtr, ApplicationHost> m_HostPool
{
get
{
return _hostPool ?? (_hostPool = new Dictionary<IntPtr, ApplicationHost>());
}
} void host_ProcessLoaded(object sender, EventArgs e)
{
var host = sender as ApplicationHost;
m_HostPool.Add(host.MainWindowHandle, host);
} void host_ProcessUnLoaded(object sender, EventArgs e)
{
var host = sender as ApplicationHost; var parent = host.Parent;
if (parent != null && !parent.IsDisposed)
{
parent.Dispose();
}
}
Winform嵌入其它应用程序的更多相关文章
- 在winform嵌入外部应用程序
应朋友要求,需要将一个第三方应用程序嵌入到本程序WinForm窗口,以前在VB6时代做过类似的功能,其原理就是利用Windows API中FindWindow函数找到第三方应用程序句柄,再利用SetP ...
- C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部【转载】
这是最近在做的一个项目中提到的需求,把一个现有的窗体应用程序界面嵌入到自己开发的窗体中来,看起来就像自己开发的一样(实际上……跟自己开发的还是有一点点区别的,就是内嵌程序和宿主程序的窗口激活状态问题) ...
- 【转】C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部
PS:文末的附件已更新,这次我放到博客园里面了,不会弹出广告,放心下载,O(∩_∩)O谢谢! 这是最近在做的一个项目中提到的需求,把一个现有的窗体应用程序界面嵌入到自己开发的窗体中来,看起来就像自己开 ...
- C# winform嵌入unity3D
最近做项目需要winform嵌入unity的功能,由于完全没接触过这类嵌入的于是在网上搜,有一种方法是UnityWebPlayer插件,也开始琢磨了一段时间,不过一会发现在5.4版本以后这个东西就被淘 ...
- 把任意的EXE嵌入到自己程序中
把任意的EXE嵌入到自己程序中 taoyuan19822008-08-24上传 Delphi把任意的EXE嵌入到自己程序中的程序 资源积分:0分 下载次数:327 资源类型:其他 资源大小:175 ...
- Qt界面中嵌入其他exe程序的界面,使用Qt5
下面用一个小例子来演示如何在Qt的界面中嵌入其他exe程序的界面,最终效果如下图所示.本文参考了 http://blog.csdn.net/jiaoyaziyang/article/details/4 ...
- WPF中嵌入普通Win32程序的方法
公司现在在研发基于.Net中WPF技术的产品,由于要兼容旧有产品,比如一些旧有的Win32程序.第三方的Win32程序等等,还要实现自动登录这些外部Win32程序,因此必须能够将这些程序整合到我们的系 ...
- SNF开发平台WinForm之八-自动升级程序部署使用说明-SNF快速开发平台3.3-Spring.Net.Framework
9.1运行效果: 9.2开发实现: 1.首先配置服务器端,把“SNFAutoUpdate2.0\服务器端部署“目录按网站程序进行发布到IIS服务器上. 2.粘贴语句,生成程序 需要调用的应用程序的Lo ...
- WinForm之窗体应用程序
WinForm之窗体应用程序 基本简单数据库操作(增删改查) using System; using System.Collections.Generic; using System.Windows. ...
随机推荐
- OpenFeign使用笔记
是什么 Feign是一个声明式Web Service客户端.使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注 ...
- jenkins显示html样式问题的几种解决方案总结
前言 jenkins上使用HTML Publisher plugin插件生成的html报告样式会丢失,需要设置下才能正常显示. 一.样式丢失 1.官方文档的解释如下,参考地址https://stack ...
- 检测Python程序的执行效率
无意中被问到代码执行效率的问题,那就总结一下检测代码执行效率的几种方式: 一.装饰器 在函数上加装饰器,来得到函数的执行时间. def cst_time(func, *args, **kwargs): ...
- uva-110-没有for循环的排序
题意:看输出就懂了,暴力枚举题,字符串最大长度是8,所有长度等于8的长度是8!=1x2x3x4x5x6x7x8=40320,数据量比较小的.只是枚举的方向比较怪异,如下,长度等于3的串 a ab,ba ...
- 通过 Lua 扩展 NGINX 实现的可伸缩的 Web 平台OpenResty®
OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高的动态 W ...
- ES6系列_9之对象
1.对象赋值 es5中的对象赋值方式如下: let name="小明"; let skill= 'es6开发'; var obj= {name:name,skill:skill}; ...
- 通过PicturreId获取图片路径(Url)
1.直接使用接口服务 _pictureService.GetPictureUrl((int)entity.SponsorPictureId); //entity是具体查询出来的实体对象 Sponsor ...
- 基础 ByteBuffer 和 ByteBuf
缓冲区 ByteBuffer buffer = ByteBuffer.allocate(); ByteBuf https://www.jianshu.com/p/3fbf54b8e8ec
- C++如何实现DNS域名解析<转>
C++如何实现DNS域名解析 这片文章介绍了C++如何实现DNS域名解析,还有对相关技术的介绍,代码很详细,需要的朋友可以参考下 一.概述 现在来搞定DNS域名解析,其实这是前面一篇文章C++实现 ...
- Graphics.Blit
[Graphics.Blit] 需求注意第4个参数,用4个参数pass用于指定使用哪一个pass.默认值为-1,即使用所有的pass. 参考:file:///C:/Program%20Files%20 ...