2017-05-29 5 views
0

Je suis tombé sur une curiosité dans WPF dont je ne pouvais pas m'expliquer et que je ne pouvais pas résoudre en cherchant en ligne. Donc, j'espère que l'un de vous est capable de me donner un indice pour comprendre mon erreur. Problème: Les dimensions de la fenêtre wpf semblent être supérieures de 16 unités à la résolution de l'écran. Les 16 pixels/unités sont indépendants de la dimension (windowwidth, windowheight) et de la résolution de l'écran. Le problème est affiché dans l'application suivante:Différentes valeurs pour la taille de l'écran et la taille de la fenêtre wpf

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

Fondamentalement, le code est nécessaire pour définir les max-valeurs pour la hauteur/largeur d'un Excel -Addin à la zone de travail disponible de l'affichage. L'application ci-dessus est juste un exemple très simple pour illustrer le problème.

Pour moi, il serait juste de savoir que les 16 pixels sont universellement valides et indépendants de hard-/software. Néanmoins, il serait bon d'obtenir une explication quelle est la raison de ce comportement.

Salutations et THX à l'avance,

Sven

Répondre

0

Pourquoi ne pas utiliser

WPF WindowState="Maximized"

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

+0

La valeur par défaut devrait être SizeToContent parce que la WPF-fenêtre moyenne est nettement plus petit que la zone de travail de l'écran. Un gros problème est l'utilisation d'un projecteur, qui pourrait avoir seulement une résolution de 640x480. –

+0

Oh désolé, peut-être que celui-ci vous aidera: https://msdn.microsoft.com/en-us/library/system.windows.systemparameters(v=vs.110).aspx (par exemple BorderWidth) – incureforce

+0

Pas de soucis. J'aurais dû le préciser: WPF-Size par défaut devrait être SizeToContent, Min = 640 * 480 et Max = workingarea. J'ai juste jeté un oeil dans le Systemparameters mais malheureusement je n'ai pas trouvé la bonne valeur. BorderWidth est 5.0 dans mon cas (et je ne savais pas exactement ce que cela signifie ...) –