I'm trying to get a Window to start in the bottom right corner of the primary display (as it will definitely be used on multi-monitor systems). So far, I've got it working, but the window first flashes somewhere in the middle of the screen for a split second, then moves to the correct location. Here's what I've got:
public MyWindow()
{
InitializeComponent();
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
var workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
var corner = transform.Transform(new Point(workingArea.Right, workingArea.Bottom));
this.Left = corner.X - this.ActualWidth;
this.Top = corner.Y - this.ActualHeight;
}));
}
I've tried the obvious stuff like hiding the window then showing it again once the move is complete, but that doesn't seem to work either as it just then never shows the window at all.
I know its a pretty small issue, but its oddly quite an annoying and I'd love to get it sorted!
Set the window location in a Window.Loaded event handler:
public MainWindow()
{
InitializeComponent();
Loaded += (o, e) =>
{
var workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
var corner = transform.Transform(new Point(workingArea.Right, workingArea.Bottom));
this.Left = corner.X - this.ActualWidth;
this.Top = corner.Y - this.ActualHeight;
};
}
来源:https://stackoverflow.com/questions/9063886/wpf-window-location