WPF窗口位置 [英] WPF Window Location

查看:456
本文介绍了WPF窗口位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在主显示器的右下角启动一个Window(因为它肯定会在多显示器系统上使用).到目前为止,我已经开始工作了,但是窗口首先在屏幕中间的某个位置闪烁了一秒钟,然后移动到正确的位置.这就是我所拥有的:

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!

推荐答案

在Window.Loaded事件处理程序中设置窗口位置:

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

这篇关于WPF窗口位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆