启动 WPF 可执行文件并设置父级?

发布于 2024-12-07 22:08:23 字数 205 浏览 7 评论 0原文

我有一个 WPF 可执行文件,我想要另一个程序来启动它,并将该启动程序设置为我的 WPF 可执行文件的父窗口(主要目的是当启动程序关闭时,WPF 可执行文件也会关闭)。我想我可以这样做:我将 Hwnd 作为一个命令行参数(作为整数字符串)传递,并且我可以调用 SetParent 或 WPF 可执行代码中的任何函数来指定父级。但是,我无法让它发挥作用。谁能告诉我该怎么做,或者有其他方法吗?谢谢!

I have a WPF executable and I want another program to launch it and set this launching program as the parent windows of my WPF executable (The main purpose is that when the launching program is closed the WPF executable is also shut down). I thought I could make it like this: I pass the Hwnd as one command line parameter (as integer-string), and I can call the SetParent or whatever function inside my WPF executable code to specify the parent. However, I can't make it work. Can anybody tell me how to do that, or any other way to do that? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如何视而不见 2024-12-14 22:08:23

你不能。窗口句柄是每个进程的。

此外,你也不会愿意。在另一个线程中拥有一个父窗口已经足够有问题了——这会导致两个线程的消息队列被附加,即,从那时起它们实际上共享同一个消息队列。因此,现在如果任一线程锁定,或者进行一些冗长的处理,两个线程都会被冻结。 (据我所知,以后无法分离消息队列。)想象一下尝试扩展此跨进程。

如果您必须启动一些新代码并使用现有窗口作为父窗口,则无法跨进程。您必须将 WPF 代码加载到进程中并调用其中的方法,并将父窗口作为参数传递。将该代码加载到进程中的最简单方法是将 WPF 应用程序更改为类库 (.dll),然后添加对该 .dll 的引用,或者使用反射动态加载它。

You can't. Window handles are per-process.

Besides, you wouldn't want to. It's problematic enough to have a parent window in another thread -- that causes the message queues of the two threads to become attached, i.e., they effetively share the same message queue from then on. So now if either thread locks up, or does some lengthy processing, both threads are frozen. (And there's no way to later detach the message queues, as far as I'm aware.) Imagine trying to extend this cross-process.

If you must start some new code and use an existing window as a parent, you can't go cross-process. You would have to load the WPF code into your process and call a method in it, passing your parent window as a parameter. The simplest way to load that code into your process would be to change your WPF application to a class library (.dll), and either add a reference to that .dll, or load it dynamically using Reflection.

任性一次 2024-12-14 22:08:23

正如@Joe White所说,你无法立即实现这一点...我想我可以“猜测”你要得到什么...

你可能有一个WinForm MDI父级(它已经是exe)并且你想启动另一个WPF窗口(另一个 exe)作为其子项。我说得对吗?

嗯,那么您必须创建一个新的 WinForm 子窗口,其中包含 WinFormWPFHostApp,然后将 WPF 程序集引用到该项目并尝试托管 ContentMainWindow 的 code>。

请参阅这篇文章...

As @Joe White said you cannot achieve this straightaway ... I think I can "guess" what you are getting at ....

You probably have a WinForm MDI parent (its exe already) and you want to launch another WPF window (another exe) as its child. Am I correct?

Hmmmm then you would have to create a new WinForm child window with WinFormWPFHostApp in it and then refer the WPF assemblies to this project and try to host the Content of the MainWindow from that other WPF application.

refer this article...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文