如何以编程方式管理 Windows 7 中的多显示器设置?

发布于 2024-11-25 10:32:15 字数 42 浏览 2 评论 0原文

我需要更改分辨率、位置,并选择哪个是主显示器,最好是在 .NET 中。

I need to change resolution, position, and select which one is the main display, preferably in .NET.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛滥成性 2024-12-02 10:32:15

我认为您可以通过 P/Invoke 使用 User32.dll api 函数来完成此操作。
查看可用函数

示例代码:

[DllImport("User32.dll")]
static extern long ChangeDisplaySettings(ref DeviceMode lpDevMode, int dwflags);

[DllImport("User32.dll")]
static extern int EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DeviceMode lpDevMode);

[DllImport("User32.dll")]
static extern int EnumDisplayDevices(string lpDevice, int iDevNum, ref DisplayDevice lpDisplayDevice, int dwFlags);

更改屏幕分辨率的代码:

//DisplayDevice is a wrapper ... you can find it [here](http://pinvoke.net/default.aspx/Structures/DISPLAY_DEVICE.html)
List<DisplayDevice> devices = new List<DisplayDevice>();

bool error = false;
//Here I am listing all DisplayDevices (Monitors)
for (int devId = 0; !error; devId++)
{
    try
    {
        DisplayDevice device = new DisplayDevice();
        device.cb = Marshal.SizeOf(typeof(DisplayDevice));
        error = EnumDisplayDevices(null, devId, ref device, 0) == 0;
        devices.Add(device);
    }
    catch (Exception)
    {
        error = true;
    }
}

List<DisplaySet> devicesAndModes = new List<DisplaySet>();

foreach (var dev in devices)
{
    error = false;
    //Here I am listing all DeviceModes (Resolutions) for each DisplayDevice (Monitors)
    for (int i = 0; !error; i++)
    {
        try
        {
            //DeviceMode is a wrapper. You can find it [here](http://pinvoke.net/default.aspx/Structures/DEVMODE.html)
            DeviceMode mode = new DeviceMode();
            error = EnumDisplaySettings(dev.DeviceName, -1 + i, ref mode) == 0;
            //Display 
            devicesAndModes.Add(new DisplaySet { DisplayDevice = dev, DeviceMode = mode });
        }
        catch (Exception ex)
        {
            error = true;
        }
    }
}

//Select any 800x600 resolution ...
DeviceMode d800x600 = devicesAndModes.Where(s => s.DeviceMode.dmPelsWidth == 800).First().DeviceMode;

//Apply the selected resolution ...
ChangeDisplaySettings(ref d800x600, 0);

I think you can do it using User32.dll api functions via P/Invoke.
See the avaiable functions.

Sample code:

[DllImport("User32.dll")]
static extern long ChangeDisplaySettings(ref DeviceMode lpDevMode, int dwflags);

[DllImport("User32.dll")]
static extern int EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DeviceMode lpDevMode);

[DllImport("User32.dll")]
static extern int EnumDisplayDevices(string lpDevice, int iDevNum, ref DisplayDevice lpDisplayDevice, int dwFlags);

Code for changing the Screen resolution:

//DisplayDevice is a wrapper ... you can find it [here](http://pinvoke.net/default.aspx/Structures/DISPLAY_DEVICE.html)
List<DisplayDevice> devices = new List<DisplayDevice>();

bool error = false;
//Here I am listing all DisplayDevices (Monitors)
for (int devId = 0; !error; devId++)
{
    try
    {
        DisplayDevice device = new DisplayDevice();
        device.cb = Marshal.SizeOf(typeof(DisplayDevice));
        error = EnumDisplayDevices(null, devId, ref device, 0) == 0;
        devices.Add(device);
    }
    catch (Exception)
    {
        error = true;
    }
}

List<DisplaySet> devicesAndModes = new List<DisplaySet>();

foreach (var dev in devices)
{
    error = false;
    //Here I am listing all DeviceModes (Resolutions) for each DisplayDevice (Monitors)
    for (int i = 0; !error; i++)
    {
        try
        {
            //DeviceMode is a wrapper. You can find it [here](http://pinvoke.net/default.aspx/Structures/DEVMODE.html)
            DeviceMode mode = new DeviceMode();
            error = EnumDisplaySettings(dev.DeviceName, -1 + i, ref mode) == 0;
            //Display 
            devicesAndModes.Add(new DisplaySet { DisplayDevice = dev, DeviceMode = mode });
        }
        catch (Exception ex)
        {
            error = true;
        }
    }
}

//Select any 800x600 resolution ...
DeviceMode d800x600 = devicesAndModes.Where(s => s.DeviceMode.dmPelsWidth == 800).First().DeviceMode;

//Apply the selected resolution ...
ChangeDisplaySettings(ref d800x600, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文