locked
How to change the display mode? RRS feed

  • Question

  • Hello All,
    IN windows 7, if we press [window key+P] we will get a small menu to change your display mode
    like, 1) display only on your computer, 2) duplicate [i.e the display will be on both your comp screen and on the projector], 3) Extend [that is multiple screens will form a single display] and so on.....

    can we do this programatically...... possibly using c++ or c#?

    if yes, can some one give some lead?
    I have come across this article, but still finding it difficult,

    http://www.codeproject.com/KB/dotnet...-settings.aspx

    please give me a start point from wher i can procced...

    Regards
    RK
    Radha Krishnan
    Tuesday, January 19, 2010 7:38 PM

Answers

All replies

  • Hello

    On Windows 7, ChangeDisplaySetting has a known compatibility issue. The workaround is to call the WDK function: SetDisplayConfig.


    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 20, 2010 3:41 AM
  • Hello

    How are you? May I know whether the workaround works for you?
    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, January 26, 2010 5:33 AM
  • Sorry for the late reply Friend.

    Yes it works for me.

    and thanks a lot :-)


    Radha Krishnan
    Wednesday, May 19, 2010 11:00 AM
  • Form me don't work !!

    I want to switch monitor to clone/Extended from a buttom but the screen on dispaly 1 flash and screen 2 don't start

    this is my code, you want help me ?

    Thanks, Alberto

    Imports System.Runtime.InteropServices
    Imports System.Windows.Forms
    
    Enum DISP_CHANGE As Integer
        Successful = 0
        Restart = 1
        Failed = -1
        BadMode = -2
        NotUpdated = -3
        BadFlags = -4
        BadParam = -5
        BadDualView = -6
    End Enum
    
    Public Class Form1
    
        Inherits Form
        <DllImport("user32.dll")> _
        Public Shared Function EnumDisplaySettings(deviceName As String, modeNum As Integer, ByRef devMode As DEVMODE) As Integer
        End Function
    
        <DllImport("user32.dll")> _
        Private Shared Function EnumDisplayDevices(lpDevice As String, iDevNum As UInteger, ByRef lpDisplayDevice As DISPLAY_DEVICE, dwFlags As UInteger) As Boolean
        End Function
    
        <DllImport("user32.dll")> _
        Private Shared Function ChangeDisplaySettingsEx(lpszDeviceName As String, ByRef lpDevMode As DEVMODE, hwnd As IntPtr, dwflags As UInteger, lParam As IntPtr) As DISP_CHANGE
        End Function
    
        '[DllImport("user32.dll")]
        'static extern int ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, uint dwflags, IntPtr lParam);
    
        <DllImport("user32.dll")> _
        Public Shared Function ChangeDisplaySettings(ByRef devMode As DEVMODE, flags As Integer) As Integer
        End Function
    
        Public Const szPrimaryDisplay As String = "\\.\DISPLAY1"
        Public Const szSecondaryDisplay As String = "\\.\DISPLAY2"
        Public Const DISP_CHANGE_SUCCESSFUL As Integer = 0
    
        Public Const ENUM_CURRENT_SETTINGS As Integer = -1
        Public Const DM_BITSPERPEL As Integer = CInt(&H40000L)
        Public Const DM_PELSWIDTH As Integer = CInt(&H80000L)
        Public Const DM_PELSHEIGHT As Integer = CInt(&H100000L)
        Public Const DM_POSITION As Integer = CInt(&H20L)
        Public Const DM_DISPLAYFREQUENCY As Integer = CInt(&H400000L)
        Public Const CDS_NORESET As Integer = CInt(&H10000000)
        Public Const CDS_RESET As Integer = CInt(&H40000000)
        Public Const CDS_UPDATEREGISTRY As Integer = CInt(&H1)
    
    
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
        Public Structure DEVMODE
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
            Public dmDeviceName As String
    
            Public dmSpecVersion As Short
            Public dmDriverVersion As Short
            Public dmSize As Short
            Public dmDriverExtra As Short
            Public dmFields As Integer
            Public dmPositionX As Integer
            Public dmPositionY As Integer
            Public dmDisplayOrientation As Integer
            Public dmDisplayFixedOutput As Integer
            Public dmColor As Short
            Public dmDuplex As Short
            Public dmYResolution As Short
            Public dmTTOption As Short
            Public dmCollate As Short
    
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
            Public dmFormName As String
    
            Public dmLogPixels As Short
            Public dmBitsPerPel As Int32
            Public dmPelsWidth As Integer
            Public dmPelsHeight As Integer
            Public dmDisplayFlags As Integer
            Public dmDisplayFrequency As Integer
            Public dmICMMethod As Integer
            Public dmICMIntent As Integer
            Public dmMediaType As Integer
            Public dmDitherType As Integer
            Public dmReserved1 As Integer
            Public dmReserved2 As Integer
            Public dmPanningWidth As Integer
            Public dmPanningHeight As Integer
        End Structure
    
    
        <DllImport("Kernel32.dll", EntryPoint:="RtlZeroMemory", SetLastError:=False)> _
        Private Shared Sub ZeroMemory(dest As IntPtr, size As IntPtr)
        End Sub
    
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
        Public Structure DISPLAY_DEVICE
            <MarshalAs(UnmanagedType.U4)> _
            Public cb As Integer
    
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
            Public DeviceName As String
    
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
            Public DeviceString As String
    
            <MarshalAs(UnmanagedType.U4)> _
            Public StateFlags As UInteger
    
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
            Public DeviceID As String
    
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
            Public DeviceKey As String
        End Structure
    
        Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim dmPrimary As New DEVMODE()
            Dim dmSecondary As New DEVMODE()
            Dim dmTemp As New DEVMODE()
    
            dmPrimary.dmSize = CShort(Marshal.SizeOf(dmPrimary))
            dmSecondary.dmSize = CShort(Marshal.SizeOf(dmSecondary))
            dmTemp.dmSize = CShort(Marshal.SizeOf(dmTemp))
    
            If EnumDisplaySettings(szPrimaryDisplay, ENUM_CURRENT_SETTINGS, dmTemp) = 0 Then
                MessageBox.Show("Primary Settings couldn't be enumerated", "SetExt")
            Else
                dmPrimary.dmBitsPerPel = dmTemp.dmBitsPerPel
                dmPrimary.dmPelsHeight = dmTemp.dmPelsHeight
                dmPrimary.dmPelsWidth = dmTemp.dmPelsWidth
                dmPrimary.dmDisplayFrequency = dmTemp.dmDisplayFrequency
                dmPrimary.dmFields = DM_BITSPERPEL Or DM_PELSWIDTH Or DM_PELSHEIGHT
                dmPrimary.dmPositionX = 0
                dmPrimary.dmPositionY = 0
                dmPrimary.dmFields = dmPrimary.dmFields Or DM_POSITION
    
                If dmPrimary.dmDisplayFrequency <> 0 Then
                    dmPrimary.dmFields = dmPrimary.dmFields Or DM_DISPLAYFREQUENCY
                End If
    
                dmSecondary.dmBitsPerPel = 32  ' dmPrimary.dmBitsPerPel
                dmSecondary.dmPelsHeight = 768 ' dmPrimary.dmPelsHeight
                dmSecondary.dmPelsWidth = 1024 ' dmPrimary.dmPelsWidth
                dmSecondary.dmFields = DM_BITSPERPEL Or DM_PELSWIDTH Or DM_PELSHEIGHT
    
                If dmSecondary.dmDisplayFrequency <> 0 Then
                    dmSecondary.dmFields = dmSecondary.dmFields Or DM_DISPLAYFREQUENCY
                End If
    
                dmSecondary.dmPositionX = dmPrimary.dmPelsWidth + 1
                dmSecondary.dmPositionY = 0
                dmSecondary.dmFields = dmSecondary.dmFields Or DM_POSITION
    
                'Set Extended Desktop.
                If ChangeDisplaySettingsEx(szPrimaryDisplay, dmPrimary, IntPtr.Zero, CDS_UPDATEREGISTRY Or CDS_NORESET, IntPtr.Zero) = DISP_CHANGE_SUCCESSFUL Then
                    If ChangeDisplaySettingsEx(szSecondaryDisplay, dmSecondary, IntPtr.Zero, CDS_UPDATEREGISTRY Or CDS_NORESET, IntPtr.Zero) = DISP_CHANGE_SUCCESSFUL Then
                        Dim NULL As New DEVMODE()
                        If ChangeDisplaySettingsEx(String.Empty, NULL, IntPtr.Zero, 0, IntPtr.Zero) = DISP_CHANGE_SUCCESSFUL Then
                            ChangeDisplaySettingsEx(szPrimaryDisplay, dmPrimary, IntPtr.Zero, CDS_UPDATEREGISTRY Or CDS_RESET, IntPtr.Zero)
                            MessageBox.Show("Extended Desktop Set sucessfully", "SetExt")
                        Else
                            MessageBox.Show("Final CDS Call Failed", "SetExt")
                        End If
                    Else
                        MessageBox.Show("Second CDS Call Failed", "SetExt")
                    End If
                Else
                    MessageBox.Show("First CDS Call Failed", "SetExt")
                End If
            End If
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim dmPrimary As New DEVMODE()
            Dim dmSecondary As New DEVMODE()
            Dim dmTemp As New DEVMODE()
    
            dmPrimary.dmSize = CShort(Marshal.SizeOf(dmPrimary))
            dmSecondary.dmSize = CShort(Marshal.SizeOf(dmSecondary))
            dmTemp.dmSize = CShort(Marshal.SizeOf(dmTemp))
    
            If EnumDisplaySettings(szPrimaryDisplay, ENUM_CURRENT_SETTINGS, dmTemp) = 0 Then
                MessageBox.Show("Primary Settings couldn't be enumerated", "SetExt")
            Else
                dmPrimary.dmBitsPerPel = dmTemp.dmBitsPerPel
                dmPrimary.dmPelsHeight = dmTemp.dmPelsHeight
                dmPrimary.dmPelsWidth = dmTemp.dmPelsWidth
                dmPrimary.dmDisplayFrequency = dmTemp.dmDisplayFrequency
                dmPrimary.dmFields = DM_BITSPERPEL Or DM_PELSWIDTH Or DM_PELSHEIGHT
                dmPrimary.dmPositionX = 0
                dmPrimary.dmPositionY = 0
                dmPrimary.dmFields = dmPrimary.dmFields Or DM_POSITION
    
                If dmPrimary.dmDisplayFrequency <> 0 Then
                    dmPrimary.dmFields = dmPrimary.dmFields Or DM_DISPLAYFREQUENCY
                End If
    
                dmSecondary.dmBitsPerPel = 32  ' dmPrimary.dmBitsPerPel
                dmSecondary.dmPelsHeight = 768 ' dmPrimary.dmPelsHeight
                dmSecondary.dmPelsWidth = 1024 ' dmPrimary.dmPelsWidth
                dmSecondary.dmFields = DM_BITSPERPEL Or DM_PELSWIDTH Or DM_PELSHEIGHT
                If dmSecondary.dmDisplayFrequency <> 0 Then
                    dmSecondary.dmFields = dmSecondary.dmFields Or DM_DISPLAYFREQUENCY
                End If
    
                dmSecondary.dmPositionX = dmPrimary.dmPelsWidth + 1
                dmSecondary.dmPositionY = 0
                dmSecondary.dmFields = dmSecondary.dmFields Or DM_POSITION
    
                'Set Extended Desktop.
                If ChangeDisplaySettingsEx(szPrimaryDisplay, dmPrimary, IntPtr.Zero, CDS_UPDATEREGISTRY Or CDS_NORESET, IntPtr.Zero) = DISP_CHANGE_SUCCESSFUL Then
                    If ChangeDisplaySettingsEx(szSecondaryDisplay, dmSecondary, IntPtr.Zero, CDS_UPDATEREGISTRY Or CDS_NORESET, IntPtr.Zero) = DISP_CHANGE_SUCCESSFUL Then
                        Dim NULL As New DEVMODE()
                        If ChangeDisplaySettingsEx(String.Empty, NULL, IntPtr.Zero, 0, IntPtr.Zero) = DISP_CHANGE_SUCCESSFUL Then
                            ChangeDisplaySettingsEx(szSecondaryDisplay, dmSecondary, IntPtr.Zero, CDS_UPDATEREGISTRY Or CDS_RESET, IntPtr.Zero)
                            MessageBox.Show("Extended Desktop Set sucessfully", "SetExt")
                        Else
                            MessageBox.Show("Final CDS Call Failed", "SetExt")
                        End If
                    Else
                        MessageBox.Show("Second CDS Call Failed", "SetExt")
                    End If
                Else
                    MessageBox.Show("First CDS Call Failed", "SetExt")
                End If
            End If
        End Sub
    End Class

    Wednesday, December 12, 2012 8:35 PM