显示与WPF,Winforms中,和双显示器的窗口 [英] Showing a window with WPF, Winforms, and Dual monitors

查看:808
本文介绍了显示与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?

推荐答案

WPF不包括方便System.Windows.Forms的。屏幕类,但你仍然可以使用它的属性来完成你的任务,你的WinForms应用程序。

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.

假设这个指的WinForms窗口 _wpfWindow 被定义的变量在下面的示例引用WPF窗口(这将是任何代码处理程序设置为打开WPF窗口,像一些Button.Click处理程序):

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):

Screen screen = Screen.FromControl(this);
_wpfWindow.StartupLocation = System.Windows.WindowStartupLocation.Manual;
_wpfWindow.Top = screen.Bounds.Top;
_wpfWindow.Left = screen.Bounds.Left;
_wpfWindow.Show();



上面的代码将实例化WPF窗口在包含您的WinForms窗口在屏幕的左上角。如果你希望把它放在像屏幕或者在下面和你的WinForms窗口右侧的级联的风格,中间的其他位置我将离开算算你。

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.

这得到WPF窗口在屏幕中间的另一种方法是简单地用

Another method that gets the WPF Window in the middle of the screen would be to simply use

_wpfWIndow.StartupLocation = System.Windows.WindowStartupLocation.CenterScreen

不过,这是因为它使用的是不是很灵活鼠标的位置搞清楚显示WPF窗口,屏幕(显然,如果用户快速移动它,或者你使用一个默认的按钮,或任何鼠标可能是一个不同的屏幕为您的WinForms应用程序上)。

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).

编辑:的下面是一个SDK文档有关使用互操作,让您的WPF窗口集中在非WPF窗口中的链接。它基本上是我搞清楚数学的角度描述了,但正确允许您使用窗口的HWND设置WPF窗口的所有者属性。

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.

这篇关于显示与WPF,Winforms中,和双显示器的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆