【问题标题】:Different values for screen size and wpf-window size屏幕大小和 wpf 窗口大小的不同值
【发布时间】:2017-05-29 14:09:31
【问题描述】:

我偶然发现了对 WPF 的好奇心,我无法解释自己,而我无法通过在线搜索来解决。所以我希望你们中的一个能够给我一个提示来理解我的错误。

问题:wpf 窗口尺寸似乎比屏幕分辨率大 16 个单位。 16 个像素/单位与尺寸(窗口宽度、窗口高度)和屏幕分辨率无关。 问题显示在以下应用程序中:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="250" Width="350">
    <DockPanel Margin="10,10,0,0">
        <DockPanel Width="152" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Left">
            <TextBlock x:Name="displayHeight" HorizontalAlignment="Left" Margin="0,5,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="60"/>
            <Label Content="Displayheight" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30"/>

        </DockPanel>

        <DockPanel Width="148" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Left">
            <TextBlock x:Name="displayWidth" HorizontalAlignment="Left" Margin="0,5,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="60"/>
            <Label Content="Displaywidth" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30"/>

        </DockPanel>
        <DockPanel Width="162" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Left">
            <TextBlock HorizontalAlignment="Left" Margin="0,5,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding ActualHeight, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Width="60"/>
            <Label Content="Windowheight" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="92"/>

        </DockPanel>
        <DockPanel Width="153" DockPanel.Dock="Top" VerticalAlignment="Top" HorizontalAlignment="Left">
            <TextBlock HorizontalAlignment="Left" Margin="0,5,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding ActualWidth, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Width="60"/>
            <Label Content="Windowwidth" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30"/>

        </DockPanel>

    </DockPanel>
</Window>

C#:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Forms;
using System.Drawing;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            IntPtr ownerHandle = Process.GetCurrentProcess().MainWindowHandle;
            WpfScreen currentScreen = WpfScreen.GetScreenFrom(ownerHandle);
            Rect workingArea = currentScreen.WorkingArea;

            this.displayHeight.Text = workingArea.Height.ToString();
            this.displayWidth.Text = workingArea.Width.ToString();
        }
    }

    public class WpfScreen
    {
        public static WpfScreen GetScreenFrom(IntPtr windowIntPTR)
        {
            Screen screen = System.Windows.Forms.Screen.FromHandle(windowIntPTR);
            WpfScreen wpfScreen = new WpfScreen(screen);
            return wpfScreen;
        }

        private readonly Screen screen;

        internal WpfScreen(System.Windows.Forms.Screen screen)
        {
            this.screen = screen;
        }

        public Rect WorkingArea
        {
            get { return this.GetRect(this.screen.WorkingArea); }
        }

        private Rect GetRect(Rectangle value)
        {
            return new Rect
            {
                X = value.X,
                Y = value.Y,
                Width = value.Width,
                Height = value.Height
            };
        }
    }
}

基本上需要代码将 Excel 插件的高度/宽度的最大值设置为显示的可用工作区域。上面的应用程序只是一个非常简单的例子来说明问题。

对我来说,只要知道 16 个像素是普遍有效的并且独立于硬件/软件就可以了。不过,如果能得到一个解释,这种行为的原因是什么,那就太好了。

提前问候和THX,

斯文

【问题讨论】:

    标签: c# .net wpf screen-resolution


    【解决方案1】:

    为什么不使用

    WPFWindowState="Maximized"

    见:How can I make a WPF window maximized on the screen with the mouse cursor?

    【讨论】:

    • 默认应该是 SizeToContent 因为平均 wpf-Window 肯定小于屏幕工作区。一个大问题是使用投影仪,它可能只有 640x480 分辨率。
    • 哦,对不起,也许这个可以帮助你:msdn.microsoft.com/en-us/library/…(例如 BorderWidth)
    • 不用担心。我应该说清楚:默认 WPF-Size 应该是 SizeToContent,Min = 640*480 和 Max = workingarea。我只是查看了 Systemparameters 但不幸的是我没有找到正确的值。在我的情况下,BorderWidth 是 5.0(我不知道它到底代表什么......)
    • 现在我明白了,BorderWidth 是 Window 的 ClientArea 和 Window 本身之间的宽度。 CaptionTitle 是标题栏的高度(不要添加 BorderWidth ^^)。使用这些值和您的 WPFScreen 类,您可以计算当前屏幕(对于您的窗口)的客户区域的最大尺寸。如果您检查窗口移动/重绘事件,您可以重新检查当前屏幕并调整最大值。
    • 首先感谢您的努力!我不确定这是否能解决我的问题,但我明天会更深入地研究 SystemParameters 并在这里回复......
    猜你喜欢
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2012-04-10
    相关资源
    最近更新 更多