屏幕大小和wpf窗口大小的不同值

时间:2017-05-29 14:09:32

标签: c# .net wpf screen-resolution

我偶然发现了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-Addin的高度/宽度的最大值设置为显示的可用工作区域。上面的应用程序只是一个非常简单的例子来说明问题。

对我来说,只要知道16个像素普遍有效并且独立于硬件/软件就可以了。然而,如果得到一个解释是什么原因,这将是很好的。

提前问候和THX,

斯文

1 个答案:

答案 0 :(得分:0)

为什么不使用

WPF WindowState="Maximized"

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