• Stars
    star
    164
  • Rank 229,056 (Top 5 %)
  • Language
    C#
  • Created over 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Embed an exe application into the panel control. make it feels like you own this exe application.

AppContainer

Embed an exe application into the panel control. make it feels like you own this exe application. http://www.cnblogs.com/bitzhuwei/archive/2012/05/24/SmileWei_EmbeddedApp.html

这是最近在做的一个项目中提到的需求,把一个现有的窗体应用程序界面嵌入到自己开发的窗体中来,看起来就像自己开发的一样(实际上……跟自己开发的还是有一点点区别的,就是内嵌程序和宿主程序的窗口激活状态问题)。

在codeproject找到了一篇相关的文章(http://www.codeproject.com/Articles/9123/Hosting-EXE-Applications-in-a-WinForm-project),虽然可用,但是很不方便,于是重新设计编写了一个类库,用一个控件完成内嵌其它应用程序的功能。

直接上图先:

 嵌入QQ影音 嵌入Windows Live Writer

嵌入photoshop 嵌入Adobe Reader

从打开Adobe Reader那张图片可以看出来所谓的“内嵌程序和宿主程序的窗口激活状态问题”。当内嵌程序窗口激活时,表面上将其包裹起来的宿主窗口却处于非激活的状态。想隐藏这一点的话,把窗口的FormBorderStyle属性设为None吧,然后自己在窗口上画关闭、最大化、最小化按钮好了。

 

原作者的实现思路更能暴露本质,所以这里用原作者的代码段解释一下实现过程。

1、启动要嵌入的应用程序进程

 1 Process p = null; 
 2 try 
 3 {
 4   // Start the process 
 5   p = System.Diagnostics.Process.Start(this.exeName); 
 6 
 7   // Wait for process to be created and enter idle condition 
 8   p.WaitForInputIdle(); 
 9 
10   // Get the main handle
11   appWin = p.MainWindowHandle; 
12 } 
13 catch (Exception ex) 
14 { 
15   MessageBox.Show(this, ex.Message, "Error"); 
16 }

2、调用Windows API将启动的应用程序窗口嵌入自定义的控件(作者用的是Panel控件)

1 // Put it into this form
2 SetParent(appWin, this.Handle);//this在这里是Panel控件

3 4 // Remove border and whatnot 5 SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE); 6 7 // Move the window to overlay it on this window 8 MoveWindow(appWin, 0, 0, this.Width, this.Height, true);

3、设置被嵌入的窗体大小随宿主窗体改变

1 protected override void OnResize(EventArgs e)
2 {
3   if (this.appWin != IntPtr.Zero)
4   {
5     MoveWindow(appWin, 0, 0, this.Width, this.Height, true);
6   }
7   base.OnResize (e);
8 }

4、设置被嵌入的窗体应用程序在宿主程序关闭时也关闭

 1 protected override void OnHandleDestroyed(EventArgs e)
 2 {
 3   // Stop the application
 4   if (appWin != IntPtr.Zero)
 5   {
 6     // Post a colse message
 7     PostMessage(appWin, WM_CLOSE, 0, 0);
 8 
 9     // Delay for it to get the message
10     System.Threading.Thread.Sleep(1000);
11 
12     // Clear internal handle
13     appWin = IntPtr.Zero;
14   }
15   base.OnHandleDestroyed (e);
16 }

 

原作者的代码实际用起来是很不方便的,具体大家试试就知道,不细说了(反正我只学了学上面的步骤,也不用他的库)。

本人开发了一个比较实用的控件,使用起来也很简单,只需三步。

首先,在窗体应用程序项目中引用类库SmileWei.EmbeddedApp。

在窗体应用程序项目中引用类库SmileWei.EmbeddedApp

然后,在宿主窗体上拖一个AppContainer控件,摆放好位置。(如果工具箱里没有AppContainer,就F6生成解决方案一下,然后再看就有了。)

在宿主窗体上拖一个AppContainer控件,摆放好位置

最后,告诉AppContainer控件,要嵌入的应用程序(*.exe文件)的绝对路径(本人以使用OpenFileDialog为例),命令AppContainer控件启动之。

1 appContainer1.AppFilename = openEXE.FileName;
2 appContainer1.Start();

这个AppContainer控件有什么好处呢?

1、原作者想到的Resize和随宿主程序关闭而关闭的问题,AppContainer都实现了。

2、AppContainer指定要嵌入的应用程序和启动是分开的,这样更灵活,开发过程中也不会看到如下的情况了:开发的时候原作者的控件就“情不自禁”地把内嵌程序加载进来了。

 开发的时候原作者的控件就“情不自禁”地把内嵌程序加载进来了

3、AppContainer防范了各种可能出错的情形,例如禁止自己嵌入自己(死循环)、内嵌Console程序时提示不能嵌入、参数为null或无效的检验等。

4、其它。例如,AppContainer里面不会使用Thread.Sleep(1000);这样低端的句子来保证程序正确地嵌入(而且对于类似photoshop这样启动很慢的程序也保证不了),而是通过Application.Ilde事件实现了在被嵌程序加载完毕后才将其窗体嵌入的技巧。

当然,有些应用程序是不能这么自动化地嵌入进来的。因为程序启动窗体和主窗体句柄不一样,AppContainer无法获得主窗体句柄,所以无法自动嵌入。

为了解决这个问题,我在宿主窗体的状态栏上设置了“句柄嵌入”标签,点击“句柄嵌入”,你可以填入想嵌入的应用程序主窗体句柄,然后宿主窗体就可以嵌入它了。

我在宿主窗体的状态栏上设置了“句柄嵌入”标签,点击“句柄嵌入”,你可以填入想嵌入的应用程序主窗体句柄,然后宿主窗体就可以嵌入它了

然后有同学就问了,我怎么知道想要嵌入的窗体句柄是多少啊?方法很多啦,我这里也提供一个自己制作的小程序,大家可以在这里下载:WindowDetective(窗口侦探)0.20.rar

界面是这个样子的:

我的WindowDetective界面是这个样子的

里面“句柄:{1903014}”那一行就给出了本人正在用的Windows Live Writer的主窗体句柄。

用法很简单,启动这个程序后,它会自动检测鼠标所在位置的窗体信息,显示在窗口中。所以把鼠标放在你想了解的窗体菜单栏上就OK了。QQ TM版也可以这样嵌进来滴。(QQ嵌不进来,不知道腾讯在搞什么)

QQ TM版也可以这样嵌进来

大家还可以试试把QQ对话框嵌进来,很好玩哦~

 大家还可以试试把QQ对话框嵌进来,很好玩哦~

 

More Repositories

1

CSharpGL

🍏Object Oriented OpenGL in C#.
C#
564
star
2

SharpFileDB

SharpFileDB is a micro database library that uses no SQL, files as storage form and totally C# to implement a CRUD system. SharpFileDB是一个纯C#的无SQL的支持CRUD的小型文件数据库,目标是支持万人级别的应用程序。
C#
63
star
3

Capital

研究《资本论》
43
star
4

Kitware.VTK

self use experimental C# wrapper for VTK.
C#
27
star
5

TankHero-2D

a 2d tank game written in Unity
C#
22
star
6

LALR1Compiler

this is obsolete. please go to https://github.com/bitzhuwei/GrammarMentor
C#
18
star
7

GLSLHighlight

This is a GLSL4.3 syntax highlighter extension to the Visual Studio.
C#
9
star
8

SoftGL

Implementation of OpenGL(only a part) in pure C#.
C#
7
star
9

CSharpGL2

此项目为支持CSharpGL而存在。 此项目下存放一些不常用的文档、资料、代码等。
C#
7
star
10

SortingExtension

集成了几种常用排序算法。
C#
6
star
11

Vulkan.net

Vulkan in C#.
C#
5
star
12

FileWiper

Wipe files' content and then delete files from your computer
C#
5
star
13

CGCompiler

Context-free Grammar Compiler is a compiler-compiler in C# that generates lexical analyzer and syntax parser automatically according to the grammar you defined.
C#
5
star
14

3DSParser

3DS文件解析器和浏览器
C#
4
star
15

GrammarMentor

lex+yacc from scratch. 从零实现Lex+YACC的功能:一键生成词法分析器和语法分析器。 多图易上手。
C#
4
star
16

ClipboardImageSaver

Automatically save image that comes from 'print screen' key to specified folder.
C#
3
star
17

ColorVertexSample

Demo on displaying huge amount of particles and 2D UI using SharpGL(C#+OpenGL)
C#
2
star
18

CPA.ProfessionalLevel

C#
1
star
19

bitzhuwei.github.io

be curious
HTML
1
star
20

JunoirAccountant.Practice2020

初级会计实务2020
1
star
21

HowHeapSorttWorks

Show how heap sort works with unity.
C#
1
star
22

CSharpGL.CSSL

CSharp Shading Language(A C# version of OpenGL GLSL shading language).
C#
1
star
23

xiayidao

侠义道挂机程序
C#
1
star
24

CSharpGL.Data

data files referenced in https://github.com/bitzhuwei/CSharpGL
1
star