3
我需要更改分辨率,位置,並選擇哪一個是主顯示屏,最好使用.NET。如何以編程方式管理Windows 7中的多個顯示設置?
我需要更改分辨率,位置,並選擇哪一個是主顯示屏,最好使用.NET。如何以編程方式管理Windows 7中的多個顯示設置?
我想你可以通過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);
一個很好的問題... – Tocco
看到http://stackoverflow.com/questions/5215001/multiple-monitors -in-net/5215080#5215080和http://stackoverflow.com/questions/195267/use-windows-api-from-c-to-set-primary-monitor – Yahia
問題1:不回答我的問題和疑問2它更好但麻煩,我想知道是否有.NET的這些Windows API包裝 – Rodney