我刚刚用 C# 编写了一个小程序,可以在我的桌面上显示引号。我的问题是将应用程序的窗口居中。要计算所需的窗口位置,我使用以下公式:
Left = (Screen.PrimaryScreen.Bounds.Width - this.ActualWidth) / 2;
Top = (Screen.PrimaryScreen.Bounds.Height - this.ActualHeight) / 2;
Run Code Online (Sandbox Code Playgroud)
不幸的是 this.ActualWidth 和 this.ActualHeight 给出了错误的值,如您在屏幕截图中所见。188,4 和 189 而不是 1000(+)。
http://www.directupload.net/file/d/4044/4x2tgadg_png.htm
这是图形用户界面:
http://www.directupload.net/file/d/4044/63rx2sul_png.htm
文本形式的代码:
public partial class QuotePresenter : Window
{
public QuotePresenter()
{
InitializeComponent();
setSize();
}
private void setSize()
{
MaxWidth = Screen.PrimaryScreen.Bounds.Width * 0.8;
MaxHeight = Screen.PrimaryScreen.Bounds.Height * 0.8;
}
private void setLocation()
{
Left = (Screen.PrimaryScreen.Bounds.Width - this.ActualWidth) / 2;
Top = (Screen.PrimaryScreen.Bounds.Height - this.ActualHeight) / 2;
System.Windows.MessageBox.Show("Actual Width: " + …
Run Code Online (Sandbox Code Playgroud)