How do I enable a second monitor in C#?
Asked Answered
D

5

21

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 below.

alt text

Dried answered 24/10, 2008 at 12:55 Comment(0)
N
14

MSDN Device Context Functions

What you basically need to do:

Use the EnumDisplayDevices() API call to enumerate the display devices on the system and look for those that don't have the DISPLAY_DEVICE_ATTACHED_TO_DESKTOP flag set (this will include any mirroring devices so not all will be physical displays.) Once you've found the display device you'll need to get a valid display mode to change it to, you can find this by calling the EnumDisplaySettingsEx() API call - Generally you'd display all the available modes and allow the user to choose however in your case it sounds like this may be possible to hard-code and save you an additional step. For the sake of future-proofing your application though I'd suggest having this easily changeable without having to dig through the source every time, a registry key would be the obvious choice. Once you've got that sorted out populate a DevMode display structure with the information about the display positioning (set the PelsWidth/Height, Position, DisplayFrequency and BitsPerPel properties) then set these flags in the fields member. Finally call ChangeDisplaySettingsEx() with this settings structure and be sure to send the reset and update registry flags. That should be all you need, hope this helps,

DISPLAY_DEVICE structure import using PInvoke

EnumDisplayDevices function import

EnumDisplaySettingsEx function import

etc. the rest of them functions can be found with a simple search by name.

Nardoo answered 24/10, 2008 at 14:44 Comment(3)
Hi Pop, I can use your method if the secondary monitor is already enabled, but I can't use it to turn on the secondary method, it always fails. Should this method be able to do this?Dried
This could be a driver specific issue, the driver doesn't automatically enable a monitor when the api sets's some valid monitor settings. In my case I was working with some old Matrox card, and I didn't have this issue. It might be useful to look up some specific manufacturer docs.Nardoo
Or better yet, ask the question on MSDN driver development forums :)Nardoo
M
12

If you have windows 7, then just start a process:

    private static Process DisplayChanger = new Process
    {
        StartInfo =
        {
            CreateNoWindow = true,
            WindowStyle = ProcessWindowStyle.Hidden,
            FileName = "DisplaySwitch.exe",
            Arguments = "/extend"
        }
    };

then DisplayChanger.Start();

Mimi answered 27/6, 2010 at 7:38 Comment(1)
Works to enable 1 monitor on first display adapter but not on second display adapter.Cat
S
2

I don't have the full answer here but I am almost sure that you will have to call out of .Net to do this. You will have to use Pinvoke to call an unmanaged dll. A great resource for this is pinvoke.net.

I did a quick search and found http://www.pinvoke.net/default.aspx/user32/ChangeDisplaySettings.html which probably isn't exactly what you want but you will probably find it somewhere on pinvoke.net

Saavedra answered 24/10, 2008 at 13:46 Comment(0)
S
2

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?

Syman answered 3/2, 2009 at 14:51 Comment(0)
O
1

To enable a monitor, set its position to something other than 0,0, like as shown:

POINTL enabledPosition = new POINTL();
enabledPosition.x = -1280;
enabledPosition.y = 0;

dm.dmPosition = enabledPosition;
dm.dmFields = DM.Position;
res = ChangeDisplaySettingsEx(d.DeviceName, ref dm, IntPtr.Zero, (uint) DeviceFlags.CDS_UPDATEREGISTRY, IntPtr.Zero);
Odontoblast answered 10/2, 2011 at 20:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.