【问题标题】:Window open on the Middle of the Right edge of the screen窗口在屏幕右边缘的中间打开
【发布时间】:2017-06-02 18:31:00
【问题描述】:

我有以下代码。我试图让窗口在主屏幕的右侧打开,在屏幕侧面的中间。它根本没有移动窗口的起始位置。

int screenWidth = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
int screenHeight = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
cd.Top = (screenHeight / 2) - (cd.Height / 2);
cd.Left = screenWidth - (cd.Width + 4);

【问题讨论】:

  • 据我了解,“cd”是您的 Window 实例?如果您将代码放在 MainWindow 类中,只需使用 this.Top 和 this.Left 而不是创建实例

标签: c# wpf xaml window


【解决方案1】:

您应该将您的代码放在窗口的 Load 事件中,而且这段代码更具可读性,并且可以按照您的意愿工作,我检查了它。

private void Window_Loaded(object sender, RoutedEventArgs e)
{
        var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
        this.Left = desktopWorkingArea.Right - this.Width;
        this.Top = desktopWorkingArea.Top + this.Height;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多