Reputation: 833
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;
Upvotes: 63
Views: 68331
Reputation: 161
Of course, I'm very late with my reply, but it may come in handy if anyone else happens to be passing by.
You can also leave the position calculation to WPF by adding this attribute to the XAML Window tag:
WindowStartupLocation="CenterScreen"
Then all that's missing is 2 lines of code in the Loaded event:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Top *= 2;
this.Left *= 2;
//this.Top -= 10; // Optional for margin
//this.Left -= 10; // Optional for margin
}
Upvotes: 0
Reputation: 404
@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();
Upvotes: 0
Reputation: 11
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);
Upvotes: 1
Reputation: 18749
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.
Upvotes: 6
Reputation: 962
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.
Upvotes: 1
Reputation: 774
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);
};
Upvotes: 3
Reputation: 3827
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.
Upvotes: 11
Reputation: 11896
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
Upvotes: 138