欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF 窗口/页 的位置

程序员文章站 2022-07-13 22:40:11
...

首先新建一个WPF工程,在主界面添加一个按钮,并给按钮添加点击事件button1_Click,然后新建一个用于测试弹出位置的窗口TestWindow。


  • 在屏幕中间显示,设置window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
private void button1_Click(object sender, RoutedEventArgs e)
{
    TestWindow window = new TestWindow();
    window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    window.ShowDialog();
}

 

  • 在父窗口中间显示,设置window.WindowStartupLocation = WindowStartupLocation.CenterOwner;,并指定Owner。
private void button1_Click(object sender, RoutedEventArgs e)
{
    TestWindow window = new TestWindow();
    window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
    window.Owner = this;
    window.ShowDialog();
}

 

  • 在任意位置显示,设置window.WindowStartupLocation = WindowStartupLocation.Manual;并制定窗口的Left和Top坐标。
private void button1_Click(object sender, RoutedEventArgs e)
{
    TestWindow window = new TestWindow();
    window.WindowStartupLocation = WindowStartupLocation.Manual;
    window.Left = 0;
    window.Top = 0;
    window.ShowDialog();
}