设置相对于用户主窗口尺寸的WPF窗口对话尺寸(宽度和高度)

时间:2020-07-16 23:53:11

标签: c# wpf xaml

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

问题是窗口设置为固定大小,因此,如果用户的屏幕较小,它将看起来像这样:

enter image description here

我不确定如何制作,因此即使在较小的屏幕尺寸下,Window也能很好地适合。我不确定WPF中是否还可以实现-但是对此有任何帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

要居中,请使用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