更改 WPF 窗口的启动位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1545258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:04:44  来源:igfitidea点击:

Changing the start up location of a WPF window

wpfwindowwpf-positioning

提问by Evan

I'd like to have a WPF window open in the top right part of the screen.

我想在屏幕的右上角打开一个 WPF 窗口。

Right now I can achieve that by opening the window and then moving it (via movewindow in user32.dll). However, this approach means the window opens in it's default location, fully loads, and then moves to the top right.

现在我可以通过打开窗口然后移动它来实现这一点(通过 user32.dll 中的 movewindow)。但是,这种方法意味着窗口在其默认位置打开,完全加载,然后移动到右上角。

How could I do I change it so that I could specify the window's initial position and size?

我该如何更改它以便我可以指定窗口的初始位置和大小?

回答by Reed Copsey

Just set WindowStartupLocation, Height, Width, Left, and Top in xaml:

只需在 xaml 中设置 WindowStartupLocation、Height、Width、Left 和 Top:

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" 
    Height="500" Width="500"
    WindowStartupLocation="Manual" 
    Left="0" Top="0">
</Window>

回答by Christian Larsson

For people who like me wanted to set the position of the window to the current mouse position, you can do it like this:

对于像我这样想将窗口的位置设置为当前鼠标位置的人,可以这样做:

myWindow.WindowStartupLocation = WindowStartupLocation.Manual;
myWindow.Left = PointToScreen(Mouse.GetPosition(null)).X;
myWindow.Top = PointToScreen(Mouse.GetPosition(null)).Y;

回答by noelicus

I like to use WindowStartupLocation="CenterOwner"(MSDN docs for it)

我喜欢使用WindowStartupLocation="CenterOwner"MSDN 文档

The caller needs to specify itself as owner for this to work though, such as:

调用者需要将自己指定为所有者才能正常工作,例如:

new MyWindow() { Owner = this }.ShowDialog();

Then just define the window height and width, e.g:

然后只需定义窗口高度和宽度,例如:

<Window ...
     Height="400" Width="600"
     WindowStartupLocation="CenterOwner"
>
...

回答by Андрей Рыжов

There is a property for Window, called "WindowStartupLocation"You can find that in properties window. Simply just select Window in constructor, then go to properties list. Search for "Startup"or smth similar and you can find that property. Change it to "CenterScreen"and it will make the deal. NOTE! Make sure, that you did not select grid instead of window! Otherwise you`ll fail.

Window 有一个属性,叫做"WindowStartupLocation"你可以在属性窗口中找到它。只需在构造函数中选择 Window,然后转到属性列表。搜索"Startup"或类似,您可以找到该属性。将其更改为"CenterScreen",它将达成交易。笔记!确保您没有选择网格而不是窗口!否则你会失败。

Or you just can done it via XAML editing as some guys wrote before.

或者你可以像一些人之前写的那样通过 XAML 编辑来完成它。

回答by Michael Webb

This is what worked for me (with a different placement on screen):

这对我有用(在屏幕上的位置不同):

<Window x:Class="BtnConfig.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BtnConfig"
        mc:Ignorable="d"
        Title="MainWindow" Height="142.802" Width="448.089"
        Top="288" Left="0"> 
</Window>

Notice it does not contain:

注意它不包含:

WindowStartupLocation="Manual"