LoginSignup
2
1

More than 5 years have passed since last update.

Visual Studio | WPF > Window > マルチモニタ実行時にウィンドウのHeight, Widthを取得する

Last updated at Posted at 2017-12-25
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community

関連

処理

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);
        }
    }
}

実行例

qiita.png

異なる解像度を持つ複数のモニタに対してソフトを実装する場合は、いろいろ考えることはでてきそう。

同じ解像度を持つ複数のモニタに対しては、簡易な処理で対応できる。

link

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1