How do I enable a second monitor in C#?

前端 未结 5 990
后悔当初
后悔当初 2020-11-30 01:39

Is it possible to enable a second monitor programatically and extend the Windows Desktop onto it in C#? It needs to do the equivalent of turning on the checkbox in the image

5条回答
  •  一生所求
    2020-11-30 01:44

    I am looking for the same solution. I have written the following code to call ChangeDisplaySettingsEx with PInvoke:

    DEVMODE dm = new DEVMODE();
    dm.dmSize = (short)Marshal.SizeOf(dm);
    dm.dmPelsWidth = 1680;
    dm.dmPelsHeight = 1050;
    dm.dmBitsPerPel = 32;
    dm.dmDisplayFrequency = 60;
    dm.dmFields = DevModeFields.DM_BITSPERPEL | DevModeFields.DM_PELSWIDTH | 
                  DevModeFields.DM_PELSHEIGHT | DevModeFields.DM_DISPLAYFREQUENCY;
    int res = ChangeDisplaySettingsEx(@"\\.\DISPLAY2", ref dm, IntPtr.Zero, CDS_RESET | CDS_UPDATEREGISTRY, IntPtr.Zero);
    Console.WriteLine("result = " + res.ToString());
    

    If the monitor is already enabled, this changes the resolution successfully. But if the monitor isn't attached to the desktop already, this won't activate it. So does anyone have a code example that works?

提交回复
热议问题