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

半腔热情 提交于 2019-11-28 16:57:57

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

Amittai Shapira

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.

Cyclion

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);
};

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!