wpf wpf中的双显示器双窗口应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25091414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dual monitor dual window app in wpf
提问by D.K.
I'm trying to create a wpf video player with media element. My goal is making muliple windows for that application. each of the windows will show up on different monitors. Like the MainWindow will get the resolution of primary monitor and resize itself to go full screen. The Second window on secondary monitor and so...
我正在尝试创建一个带有媒体元素的 wpf 视频播放器。我的目标是为该应用程序制作多个窗口。每个窗口将显示在不同的显示器上。就像 MainWindow 将获得主显示器的分辨率并调整自身大小以全屏显示。辅助监视器上的第二个窗口等等...
So far, I've made the MainWindow fullscreen on the primary monitor. but I've no idea how to show the second window on second monitor with it's resolution. please help.
到目前为止,我已经在主监视器上使 MainWindow 全屏显示。但我不知道如何在第二台显示器上以它的分辨率显示第二个窗口。请帮忙。
回答by tkelch
Easiest option is to display it. And after the show() method, resize it.
最简单的选择是显示它。在 show() 方法之后,调整它的大小。
// display on second window
NewWindow win = new NewWindow();
System.Windows.Forms.Screen s1 = System.Windows.Forms.Screen.AllScreens[2];
System.Drawing.Rectangle r1 = s1.WorkingArea;
win.WindowState = System.Windows.WindowState.Normal;
win.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
win.Top = r1.Top;
win.Left = r1.Left;
win.Show();
win.WindowState = System.Windows.WindowState.Maximized;
回答by Loren Pechtel
Each screen has coordinates in space, you don't actually put a window on another screen, at the coordinates of the other screen(s).
每个屏幕都有空间坐标,您实际上并没有将窗口放在另一个屏幕上,位于其他屏幕的坐标处。
On this machine -1280,0 is the left monitor, 0,0 is the main monitor and 1280,0 is the right monitor. Programs with no knowledge of multi-screen operation generally have no problem with this, although old enough stuff can do some strange things when placed on the left monitor (trying to keep pop-ups from going off the edge of the screen, unaware that negative coordinates can be visible.)
在这台机器上,-1280,0 是左监视器,0,0 是主监视器,1280,0 是右监视器。对多屏操作一无所知的程序通常没有这个问题,尽管足够老的东西放在左边的显示器上会做一些奇怪的事情(试图阻止弹出窗口离开屏幕边缘,不知道负面影响)坐标可见。)
As for locating the screens, see: How to easily find screen location of form Location in multi-monitor environment?
至于定位屏幕,请参阅:如何在多显示器环境中轻松找到表单Location的屏幕位置?
回答by romanoza
You can try something like this:
你可以尝试这样的事情:
private System.Windows.Forms.Screen findScreen(string screenName) {
System.Windows.Forms.Screen res = System.Windows.Forms.Screen.AllScreens.FirstOrDefault(s => s.DeviceName == screenName);
if (res == null)
res = System.Windows.Forms.Screen.AllScreens[0];
return res;
}
private void setupForms() {
System.Windows.Forms.Screen mainScreen = findScreen(@"\.\DISPLAY2");
FrmMain frmMain = new FrmMain()
{
WindowStartupLocation = WindowStartupLocation.Manual,
WindowState = System.Windows.WindowState.Normal,
Left = mainScreen.WorkingArea.Left,
Top = mainScreen.WorkingArea.Top,
Width = mainScreen.WorkingArea.Width,
Height = mainScreen.WorkingArea.Height
};
System.Windows.Forms.Screen secondaryScreen = findScreen(@"\.\DISPLAY1");
FrmSecondary frmSecondary = new FrmSecondary()
{
WindowStartupLocation = WindowStartupLocation.Manual,
WindowState = System.Windows.WindowState.Normal,
Left = secondaryScreen.WorkingArea.Left,
Top = secondaryScreen.WorkingArea.Top,
Width = secondaryScreen.WorkingArea.Width,
Height = secondaryScreen.WorkingArea.Height
};
}
回答by Fred
I will update @romanoza's answer
我会更新@romanoza 的回答
private void SetupWindows()
{
System.Windows.Forms.Screen mainScreen = Screen.AllScreens[0]; ;
WinLeft leftWin = new WinLeft()
{
WindowStartupLocation = WindowStartupLocation.Manual,
WindowState = System.Windows.WindowState.Normal,
Left = mainScreen.WorkingArea.Left,
Top = mainScreen.WorkingArea.Top,
Width = mainScreen.WorkingArea.Width,
Height = mainScreen.WorkingArea.Height
};
System.Windows.Forms.Screen secondaryScreen = Screen.AllScreens[1];
WinRight rightWin = new WinRight()
{
WindowStartupLocation = WindowStartupLocation.Manual,
WindowState = System.Windows.WindowState.Normal,
Left = secondaryScreen.WorkingArea.Left,
Top = secondaryScreen.WorkingArea.Top,
Width = secondaryScreen.WorkingArea.Width,
Height = secondaryScreen.WorkingArea.Height
};
leftWin.Show();
leftWin.WindowState = WindowState.Maximized;
rightWin.Show();
rightWin.WindowState = WindowState.Maximized;
rightWin.Owner = leftWin;
}

