跟踪Windows窗体应用程序中的屏幕更改和屏幕分辨率更改以更改表单大小 [英] Keep track of screen change and screen resolution change in Windows Form Application to change form size

查看:115
本文介绍了跟踪Windows窗体应用程序中的屏幕更改和屏幕分辨率更改以更改表单大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据屏幕及其分辨率来更改表单大小.

I want to change form size depending on Screen and it's resolution.

我想要的是一个正确的事件,以在运行时跟踪这些屏幕更改以及屏幕分辨率更改.

What I want is a correct event to track these screen changes as well as screen resolution changes at runtime.

换句话说,

  1. 如果用户使用两个屏幕并将应用程序移动到另一个屏幕,则应跟踪该屏幕并相应地更改尺寸,即,如果新屏幕的分辨率较低,则减小尺寸;如果分辨率较大,则增大尺寸.

  1. If user is using two screens and move application to another screen, that should be tracked and change size accordingly, i.e. reduce size if new screen's resolution is low or increase size if resolution is larger.

还可以跟踪同一屏幕上屏幕分辨率的变化,并相应地更改尺寸.

Also track screen resolution change on the same screen, and make changes to size accordingly.

我知道如何更改表格大小,获取当前屏幕及其分辨率,只需要这些事件即可跟踪这些更改.

I know how to change Form size, get current screen and it's resolution, just need these events to keep track of these changes.

推荐答案

仔细研究这个答案,我决定对其进行改进,并添加更多信息以形成更完整的解决方案.

Going over this answer I've decided to improve it and add further information to form a more complete solution.

挑战

跟踪当前在哪个屏幕上呈现Form.如果用户将表单拖动到另一台监视器或拔出监视器,则可以更改此设置.如果用户手动将窗口拖动到其他显示器或直接更改分辨率,则分辨率可以更改.

Tracking which screen a Form is currently being rendered on. This can change if a user drags the form to another monitor or unplugs a monitor. The resolution can change if a user manually drags a window to a different display or changes the resolution directly.

首先,跟踪表单位置.我们需要加入表单上下文的Move事件,幸运的是.Net框架提供了这样的事件,它名为

Firstly, tracking form location. We need to hook into a Move event for the form context, fortunately the .Net framework provides such an event, and it is named Control.Move Event.

其次,我们将需要处理屏幕分辨率更改事件,我们可以使用

Secondly, we will need to hook into a screen resolution changed event, we can do this with the SystemEvents.DisplaySettingsChanged event.

把它们放在一起,我得到了:

And putting it together, I got this:

struct Resolution
{
    public int Width;
    public int Height;
}

int previous = -1;
int current = -1;

private bool CheckScreenChanged()
{
    bool changed = false;
    current = GetScreenIndex();

    if (current != -1 && previous != -1 && current != previous) // form changed screen.
    {
        changed = true;
    }

    previous = current;

    return changed;
}

private int GetScreenIndex()
{
    return Array.IndexOf(Screen.AllScreens, Screen.FromControl(this));
}

private Resolution GetCurrentResolution()
{
    Screen screen = Screen.FromControl(this);
    Resolution res = new Resolution();
    res.Width = screen.Bounds.Width;
    res.Height = screen.Bounds.Height;

    return res;
}

private void SetResolutionLabel()
{
    Resolution res = GetCurrentResolution();
    label2.Text = String.Format("Width: {0}, Height: {1}", res.Width, res.Height);
}

private void ScreenChanged()
{
    label1.Text = "Screen " + current.ToString();
}

private void Form_Moved(object sender, System.EventArgs e)
{
    bool changed = CheckScreenChanged();
    if (changed == true)
    {
        ScreenChanged();
        SetResolutionLabel();
    }
}

public void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
    SetResolutionLabel();
}

public void Initialize()
{
    this.Move += Form_Moved;
    SystemEvents.DisplaySettingsChanged += new
    EventHandler(SystemEvents_DisplaySettingsChanged);

    previous = GetScreenIndex();
    current = GetScreenIndex();
    ScreenChanged();
    SetResolutionLabel();
}

上面的代码在具有两个标签的简单表单上进行了测试,这两个标签分别称为label1和label2,这些标签会在屏幕上的表单更改或分辨率更改时进行更新.

The code above is tested on a simple form with two labels called label1 and label2, which are updated when the screen the form is on changes or the resolution changes.

此操作在我的主屏幕/显示屏上的图片

An image of this in action on my primary screen/display

将表单拖到辅助屏幕/显示屏上时:

And on my secondary screen/display when the form has been dragged to it:

这篇关于跟踪Windows窗体应用程序中的屏幕更改和屏幕分辨率更改以更改表单大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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