2014/07/21

Per-Monitor DPIのビヘイビアによる実装

WPFでPer-Monitor DPI対応を実装する方法として、これを実装したWindowから継承するやり方を取りましたが、標準のWindowにPer-Monitor DPI対応を付加する方法もあります。

その一つとしてespresso3389さんが添付プロパティを使ったやり方を示されています。
これで基本的な用は足りるわけですが、ウィンドウ内の構成によってはDPIの値等を直接取得して処理したい場合もあります(例えば、画像をDPIに応じて切り替える)。

これを簡単に取得できるようにするにはどうすればいいか考えたところ、Per-Monitor DPI対応のためには要は対象Windowのインスタンスが参照できればいいので、ビヘイビアを使ったやり方があると思いついたところで、このやり方には先人がいたことを思い出しました。
これをどうせならXaml内で完結できるといいかなと思ったので、Expression Blendの(というより既に標準ライブラリと言っていいと思うSystem.Windows.Interactivityの)Behaviorとして実装を書いてみました。
これを使うとWindowのXamlで以下のような使い方ができます。
<Window x:Class="WpfPerMonitorDpiBehavior.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        xmlns:local="clr-namespace:WpfPerMonitorDpiBehavior"
        Title="WPF Per-Monitor DPI Behavior"
        FontFamily="Segoe UI"
        Width="400" Height="200">
    <i:Interaction.Behaviors>
        <local:PerMonitorDpiBehavior x:Name="DpiBehavior"/>
    </i:Interaction.Behaviors>

    <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid Margin="4">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Label VerticalAlignment="Center"
                   Content="System DPI"/>
            <TextBox Grid.Column="1"
                     Width="80" Height="22" Margin="6,4"
                     Text="{Binding ElementName=DpiBehavior, Path=SystemDpi, Mode=OneWay}"/>

            <Label Grid.Row="1"
                   VerticalAlignment="Center"
                   Content="Per-Monitor DPI"/>
            <TextBox Grid.Column="1" Grid.Row="1"
                     Width="80" Height="22" Margin="6,4"
                     Text="{Binding ElementName=DpiBehavior, Path=WindowDpi, Mode=OneWay}"/>
        </Grid>
    </Grid>
</Window>
10-12行目でこのビヘイビアを指定し、35と42行目でビヘイビア内のプロパティをバインディングして表示するようにしています。

なお、ここでは簡単にするためウィンドウのリサイズにはWM_DPICHANGEDのlParamの値をそのまま使うようにしています。したがって、実用にするにはリサイズ時にサイズが狂う問題への対策が必要です。また、Per-Monitor DPI対応であることをOSに伝えるため、利用するアプリのアプリケーションマニフェストでWindows 8.1対応であること、Per-Monitor DPI対応であることを示す必要があります。

[追記] 添付プロパティによる実装

一応、同じことは添付プロパティでも可能なので、書いてみました。
クラスのインスタンス自身をプロパティとして持たせるという、よくあるパターンを添付プロパティでやったものです。WindowChromeクラスのWindowChrome添付プロパティが同じような感じです。

これでビヘイビアとよく似た使い方ができます。
<Window x:Class="WpfPerMonitorDpiProperty.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfPerMonitorDpiProperty"
        Title="WPF Per-Monitor DPI Property"
        FontFamily="Segoe UI"
        Width="400" Height="200">
    <local:PerMonitorDpiProperty.AttachedProperty>        
        <local:PerMonitorDpiProperty x:Name="DpiProperty"/>
    </local:PerMonitorDpiProperty.AttachedProperty>

    <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid Margin="4">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Label VerticalAlignment="Center"
                   Content="System DPI"/>
            <TextBox Grid.Column="1"
                     Width="80" Height="22" Margin="6,4"
                     Text="{Binding ElementName=DpiProperty, Path=SystemDpi, Mode=OneWay}"/>

            <Label Grid.Row="1"
                   VerticalAlignment="Center"
                   Content="Per-Monitor DPI"/>
            <TextBox Grid.Column="1" Grid.Row="1"
                     Width="80" Height="22" Margin="6,4"
                     Text="{Binding ElementName=DpiProperty, Path=WindowDpi, Mode=OneWay}"/>
        </Grid>
    </Grid>
</Window>
こちらだとSystem.Windows.Interactivity.dllは当然不要です。

[追伸] Visual Studio 2013のデバッガーにおけるアプリケーションマニフェスト

Per-Monitor DPI対応であることをアプリケーションマニフェストで示したアプリをVS2013内のデバッガーで実行すると、Per-Monitor DPI対応にならない問題がUpdate 2まで発生していて(SetProcessDpiAwareness関数を使った場合は問題ない)、どうもデバッガーでアプリケーションマニフェストが反映されない既知の問題があったようです。この問題はUpdate 3 RCを試したところ発生しなかったので、Update 3で修正されたのだと思います。