C#多屏幕显示切换

最近研究在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更加完美。

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值