C# 切换多屏幕显示模式
网上搜索了下,有2种方式。
一、直接执行系统自带的程序 DisplaySwitch.exe,它在 windows\system32 目录下。对应的 C# 代码如下:
using (var proc = new Process())
{
proc.StartInfo.FileName = @"DisplaySwitch.exe";
proc.StartInfo.Arguments = "/external";
proc.Start();
}
二、调用系统 API SetDisplayConfig 函数
private const uint SDC_APPLY = 0x00000080;
private const uint SDC_TOPOLOGY_INTERNAL = 0x00000001;
private const uint SDC_TOPOLOGY_CLONE = 0x00000002;
private const uint SDC_TOPOLOGY_EXTERNAL = 0x00000008;
private const uint SDC_TOPOLOGY_EXTEND = 0x00000004;
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern long SetDisplayConfig(uint numPathArrayElements, IntPtr pathArray, uint numModeArrayElements,IntPtr modeArray, uint flags);
/// /// 设置屏幕的显示模式 ///
/// 模式(0 - 主屏 1 - 双屏复制 2 - 双屏扩展 3 - 第二屏幕
///
public void SetScreenMode(int type)
{
uint mode;
switch (type)
{
case 0:
mode = SDC_APPLY | SDC_TOPOLOGY_INTERNAL;
break;
case 1:
mode = SDC_APPLY | SDC_TOPOLOGY_CLONE;
break;
case 2:
mode = SDC_APPLY | SDC_TOPOLOGY_EXTEND;
break;
case 3:
mode = SDC_APPLY | SDC_TOPOLOGY_EXTERNAL;
break;
default:
mode = SDC_APPLY | SDC_TOPOLOGY_INTERNAL;
break;
}
SetDisplayConfig(0, IntPtr.Zero, 0, IntPtr.Zero, mode);
}