gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:07:19 24 4
gpt4 key购买 nike

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

最佳答案

我认为您可以通过 P/Invoke 使用 User32.dll api 函数来完成。
See the avaiable functions .

示例代码:

[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);

关于c# - 如何以编程方式管理 Windows 7 中的多显示设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6766147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com