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

后端 未结 7 2001
太阳男子
太阳男子 2020-12-07 20:39

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?

相关标签:
7条回答
  • 2020-12-07 21:07

    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);
    };
    
    0 讨论(0)
  • 2020-12-07 21:08

    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

    0 讨论(0)
  • 2020-12-07 21:12

    @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();
    
    0 讨论(0)
  • 2020-12-07 21:15

    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.

    0 讨论(0)
  • 2020-12-07 21:24

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

    0 讨论(0)
  • 2020-12-07 21:25

    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.

    0 讨论(0)
提交回复
热议问题