显示带有 WPF、Winforms 和双显示器的窗口
我有 2 个显示器和一个启动 WPF 窗口的 WinForm 应用程序。 我想获取 WinForm 所在的屏幕,并在同一屏幕上显示 WPF 窗口。 我怎样才能做到这一点?
I have a 2 monitors and a WinForm app that launches a WPF window. I want to get the screen that the WinForm is on, and show the WPF window on the same screen. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是最简单的方法(使用 WindowStartupLocation.CenterOwner)。
无需互操作或设置窗口坐标:)
Here's the simplest way (uses WindowStartupLocation.CenterOwner).
No need for interop or setting window coords :)
另一种方法是:
this = your WPF Window...
Another way of doing it is:
this = your WPF Window...
您应该能够使用 System.Windows.Forms.Screen [1],并使用 FromControl 方法获取表单的屏幕信息。 然后,您可以使用它根据您尝试定位的屏幕来定位 WPF 窗口(顶部、左侧)。
[1] 如果您不加载 WinForms dll,您还可以使用 win32 MonitorFromRect 等。 但是,由于您已经获得了 winforms API,因此您不会付出任何内存/性能损失。
You should be able to use System.Windows.Forms.Screen [1], and use the FromControl method to get the screen info for the form. You can then use this for positioning the WPF window (top, left) based on the screen you are trying to locate it on.
[1] You can also use the win32 MonitorFromRect et al, if you don't to load the WinForms dlls. However, since you've already got the winforms API, you're not going to pay any memory/perf hit.
WPF 不包含方便的 System.Windows.Forms.Screen 类,但您仍然可以使用其属性在 WinForms 应用程序中完成任务。
假设 this 表示 WinForms 窗口,并且 _wpfWindow 是引用下面示例中的 WPF 窗口的已定义变量(这将位于您设置用于打开 WPF 窗口的任何代码处理程序中) ,像一些 Button.Click 处理程序):
上面的代码将在包含 WinForms 窗口的屏幕的左上角实例化 WPF 窗口。 如果您希望将其放置在其他位置(例如屏幕中间)或以“层叠”样式放置在 WinForms 窗口的下方和右侧,我将把数学留给您。
在屏幕中间获取 WPF 窗口的另一种方法是简单地使用
但是,这不太灵活,因为它使用鼠标的位置来确定显示 WPF 窗口的屏幕(显然鼠标如果用户快速移动它,或者您使用默认按钮,或者其他什么,则可能与您的 WinForms 应用程序位于不同的屏幕上)。
编辑: 这里是 SDK 文档的链接,介绍如何使用 InterOp 让 WPF 窗口在非 WPF 窗口上居中。 它基本上完成了我在计算数学方面所描述的内容,但正确地允许您使用窗口的 HWND 设置 WPF 窗口的“所有者”属性。
WPF doesn't include the handy System.Windows.Forms.Screen class, but you can still use its properties to accomplish your task in your WinForms application.
Assume that this means the WinForms window and _wpfWindow is a defined variable referencing the WPF Window in the example below (this would be in whatever code handler you set to open the WPF Window, like some Button.Click handler):
The above code will instantiate the WPF Window at the Top-Left corner of the screen containing your WinForms Window. I'll leave the math to you if you wish to place it in another location like the middle of the screen or in a "cascading" style below and to the right of your WinForms Window.
Another method that gets the WPF Window in the middle of the screen would be to simply use
However, this isn't quite as flexible because it uses the position of the mouse to figure out which screen to display the WPF Window (and obviously the mouse could be on a different screen as your WinForms app if the user moves it quickly, or you use a default button, or whatever).
Edit: Here's a link to an SDK document about using InterOp to get your WPF Window centered over the non-WPF Window. It does basically what I was describing in terms of figuring out the math, but correctly allows you to set the WPF Window's "Owner" property using the Window's HWND.