2017-05-29 64 views
0

我偶然发现了WPF的好奇心,我无法通过在线搜索解释自己。所以我希望你们中的一个能够给我一个提示,让我明白我的错误。不同的屏幕尺寸和wpf窗口大小的值

问题:wpf-window尺寸似乎比屏幕分辨率大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,

斯文

回答

0

为什么不使用

WPF WindowState="Maximized"

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

+0

默认应该是SizeToContent由于平均WPF的窗口是绝对比屏幕工作区小。一个大问题是使用投影机,它可能只有640x480分辨率。 –

+0

对不起,也许这个人会帮助你:https://msdn.microsoft.com/en-us/library/system.windows.systemparameters(v=vs.110).aspx(例如BorderWidth) – incureforce

+0

不用担心。我应该明白这一点:默认的WPF大小应该是SizeToContent,Min = 640 * 480,Max = workingarea。我只是看看系统参数,但不幸的是我没有找到正确的值。在我的情况下BorderWidth是5.0(并且我不知道它代表的是什么...) –