Click here to Skip to main content
15,891,864 members
Articles / Programming Languages / C#

Set Primary Display (ChangeDisplaySettingsEx)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
11 Aug 2009CPOL 65.3K   2.8K   18   6
Simplified code for setting the primary display on a multi-monitor PC.

Image 1

Introduction

I've seen plenty of "complicated" code for setting the "primary" display in a multiple display PC environment, but never a "simple" C# version. (This code is based off plenty of samples I've seen on the web.)

Background

This code originated out of the need to pop applications to multiple monitors during an automated start-up script (i.e., set a designated monitor as primary, start apps, then revert back to original monitor).

Using the code

The code is left raw for addition of "bells and whistles".

C#
//manual gather - NewPrimary name ----------------------------------
WinApi.DISPLAY_DEVICE ddOne = new WinApi.DISPLAY_DEVICE();

ddOne.cb = Marshal.SizeOf(ddOne);
deviceID = 1;
WinApi.User_32.EnumDisplayDevices(null, deviceID, ref ddOne, 0);
string NewPrimary = ddOne.DeviceName;

WinApi.DEVMODE ndm6 = NewDevMode();
result = (WinApi.DisplaySetting_Results)WinApi.User_32.ChangeDisplaySettingsEx(NewPrimary, 
          ref ndm6, (IntPtr)null, (int)WinApi.DeviceFlags.CDS_SET_PRIMARY | 
          (int)WinApi.DeviceFlags.CDS_UPDATEREGISTRY, IntPtr.Zero);
Console.WriteLine("Action 3.2 result:" + result.ToString());

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionWorking VB.Net "Conversion" Pin
LostIt127823-Jan-13 16:55
LostIt127823-Jan-13 16:55 
After tinkering a bit I got a useable, albeit not very pretty, VB.Net version that you can pass a Screen object to set as Primary. If you don't pass a Screen object it will switch to the next available monitor. I wanted to offer a VB.Net alternative for anyone who may find it useful. I had a hard time getting the screens to stay in their current position when switching the primary. Setting "ndm3.dmPosition.x = 1" somehow allowed the screens to stay where they were already located when switching the primary screen as I desired. Thanks for the help!

VB
Imports System.Runtime.InteropServices

Class clsPrimaryScreen
    Private Shared Function NewDevMode() As WinAPI.DEVMODE
        Dim dm As New WinAPI.DEVMODE()
        dm.dmDeviceName = New [String](New Char(30) {})
        dm.dmFormName = New [String](New Char(30) {})
        dm.dmSize = CUShort(Marshal.SizeOf(dm))
        Return dm
    End Function
    Public Shared Function SetPrimary(Optional ByVal oNewPrimary As Screen = Nothing) As Boolean
        Dim result As WinAPI.DisplaySetting_Results = 0
        Dim OldPrimary As String = Screen.PrimaryScreen.DeviceName
        Dim NewPrimary As String = ""
        If oNewPrimary Is Nothing Then
            For Each oScreen As Screen In Screen.AllScreens
                If Not oScreen.Primary Then
                    oNewPrimary = oScreen
                    Exit For
                End If
            Next
        End If
        If Not oNewPrimary Is Nothing Then
            NewPrimary = oNewPrimary.DeviceName
        ElseIf oNewPrimary.Primary Then
            Return True
        Else
            Return False
        End If
        Dim ndm3 As WinAPI.DEVMODE
        Dim ndm4 As WinAPI.DEVMODE
        ndm3 = NewDevMode()
        ndm3.dmFields = WinAPI.DEVMODE_Flags.DM_POSITION
        ndm3.dmPosition.x = 1
        ndm3.dmPosition.y = 0
        result = DirectCast(WinAPI.ChangeDisplaySettingsEx(OldPrimary, ndm3, 0, CType(CInt(WinAPI.DeviceFlags.CDS_UPDATEREGISTRY) Or CInt(WinAPI.DeviceFlags.CDS_NORESET), DeviceFlags), 0), WinAPI.DisplaySetting_Results)
        If result <> DisplaySetting_Results.DISP_CHANGE_SUCCESSFUL Then Return False
        ndm4 = NewDevMode()
        ndm4.dmFields = WinAPI.DEVMODE_Flags.DM_POSITION
        ndm4.dmPosition.x = 0
        ndm4.dmPosition.y = 0
        result = DirectCast(WinAPI.ChangeDisplaySettingsEx(NewPrimary, ndm4, 0, CType(CInt(WinAPI.DeviceFlags.CDS_SET_PRIMARY) Or CInt(WinAPI.DeviceFlags.CDS_UPDATEREGISTRY) Or CInt(WinAPI.DeviceFlags.CDS_NORESET), DeviceFlags), 0), WinAPI.DisplaySetting_Results)
        If result <> DisplaySetting_Results.DISP_CHANGE_SUCCESSFUL Then Return False
        Dim ndm5 As WinAPI.DEVMODE = NewDevMode()
        result = DirectCast(WinAPI.ChangeDisplaySettingsEx(OldPrimary, ndm5, 0, CType(WinAPI.DeviceFlags.CDS_UPDATEREGISTRY, DeviceFlags), 0), WinAPI.DisplaySetting_Results)
        If result <> DisplaySetting_Results.DISP_CHANGE_SUCCESSFUL Then Return False
        Dim ndm6 As WinAPI.DEVMODE = NewDevMode()
        result = DirectCast(WinAPI.ChangeDisplaySettingsEx(NewPrimary, ndm6, 0, CType(WinAPI.DeviceFlags.CDS_SET_PRIMARY, DeviceFlags) Or CType(WinAPI.DeviceFlags.CDS_UPDATEREGISTRY, DeviceFlags), 0), WinAPI.DisplaySetting_Results)
        If result <> DisplaySetting_Results.DISP_CHANGE_SUCCESSFUL Then Return False
        Return True
    End Function
End Class

GeneralRe: Working VB.Net "Conversion" Pin
LostIt127824-Feb-13 7:59
LostIt127824-Feb-13 7:59 
GeneralDoes not work for me. Pin
Bill Langlais2-Nov-09 13:02
Bill Langlais2-Nov-09 13:02 
GeneralManually set monitor\display name Pin
beyonder42211-Aug-09 5:48
beyonder42211-Aug-09 5:48 
GeneralRe: Manually set monitor\display name Pin
The_Mega_ZZTer11-Aug-09 6:51
The_Mega_ZZTer11-Aug-09 6:51 
GeneralRe: Manually set monitor\display name Pin
dalek925-Aug-09 2:23
dalek925-Aug-09 2:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.