【问题标题】:WPF for LCD screen Full HD液晶屏全高清的WPF
【发布时间】:2012-09-13 17:58:48
【问题描述】:

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

完成此任务的最佳做法是什么?

【问题讨论】:

  • 设计师左上角有一个缩放栏。你是这个意思吗?
  • 没有。当应用程序在目标 LCD 屏幕中启动(最大化)时,我想保留控件的绝对位置
  • 那么你应该为控件指定绝对位置。显示一些可视化问题的代码。
  • 如果您使用画布,那么您可以使用 DIP 在其上放置元素(设备独立像素....1 DIP = 1 像素,当您将屏幕 DPI 设置为 96dpi 时)...棘手的事情是当有人没有以 96dpi 运行时该怎么办......如果你想保持像素定位,你必须缩放你的画布。
  • 假设我们在96dpi,那么如何根据画布属性(左,上)缩放控件的位置?你帮忙

标签: wpf canvas viewbox lcd scaletransform


【解决方案1】:

WPF 使用设备独立单位来指定宽度/高度/位置/厚度等。

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

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

现在你暗示你想根据像素坐标进行绝对定位。

因此,无论当前 DPI 设置是什么,要使用 Canvas 执行此操作,您都必须使用缩放技巧(您可以使用 ViewBoxLayoutTransform 执行此操作)。

下面的示例显示了一种实现方式(我的屏幕是 1366x768....您可以将其更改为全高清)。

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

如果您能够将用户屏幕更改为 96dpi,则无需进行缩放技巧,因为 1 DIU = 96dpi 时的 1 个物理像素...无需重新缩放。

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

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

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

在 120 DPI 设置(中 - 125%)(即 96 x 1.25 = 120DPI)下,当您根本不进行任何调整时,屏幕看起来像这样(注意圆圈变大了,字体和大小按钮)。

将所有 3 张图片并排比较:

【讨论】:

  • 感谢您的问候。但它在我的场景中不起作用。我正在使用背景为全高清(1920x1080)的画布,我的屏幕分辨率为 1366x768。在我的工作机器中,我看不到正确定位的控件。我认为我们错过了一些转换(可能是 TranslateTransform)
  • 取消注释这些行以获得全高清分辨率 ... //const int screenwidth = 1920;//const int screenheight = 1080; ....如果您想要一个带有 Title 的正确窗口,请删除窗口上的 WindowStyle="None" 和 AllowsTransparency="True" 属性。
  • 我还是看不到控件
  • 嗨,对不起,很难知道你想要什么......但是试试这个......这假设你使用的是 96dpi 图形模式......把全高清宽度和高度在 Canvas 上.....把 Half HD 的宽度和大小放在 ViewBox 上......以适应不同的 DPI 屏幕......然后使用 ScaleTransform 缩放调整代码
【解决方案2】:

这是使屏幕分辨率 1920x1080 (FullHD) 在我的笔记本电脑屏幕分辨率 1366x768 中可见的转换:

XAML

        <ContentControl  Canvas.Left="1630" Canvas.Top="400" Content="{Binding Time}" />
        <ContentControl  Canvas.Left="1630" Canvas.Top="590" Content="{Binding NextPrayTime}" />
        <ContentControl  Canvas.Left="1650" Canvas.Top="700" Content="{Binding Today}" />
        <ContentControl  Canvas.Right="520" Canvas.Top="120" Content="{Binding Content}" />
        <ContentControl  Canvas.Left="0" Canvas.Top="965" Content="{Binding PrayTimes}">

        </ContentControl>
    </Canvas>
</Viewbox>

C#

static public class HD
{
    static public float Width { get { return 1366.0f; } }
    static public float Height { get { return 768.0f; } }
}

static public class FHD
{
    static public float Width { get { return 1920.0f; } }
    static public float Height { get { return 1080.0f; } }
}

static public class HDRatios
{
    static public double Width
    {
        get
        {
#if (DEBUG)
            return double.Parse((HD.Width / FHD.Width).ToString("0.0"));
#else
            return 1;
#endif
        }
    }
    static public double Height
    {
        get
        {
#if (DEBUG)
            return double.Parse((HD.Height / FHD.Height).ToString("0.0"));
#else
            return 1;
#endif
        }
    }

代码显示,在开发环境(DEBUG 标志)中将应用转换,而在发布版本中将不应用转换,因为Canvas.LeftCanvas.Top 是根据全高清的分辨率。

我希望这种体验能够帮助其他遇到在 WPF 中以绝对指标在 Canvas 中显示控件的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 2011-02-22
    相关资源
    最近更新 更多