【发布时间】: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