How to set the location of WPF window to the bottom right corner of desktop?
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
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.
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.