简体   繁体   English

将 window 自动移动到具有指定屏幕分辨率的显示器

[英]Moving window automatically to a monitor with a specified screen resolution

I have an application that when it loads a certain window it need to automatically move to the screen that has 1920x515 resolution.我有一个应用程序,当它加载某个 window 时,它需要自动移动到分辨率为 1920x515 的屏幕。 I found a way to make it work but I'd be looking for a more efficient way of doing this as clearly this is the worst approach.我找到了一种让它工作的方法,但我正在寻找一种更有效的方法来做到这一点,因为显然这是最糟糕的方法。 How can I improve this?我该如何改进呢?

private void ComboBox2_Initialized(object sender, EventArgs e)
        {
            foreach (var screen in Screen.AllScreens)
            {
                // For each screen, add the screen properties to a list box.
                //ComboBox2.Items.Add("Location: "  + screen.Bounds.Location .ToString());
                ComboBox2.Items.Add(screen.Bounds.Left.ToString());
                ComboBox2.Items.Add(screen.Bounds.Width.ToString());
                ComboBox2.Items.Add(screen.Bounds.Top.ToString());
                ComboBox2.Items.Add(screen.Bounds.Height.ToString());

                //double top = 0;
                //double left = 0; 


                if (ComboBox2.Items.Contains("1920"))
                {


                    if (ComboBox2.Items.Contains("515"))
                    {

                        Properties.Settings.Default.Top = screen.Bounds.Top;
                        Properties.Settings.Default.Left = screen.Bounds.Left;


                        Properties.Settings.Default.Save();
                        //System.Windows.MessageBox.Show(Properties.Settings.Default.Left.ToString());

                    }
                }


            }
        }```

This code doesn't move the screen at all.这段代码根本不移动屏幕。 It only fills a combo with the dimensions of the screens, then saves some of them in the application's settings.它只用屏幕的尺寸填充组合,然后将其中一些保存在应用程序的设置中。

To move the form you need to find the top left coordinates of the target screen and move the form to them:要移动表单,您需要找到目标屏幕的左上角坐标并将表单移动到它们:

var desired=new Size(1920,515);
var targetScreen= Screen.AllScreens.FirstOrDefault(
                          screen=> screen.Bounds.Size==desired);
if (targetScreen != null)
{
    this.Location=targetScreen.WorkingArea.Location;
}

Thank you @Panagiotis Kanavos This fixed my issue谢谢@Panagiotis Kanavos 这解决了我的问题

var desired = new System.Drawing.Size(1920, 515);


            var targetScreen = Screen.AllScreens.FirstOrDefault(
                                      screen => screen.Bounds.Size == desired);

            
            if (targetScreen != null)
            {
                this.Left = targetScreen.WorkingArea.Location.X;
                this.Top = targetScreen.WorkingArea.Location.Y;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM