How to set the location of WPF window to the bottom right corner of desktop?

WpfWindowLocation

Wpf Problem Overview


I want to show my window on top of the TaskBar's clock when the windows starts.

How can I find the bottom right corner location of my desktop?

I use this code that works well in windows forms app but does not work correctly in WPF:

var desktopWorkingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
this.Left = desktopWorkingArea.Right - this.Width;
this.Top = desktopWorkingArea.Bottom - this.Height;

Wpf Solutions


Solution 1 - Wpf

This code works for me in WPF both with Display 100% and 125%

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

In brief I use

System.Windows.SystemParameters.WorkArea

instead of

System.Windows.Forms.Screen.PrimaryScreen.WorkingArea

Solution 2 - Wpf

To access the desktop rectangle, you could use the Screen class - Screen.PrimaryScreen.WorkingArea property is the rectangle of your desktop.

Your WPF window has Top and Left properties as well as Width and Height, so you could set those properties relative to the desktop location.

Solution 3 - Wpf

You can use the window's SizeChanged event instead of Loaded if you want the window to stay in the corner when its size changes. This is especially handy if the window has Window.SizeToContent set to some value other than SizeToContent.Manual; in this case it will adjust to fit the content while staying in the corner.

public MyWindow()
{
    SizeChanged += (o, e) =>
    {
        var r = SystemParameters.WorkArea;
        Left = r.Right - ActualWidth;
        Top = r.Bottom - ActualHeight;
    };
    InitializeComponent();
}

Note also that you should subtract ActualWidth and ActualHeight (instead of Width and Height as shown in some other replies) to handle more possible situations, for example switching between SizeToContent modes at runtime.

Solution 4 - Wpf

My code:

MainWindow.WindowStartupLocation = WindowStartupLocation.Manual;

MainWindow.Loaded += (s, a) =>
{
    MainWindow.Height = SystemParameters.WorkArea.Height;
    MainWindow.Width = SystemParameters.WorkArea.Width;
    MainWindow.SetLeft(SystemParameters.WorkArea.Location.X);
    MainWindow.SetTop(SystemParameters.WorkArea.Location.Y);
};

Solution 5 - Wpf

I solved this problem with a new window containing a label named MessageDisplay. The code accompanying the window was as follows:

public partial class StatusWindow : Window
{
    static StatusWindow display;

    public StatusWindow()
    {
        InitializeComponent();
    }

    static public void DisplayMessage( Window parent, string message )
    {
        if ( display != null )
            ClearMessage();
        display = new StatusWindow();
        display.Top = parent.Top + 100;
        display.Left = parent.Left + 10;
        display.MessageDisplay.Content = message;
        display.Show();
    }

    static public void ClearMessage()
    {
        display.Close();
        display = null;
    }
}

For my application, the setting of top and left puts this window below the menu on the main window (passed to DisplayMessage in the first parameter);

Solution 6 - Wpf

This above solutions did not entirely work for my window - it was too low and the bottom part of the window was beneath the taskbar and below the desktop workspace. I needed to set the position after the window content had been rendered:

private void Window_ContentRendered(object sender, EventArgs e)
{
    var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
    this.Left = desktopWorkingArea.Right - this.Width - 5;
    this.Top = desktopWorkingArea.Bottom - this.Height - 5;
}

Also, part of the frame was out of view, so I had to adjust by 5. Not sure why this is needed in my situation.

Solution 7 - Wpf

@Klaus78 's answer is correct. But since this is first thing google pops up and if working in environments where screen resolution can change often such that your app runs on virtual desktops or virtual servers and you still need it to update its placement when the screen resolution changes I have found linking to the SystemEvents.DisplaySettingsChanged event to be beneficial. Here is an example using rx and you can put this in your constructor for your view.

        Observable
            .FromEventPattern<EventHandler, EventArgs>(_ => SystemEvents.DisplaySettingsChanged += _, _ => SystemEvents.DisplaySettingsChanged -= _)
            .Select(_ => SystemParameters.WorkArea)
            .Do(_ =>
            {
                Left = _.Right - Width;
                Top = _.Bottom - Height;
            })
            .Subscribe();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVirtualWorldView Question on Stackoverflow
Solution 1 - WpfKlaus78View Answer on Stackoverflow
Solution 2 - WpfAmittai ShapiraView Answer on Stackoverflow
Solution 3 - WpfGlenn SlaydenView Answer on Stackoverflow
Solution 4 - WpfCyclionView Answer on Stackoverflow
Solution 5 - WpfGeorge HahnView Answer on Stackoverflow
Solution 6 - WpfPIntagView Answer on Stackoverflow
Solution 7 - WpfJMIIIView Answer on Stackoverflow