Changing the start up location of a WPF window
Asked Answered
M

6

55

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?

Maurey answered 9/10, 2009 at 18:5 Comment(0)
P
119

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>
Prindle answered 9/10, 2009 at 18:9 Comment(3)
Thanks!! I knew it had to be simple, but of course I tried to find the complicated solution :).Maurey
Great! This is what I was looking for win.WindowStartupLocation = WindowStartupLocation.CenterScreen;Sceptre
@Maurey Didn't you look for "top right part of the screen"? Wouldn't it have to be Left = ScreenWidth - 500 (which shouldn't be THAT EASY in xaml)?Natator
H
9

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"
>
...
Herschelherself answered 5/7, 2018 at 13:18 Comment(0)
G
8

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;
Griner answered 27/10, 2018 at 17:22 Comment(2)
I receive an error: System.InvalidOperationException: 'This Visual is not connected to a PresentationSource.'Dialyser
Hi Jeson! Although I don't know the source of your problem, I think this thread may have some useful anwers: #2154711. It seems that the visual needs to be visible in order for PointToScreen to work. Drew Noakes had this solution to that problem: "I've found you can test IsVisible before calling PointFromScreen to protect against the InvalidOperationException.". Hope this helps.Griner
G
1

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.

Glisson answered 11/9, 2018 at 7:21 Comment(2)
In my case I don't have any window.I just use stackpanel so how can I use it?Cellini
@Cellini How can a StackPanel exist without a window?Natator
C
0

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" 
Condorcet answered 25/4, 2019 at 19:22 Comment(0)
D
0

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?

Defecate answered 22/5, 2022 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.