作为 MDI 子窗口的程序
有没有办法将程序用作 MDI 子窗口。我正在考虑拥有一个主 MDI 父窗口,它可以有多个子窗口,其中一些子窗口本身就是程序(.exe 文件)。
蒂姆
Is there a way to use a program as a MDI child window. I am thinking of having one main MDI parent window which can have multipe child windows, some of which will be programs(.exe files) in there own right.
Tim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上有一个非常简单的方法可以做到这一点。
首先,您需要向表单添加一个面板。该面板将用于“托管”应用程序。
接下来,您需要将“System.Runtime.InteropServices”和“System.Diagnostics”命名空间添加到您的命名空间中:
csharp
现在,我们需要设置 WinAPI 函数:
现在,在按钮单击事件内,启动进程,并将其父级设置为面板。在此示例中,我将使用记事本:
There is actually a pretty easy way to do this.
First, you need to add a panel to your form. This panel will be used to "host" the application.
Next, you need to the "System.Runtime.InteropServices" and the "System.Diagnostics" namespace to your namespaces:
csharp
Now, we need setup our WinAPI functions:
Now, inside a button click event, start the process, and set it's parent to the panel. In this example, I will be using notepad:
几年前我已经实现了类似的事情(基于 .NET Framework 1.1,如果我没记错的话)。该实现的关键要素是:
Form
类,它公开了一些特定的功能,例如用于提取将调用 UI 的用户命令的接口。Activator.CreateInstance
),从表单边框中剥离并嵌入到容器中(在我们的例子中是TabControl 中的
,在您的情况下,您的应用程序中很可能是“空”MDI 子表单)。TabPage
我认为这一切都进展得相当好(实际上我认为该框架仍在其创建的公司内维护和使用)。
您可能需要关注内存管理。例如,由于无法卸载程序集,因此如果有此要求,您将需要将外部程序集加载到单独的 AppDomain 中。还要注意加载子窗口 UI 时动态附加的任何事件处理程序,以便在卸载 UI 时正确分离它们。
I have implemented a similar thing a few years ago (.NET Framework 1.1 based, if I recall correctly). Key elements of that implementation were:
Form
class that exposed some specific functionality, such as an interface for extracting user commands that would invoke the UI.Activator.CreateInstance
), stripped from form borders and embedded into a container (in our case aTabPage
in aTabControl
, in your case most likely an "empty" MDI Child form in your application).This all worked out fairly well I think (I actually think that the framework is still being maintained and used within the company it was created for).
You may want to keep an eye at memory management. For instance, since an assembly cannot be unloaded, if that is a requirement you will need to load external assemblies into separate AppDomains. Also pay attention to any event handlers that are attached dynamically when child window UI's are loaded, so that they are properly detached when the UI's are unloaded.
导入
InteropServices
和Threading
命名空间从
user32.dll
导入SetParent
创建一个新进程并使其成为 MDI 子进程使用
SetParent
修改我们的表单Import the
InteropServices
andThreading
namespacesImport
SetParent
fromuser32.dll
Create a new process and make it an MDI child of our form using
SetParent