最近研究在Windows中多屏幕显示时,如何切换显示模式:仅电脑屏幕、复制、扩展、仅第二屏幕,有两种方案。
1、通过系统自带的DisplaySwitch.exe,在C:\Windows\System32中,不过要拷贝到程序当前目录调用,不然会提示无法找到文件:
using (var proc = new Process())
{
proc.StartInfo.FileName = @"DisplaySwitch.exe";
proc.StartInfo.Arguments = "/external";
proc.Start();
}
proc.StartInfo.Arguments为传递的参数,共计有四种,可实现屏幕切换。
2、调用Windows 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)]
private static extern long SetDisplayConfig(uint numPathArrayElements, IntPtr pathArray, uint numModeArrayElements,IntPtr modeArray, uint flags);
/// <summary>
/// 设置屏幕的显示模式
/// </summary>
/// <param name="type">模式(0 - 主屏 1 - 双屏复制 2 - 双屏扩展 3 - 第二屏幕</param>
/// <returns></returns>
public void SetScreenMode(int type)
{
uint smode;
switch (type)
{
case 0:
smode = SDC_APPLY | SDC_TOPOLOGY_INTERNAL;
break;
case 1:
smode = SDC_APPLY | SDC_TOPOLOGY_CLONE;
break;
case 2:
smode = SDC_APPLY | SDC_TOPOLOGY_EXTEND;
break;
case 3:
smode = SDC_APPLY | SDC_TOPOLOGY_EXTERNAL;
break;
default:
smode = SDC_APPLY | SDC_TOPOLOGY_INTERNAL;
break;
}
SetDisplayConfig(0, IntPtr.Zero, 0, IntPtr.Zero, smode);
}
方法1和2都可以实现多屏幕显示切换,但是方法2的切换更快,而且在win10中屏幕右侧不会出现win+P一样的菜单,所以方法2时首选,比方法1更加完美。