반응형

 

1. DpiDecorator class 추가

public class DpiDecorator : Decorator
{
    public DpiDecorator()
    {
        Loaded += OnLoaded;
        Unloaded -= OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        if (null != e)
            e.Handled = true;

        var source = PresentationSource.FromVisual(this);

        if (null != source && null != source.CompositionTarget)
        {
            var matrix = source.CompositionTarget.TransformToDevice;

            var dpiTransform = new ScaleTransform(1 / matrix.M11, 1 / matrix.M22);

            if (dpiTransform.CanFreeze)
                dpiTransform.Freeze();

            LayoutTransform = dpiTransform;
        }
    }
}

 

2. XML에 DpiDecorator로 감싸줌

<local:DpiDecorator>
    <Grid>
        <TextBlock Text="With DPI decorator" FontSize="20"/>
        <Button Content="Window" Click="btnOpenWindow_Click" Width="100" Height="50"/>
    </Grid>
</local:DpiDecorator>

 

참고

https://github.com/mesta1/DPIHelper

 

GitHub - mesta1/DPIHelper: This project explains how to create a DPI-aware WPF application and a simple way to scale it for diff

This project explains how to create a DPI-aware WPF application and a simple way to scale it for different resolutions. - GitHub - mesta1/DPIHelper: This project explains how to create a DPI-aware ...

github.com

 

반응형

+ Recent posts