Programmatically changing the “presentation display mode”

不羁岁月 提交于 2019-11-27 01:42:32

问题


The presentation display modes are those you see when using the Windows+p shortcut:

  1. Computer Only
  2. Duplicate
  3. Extend
  4. Projector Only

Do any API calls exist which allow one to switch between these display modes?

I want to programmatically switch between monitor and HDMI TV (and do a bunch of other things simultaneously, hence Windows+p not being useful), but I'm hitting a brick wall.


回答1:


In case the EnumDisplaySettingsEx and ChangeDisplaySettingsEx functions do not work for you, you can also use this:

        private void SetDisplayMode(DisplayMode mode)
        {
            var proc = new Process();
            proc.StartInfo.FileName = "DisplaySwitch.exe";
            switch (mode)
            {
                case DisplayMode.External:
                    proc.StartInfo.Arguments = "/external";
                    break;
                case DisplayMode.Internal:
                    proc.StartInfo.Arguments = "/internal";
                    break;
                case DisplayMode.Extend:
                    proc.StartInfo.Arguments = "/extend";
                    break;
                case DisplayMode.Duplicate:
                    proc.StartInfo.Arguments = "/clone";
                    break;
            }
            proc.Start();
        }
        enum DisplayMode
        {
            Internal,
            External,
            Extend,
            Duplicate
        }

This will call the DisplaySwitcher with different arguments based on the required configuration. You can thus call:

   SetDisplayMode(DisplayMode.Extend);



回答2:


You can obtain and change the display setting using EnumDisplaySettingsEx and ChangeDisplaySettingsEx:

The ChangeDisplaySettingsEx function changes the settings of the specified display device to the specified graphics mode.

Check this Codeproject project and this Stackoverflow question for example code




回答3:


You can set the desktop display mode with SetDisplayConfig() eg.

SetDisplayConfig(0, NULL, 0, NULL, SDC_TOPOLOGY_EXTERNAL | SDC_APPLY);

You can retrieve the current display mode with QueryDisplayConfig(). eg.

DISPLAYCONFIG_TOPOLOGY_ID currentTopology;
QueryDisplayConfig(QDC_DATABASE_CURRENT, &PathArraySize, PathArray, &ModeArraySize, ModeArray, &currentTopology);

(Related source for this call here)

This is for C++. C# would require DLL imports.



来源:https://stackoverflow.com/questions/16790287/programmatically-changing-the-presentation-display-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!