WPF Window Location

青春壹個敷衍的年華 提交于 2019-12-08 01:18:25

问题


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!


回答1:


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

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