WPF液晶屏全高清 [英] WPF for LCD screen Full HD

查看:241
本文介绍了WPF液晶屏全高清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个将在全高清液晶屏(42英寸)上显示的WPF应用程序。
此外,我需要在绝对位置容纳控件。
在开发环境中,我看不到长度为1920x1080的窗口(这是目标屏幕的固定分辨率)。



什么是最佳实践完成这个任务?

解决方案

WPF使用设备无关单位指定宽度/高度/位置/厚度等。



1当您的屏幕DPI设置为96dpi时,DIU / DIP = 1物理像素.....但是,当DPI不是96dpi时,1 DIU =不同数量的物理像素。



如果使用 Canvas ,那么它使用DIU定位元素。





这样就可以用 Canvas 无论当前的DPI设置是什么,你必须使用缩放技巧(你可以使用 ViewBox LayoutTransform )。



下面的例子显示了实现它的一种方法(我的屏幕是1366x768 ....你可以更改为全高清) p>

它查看系统的DPI,并在DPI上升时缩小 Canvas 。这允许你使用真正意味着像素坐标的Canvas坐标。



如果你能够将用户屏幕更改为96dpi,那么没有必要做缩放技巧1 DIU = 1物理像素在96dpi ...无需重新缩放。

 < Window x:Class =WpfApplication12.MainWindow 
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
WindowStyle =None
AllowTransparency =TrueBackground =White
SizeToContent =WidthAndHeight
Title =MainWindowLoaded =Window_Loaded>
< Viewbox x:Name =viewbox>
< Canvas x:Name =canvas>
< Rectangle x:Name =rectCanvas.Top =10Canvas.Left =10Stroke =RedStrokeThickness =1/>
< Button Canvas.Top =20Canvas.Left =20>测试按钮< / Button>
< Ellipse Canvas.Top =100Canvas.Left =100Width =100Height =100Stroke =RedStrokeThickness =10/&
< TextBlock Canvas.Top =100Canvas.Left =100FontSize =15>一些文本< / TextBlock>
< / Canvas>
< / Viewbox>
< / Window>






 系统; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
使用System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;

命名空间WpfApplication12
{
///< summary>
/// MainWindow.xaml的交互逻辑
///< / summary>
public partial class MainWindow:Window
{
// HD
const int screenwidth = 1366;
const int screenheight = 768;

// FULL HD
// const int screenwidth = 1920;
// const int screenheight = 1080;

public MainWindow()
{
InitializeComponent();

Top = 0;
Left = 0;

canvas.Width = screenwidth;
canvas.Height = screenheight;

rect.Width = screenwidth - 20;
rect.Height = screenheight - 20;
}

private void Window_Loaded(object sender,RoutedEventArgs e)
{
bool bScaleBackToPixels = true;

if(bScaleBackToPixels)
{
PresentationSource presentationsource = PresentationSource.FromVisual(this);
Matrix m = presentationsource.CompositionTarget.TransformToDevice;

double DpiWidthFactor = m.M11;
double DpiHeightFactor = m.M22;

viewbox.Width = screenwidth / DpiWidthFactor;
viewbox.Height = screenheight / DpiHeightFactor;
}
else
{
viewbox.Width = screenwidth;
viewbox.Height = screenheight;
}
}
}
}






 < Window x:Class =WpfApplication12.MainWindow
xmlns =http://schemas.microsoft.com/winfx/ 2006 / xaml / presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
WindowStyle =None
AllowTransparency =True =White
SizeToContent =WidthAndHeight
Title =MainWindowLoaded =Window_Loaded>
< Canvas x:Name =canvas>
< Rectangle x:Name =rectCanvas.Top =10Canvas.Left =10Stroke =RedStrokeThickness =1/>
< Button Canvas.Top =20Canvas.Left =20>测试按钮< / Button>
< Ellipse Canvas.Top =100Canvas.Left =100Width =100Height =100Stroke =RedStrokeThickness =10/&
< TextBlock Canvas.Top =100Canvas.Left =100FontSize =15>一些文本< / TextBlock>
< / Canvas>
< / Window>






 系统; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
使用System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;

namespace WpfApplication12
{
///< summary>
/// MainWindow.xaml的交互逻辑
///< / summary>
public partial class MainWindow:Window
{
// HD
const int screenwidth = 1366;
const int screenheight = 768;

// FULL HD
// const int screenwidth = 1920;
// const int screenheight = 1080;

public MainWindow()
{
InitializeComponent();

Top = 0;
Left = 0;

canvas.Width = screenwidth;
canvas.Height = screenheight;

rect.Width = screenwidth - 20;
rect.Height = screenheight - 20;
}

private void Window_Loaded(object sender,RoutedEventArgs e)
{
bool bScaleBackToPixels = true;

if(bScaleBackToPixels)
{
PresentationSource presentationsource = PresentationSource.FromVisual(this);
Matrix m = presentationsource.CompositionTarget.TransformToDevice;

double DpiWidthFactor = m.M11;
double DpiHeightFactor = m.M22;

double scalex = 1 / DpiWidthFactor;
double scaley = 1 / DpiHeightFactor;

canvas.LayoutTransform = new ScaleTransform(scalex,scaley);
}
}
}
}






在96 DPI设置(小 - 100%)下,屏幕显示如下:





在120 DPI设置(中 - 125% 96 x 1.25 = 120DPI)当使用上述ScaleBackToPixels技术时,屏幕看起来像这样(即它看起来与第一个屏幕相同)。





在120 DPI设置(中 - 125%) (即96 x 1.25 = 120DPI),当你不做任何调整时,屏幕看起来像这样(注意圆圈是否更大,以及按钮的字体和大小)。



>



并排所有3张图片进行比较:




I am developing a WPF application that will be displayed in a Full-HD LCD screen (42 inch). In addition, I need to accommodate the controls in absolute positions. In the development environment I can not see a window in length 1920x1080 (this is the fixed resolution of the targeted screen).

What is the best practice to accomplish this task?

解决方案

WPF uses Device Independent Units for specifying width/heights/positions/thicknesses, etc.

1 DIU/DIP = 1 physical pixel when your screen DPI is set to 96dpi.....but 1 DIU = a different number of physical pixels when the DPI is not 96dpi.

If you use a Canvas then it positions elements using DIUs.

Now you imply that you want to position absolutely in terms of pixel coordinates.

So to do this with the Canvas no matter what the current DPI setting is, you have to use a scaling trick (you can do this with a ViewBox, or a LayoutTransform).

The example below shows one way to achieve it (my screen is 1366x768....you can change it to Full HD).

It looks at the DPI of the system and gets the Canvas scaled down whenever the DPI goes up. This allows you to use Canvas coordinates that really mean pixel coords.

If you are able to change the users screen to 96dpi then there is no need to do the scaling trick because 1 DIU = 1 physical pixel at 96dpi...no rescaling needed.

<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStyle="None"
        AllowsTransparency="True" Background="White"
        SizeToContent="WidthAndHeight"
        Title="MainWindow" Loaded="Window_Loaded">
    <Viewbox x:Name="viewbox">
    <Canvas x:Name="canvas">
        <Rectangle x:Name="rect" Canvas.Top="10" Canvas.Left="10" Stroke="Red" StrokeThickness="1"/>
        <Button Canvas.Top="20" Canvas.Left="20">Test Button</Button>
            <Ellipse Canvas.Top="100" Canvas.Left="100" Width="100" Height="100" Stroke="Red" StrokeThickness="10"/>
            <TextBlock Canvas.Top="100" Canvas.Left="100" FontSize="15">Some Text</TextBlock>
        </Canvas>
    </Viewbox>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication12
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // HD
        const int screenwidth = 1366;
        const int screenheight = 768;

        // FULL HD
        //const int screenwidth = 1920;
        //const int screenheight = 1080;

        public MainWindow()
        {
            InitializeComponent();

            Top = 0;
            Left = 0;

            canvas.Width = screenwidth;
            canvas.Height = screenheight;

            rect.Width = screenwidth - 20;
            rect.Height = screenheight - 20;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            bool bScaleBackToPixels = true;

            if (bScaleBackToPixels)
            {
                PresentationSource presentationsource = PresentationSource.FromVisual(this);
                Matrix m = presentationsource.CompositionTarget.TransformToDevice;

                double DpiWidthFactor = m.M11;
                double DpiHeightFactor = m.M22;

                viewbox.Width = screenwidth / DpiWidthFactor;
                viewbox.Height = screenheight / DpiHeightFactor;
            }
            else
            {
                viewbox.Width = screenwidth;
                viewbox.Height = screenheight;
            }
        }
    }
}


<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStyle="None"
        AllowsTransparency="True" Background="White"
        SizeToContent="WidthAndHeight"
        Title="MainWindow" Loaded="Window_Loaded">
    <Canvas x:Name="canvas">
        <Rectangle x:Name="rect" Canvas.Top="10" Canvas.Left="10" Stroke="Red" StrokeThickness="1"/>
        <Button Canvas.Top="20" Canvas.Left="20">Test Button</Button>
            <Ellipse Canvas.Top="100" Canvas.Left="100" Width="100" Height="100" Stroke="Red" StrokeThickness="10"/>
            <TextBlock Canvas.Top="100" Canvas.Left="100" FontSize="15">Some Text</TextBlock>
        </Canvas>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication12
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // HD
        const int screenwidth = 1366;
        const int screenheight = 768;

        // FULL HD
        //const int screenwidth = 1920;
        //const int screenheight = 1080;

        public MainWindow()
        {
            InitializeComponent();

            Top = 0;
            Left = 0;

            canvas.Width = screenwidth;
            canvas.Height = screenheight;

            rect.Width = screenwidth - 20;
            rect.Height = screenheight - 20;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            bool bScaleBackToPixels = true;

            if (bScaleBackToPixels)
            {
                PresentationSource presentationsource = PresentationSource.FromVisual(this);
                Matrix m = presentationsource.CompositionTarget.TransformToDevice;

                double DpiWidthFactor = m.M11;
                double DpiHeightFactor = m.M22;

                double scalex = 1 / DpiWidthFactor;
                double scaley = 1 / DpiHeightFactor;

                canvas.LayoutTransform = new ScaleTransform(scalex, scaley);
            }
        }
    }
}


At the 96 DPI setting (Smaller - 100%) the screen looks like this:

At the 120 DPI setting (Medium - 125%) (i.e. 96 x 1.25 = 120DPI) the screen looks like this when using my ScaleBackToPixels technique above (i.e. it looks the same as the first screen).

At the 120 DPI setting (Medium - 125%) (i.e. 96 x 1.25 = 120DPI) the screen looks like this when you don't do any adjustments at all (notice how the circle is bigger, and the font and size of the Button).

All 3 images side by side for comparison:

这篇关于WPF液晶屏全高清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆