【问题标题】:Set WPF Window Dialogue Dimensions (Width and Height) Relative To User's Main Window Dimensions设置相对于用户主窗口尺寸的 WPF 窗口对话框尺寸(宽度和高度)
【发布时间】:2020-07-17 11:42:33
【问题描述】:

我在 WPF 中有一个游戏,当用户输掉这个特定游戏时,会显示一个包含所有游戏详细信息和游戏统计信息的窗口:

问题是窗口被设置为固定大小,所以如果用户有一个较小的屏幕,它会看起来像这样:

我不确定如何制作它,因此即使在较小的屏幕尺寸下,Window 也能很好地适应。我不确定这在 WPF 中是否可行 - 但如有任何帮助,我们将不胜感激。

谢谢,

【问题讨论】:

  • 如果你没有设置任何尺寸而只设置为中心到屏幕它应该有合理的默认 IIRC。

标签: c# wpf xaml


【解决方案1】:

对于居中,使用Window.WindowStartupLocationWindow.Owner

//"dialog" being your child window
//"this" being your calling window
dialog.Owner = this;
dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;

或者,您可以在子窗口的 XAML 中而不是在代码中设置 WindowStartupLocation="CenterOwner"

为了调整大小,您可以将子窗口设置为主窗口大小或用户屏幕大小的某个百分比:

//Child window would 25% the size of the caller.
double scale = 0.25;

dialog.Width = this.Width * scale;
dialog.Height = this.Height * scale;

//Or 25% the size of the screen
dialog.Width = System.Windows.SystemParameters.WorkArea.Width * scale;
dialog.Height = System.Windows.SystemParameters.WorkArea.Height * scale;

请注意,SystemParameters.WorkArea 为您提供“主屏幕”的尺寸,因此这在多显示器设置中可能无法正常工作。如果这是一个问题,您可以查看this question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2016-06-07
    • 1970-01-01
    相关资源
    最近更新 更多