更改 WPF 窗口的启动位置

发布于 2024-08-07 11:44:10 字数 157 浏览 14 评论 0原文

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

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

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

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

几度春秋 2024-08-14 11:44:10

只需在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>

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

<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>
瞎闹 2024-08-14 11:44:10

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

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

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

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

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

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

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"
>
...
街角迷惘 2024-08-14 11:44:10

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

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

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;
埋情葬爱 2024-08-14 11:44:10

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

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

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.

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

蘑菇王子 2024-08-14 11:44:10

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

<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>

注意它不包含:

WindowStartupLocation="Manual" 

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" 
白衬杉格子梦 2024-08-14 11:44:10

就我而言,我希望“查找”工具框出现在 RichTextBox 控件的右上角位置。

输入图片这里的描述

这是用这段代码完成的:

    // Assumes variable: RichTextBox m_Box, Window m_FindWordDialog
    m_FindWordDialog.Show( ); // Let dialog show itself first

    // Note: I had to use ActualWidth because m_Box.Width somehow is (NaN) in my case
    // Not tested in different DPI.
    Point locationFromScreen = m_Box.PointToScreen( new Point(m_Box.ActualWidth, 0) );
    m_FindWordDialog.Left = locationFromScreen.X - m_FindWordDialog.Width;
    m_FindWordDialog.Top = locationFromScreen.Y;

我的搜索窗口 XAML 具有以下属性。

WindowStartupLocation="Manual" Left="0" Top="0"

它会短暂地出现在其初始位置并跳跃一点,但我可以忍受它。实际上任何值都可以, WindowStartupLocation="CenterOwner" 也可以,它只是从父级的中心跳转。或者作为肮脏的解决方法,您可以使用手动模式并让它在较远的位置启动,以免用户看到跳跃。

要使其显示相对于桌面位置,请获取屏幕工作区位置,您可以检查:如何将WPF窗口的位置设置为桌面右下角?

In my case I want my "Find" tool box to appear top-right corner of the RichTextBox control.

enter image description here

It is done with this code:

    // Assumes variable: RichTextBox m_Box, Window m_FindWordDialog
    m_FindWordDialog.Show( ); // Let dialog show itself first

    // Note: I had to use ActualWidth because m_Box.Width somehow is (NaN) in my case
    // Not tested in different DPI.
    Point locationFromScreen = m_Box.PointToScreen( new Point(m_Box.ActualWidth, 0) );
    m_FindWordDialog.Left = locationFromScreen.X - m_FindWordDialog.Width;
    m_FindWordDialog.Top = locationFromScreen.Y;

My search window XAML has following properties.

WindowStartupLocation="Manual" Left="0" Top="0"

It will briefly appear on its initial location and jump a bit, but I can live with it. Actually any value is fine, WindowStartupLocation="CenterOwner" also work, it just jump from center of parent. Or as dirty workaround, you could use manual mode and let it start in faraway position to not let user see jumping.

To make it appear relative on desktop position, acquire the screen workspace location, you can check: How to set the location of WPF window to the bottom right corner of desktop?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文