動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
関連
- c++ builder > form > モニタ解像度の取得 > Screen->Monitors[0]->Width; / ->Height; // Screen->PrimaryMonitor->
- c++ builder > Form > 4つのFormをモニタいっぱいに並べる
処理
WPFにてマルチモニタ実行時にウィンドウのHeightやWidthを得るにはどうするか。
StackOverflowのいくつかの回答ではWinFormsを使う例があるが、WPF使用時にWinFormsを使いたくはない。異なる設計思想のものを組合わせるとどこかで帳尻合わせをするためのおかしな実装を誘発するように思う。
WPFではSystemParametersにVirtualScreenがあるようだ。
https://stackoverflow.com/questions/2704887/is-there-a-wpf-equaivalent-to-system-windows-forms-screen
これをSystemParametersのPrimaryScreenHeightなどと合わせて使ってみた。
code
MainWindow.xaml
<Window x:Class="_171225_t1450_multiMonitors.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_171225_t1450_multiMonitors"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Name="B_monitors" Height="30" Content="Check Monitors"
Click="B_monitors_Click"/>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
namespace _171225_t1450_multiMonitors
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void B_monitors_Click(object sender, RoutedEventArgs e)
{
// Primary
var pheight = SystemParameters.PrimaryScreenHeight;
var pwidth = SystemParameters.PrimaryScreenWidth;
// Combined
var vheight = SystemParameters.VirtualScreenHeight;
var vwidth = SystemParameters.VirtualScreenWidth;
//
string msg = string.Format("Combined: {0}x{1}", vwidth, vheight);
msg += System.Environment.NewLine;
msg += string.Format("Primary: {0}x{1}", pwidth, pheight);
MessageBox.Show(msg);
}
}
}
実行例
異なる解像度を持つ複数のモニタに対してソフトを実装する場合は、いろいろ考えることはでてきそう。
同じ解像度を持つ複数のモニタに対しては、簡易な処理で対応できる。