none
Modifier l'écran d'affichage principal (PrimaryDisplay) RRS feed

  • Discussion générale

  • Bonjour,

    Je cherche à pouvoir modifier l'écran d'affichage principal...malheureusement sans succès jusqu'à présent!
    Voilà le code que j'ai, pourriez-vous svp m'aider à voir où ça coince! (il n'y a pas d'erreur de compilation...c'est juste que cela ne débouche sur aucun résultat)
    D'avance merci pour l'aide que vous voudrez bien m'accorder !

    Imports System.Runtime.InteropServices
    Class MainWindow
        Const CCDEVICENAME As Short = 32
        Const CCFORMNAME As Short = 32
     
        Private Const MONITORINFOF_PRIMARY As Integer = &H1
        Private Const DISPLAY_DEVICE_ATTACHED_TO_DESKTOP As Integer = &H1
        Private Const DISPLAY_DEVICE_PRIMARY_DEVICE As Integer = &H4
        Private Const DISPLAY_DEVICE_MIRRORING_DRIVER As Integer = &H8
        Private Const DISPLAY_DEVICE_VGA_COMPATIBLE As Integer = &H10
        Private Const DISPLAY_DEVICE_REMOVABLE As Integer = &H20
        Private Const DISPLAY_DEVICE_MODESPRUNED As Integer = &H8000000
     
        Private Const DM_POSITION = &H20
        Private Const DM_DISPLAYORIENTATION = &H80 ' XP only
        Private Const DM_BITSPERPEL = &H40000
        Private Const DM_PELSWIDTH = &H80000
        Private Const DM_PELSHEIGHT = &H100000
        Private Const DM_DISPLAYFLAGS = &H200000
        Private Const DM_DISPLAYFREQUENCY = &H400000
        'Private Const DM_DISPLAYFIXEDOUTPUT As Long = &H20000000 ' XP only
     
        Private Const ENUM_CURRENT_SETTINGS As Integer = -1
        Private Const ENUM_REGISTRY_SETTINGS As Integer = -2
        Private Const EDS_RAWMODE As Integer = &H2
     
        Private Const CDS_UPDATEREGISTRY As Integer = &H1
        Private Const CDS_TEST As Integer = &H2
        Private Const CDS_FULLSCREEN As Integer = &H4
        Private Const CDS_GLOBAL As Integer = &H8
        Private Const CDS_SET_PRIMARY As Integer = &H10
        Private Const CDS_VIDEOPARAMETERS As Integer = &H20
        Private Const CDS_NORESET As Integer = &H10000000
        Private Const CDS_RESET As Integer = &H40000000
        Private Const CDS_FORCE As Integer = &H80000000
        Private Const CDS_NONE As Integer = 0
     
        Public Structure PointL
            Dim x As Integer
            Dim y As Integer
        End Structure
     
        <Flags()> _
        Enum DisplayDeviceStateFlags As Integer
            AttachedToDesktop = &H1
            MultiDriver = &H2
            PrimaryDevice = &H4
            MirroringDriver = &H8
            VGACompatible = &H10
            Removable = &H20
            ModesPruned = &H8000000
            Remote = &H4000000
            Disconnect = &H2000000
        End Enum
     
        '0 is Not Attached
     
        Const DISPLAY_PRIMARY_DEVICE = &H4 'Primary device
     
        'Holds the information of display adpter
        Private Structure DISPLAY_DEVICE
            Public cb As Integer
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> Public DeviceName As String
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> Public DeviceString As String
            Public StateFlags As Short
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> Public DeviceID As String
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> Public DeviceKey As String
        End Structure
     
        'Holds the setting of display adapter
        Private Structure DEVMODE
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> _
            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 dmOrientation As Short
            'Public dmPaperSize As Short
            'Public dmPaperLength As Short
            'Public dmPaperWidth As Short
            'Public dmScale As Short
            Public dmPositionX As Integer
            Public dmPositionY As Integer
            Public dmCopies As Short
            Public dmDefaultSource As Short
            Public dmPrintQuality As Short
            Public dmColor As Short
            Public dmDuplex As Short
            Public dmYResolution As Short
            Public dmTTOption As Short
            Public dmCollate As Short
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)> _
            Public dmFormName As String
            Public dmLogPixels As Short
            Public dmBitsPerPel As Short
            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
            'Public dmPosition As Point
            'Public dmPositionX As Integer
            'Public dmPositionY As Integer
        End Structure
     
        'API declaration set or get display adpter information
        <DllImport("user32.dll")> _
        Private Shared Function EnumDisplayDevices(ByVal Unused As Integer, _
        ByVal iDevNum As Short, ByRef lpDisplayDevice As DISPLAY_DEVICE, ByVal dwFlags As Integer) As Integer
        End Function
     
        <DllImport("user32.dll")> _
        Private Shared Function EnumDisplaySettings(ByVal lpszDeviceName As String, _
        ByVal iModeNum As Integer, ByRef lpDevMode As DEVMODE) As Integer
        End Function
     
        <DllImport("user32.dll")> _
        Private Shared Function ChangeDisplaySettingsEx(ByVal lpszDeviceName As String, _
        ByRef lpDevMode As DEVMODE, ByVal hWnd As Integer, ByVal dwFlags As Integer, _
        ByVal lParam As Integer) As Integer
        End Function
        <DllImport("user32.dll")> _
        Private Shared Function ChangeDisplaySettingsEx(ByVal lpszDeviceName As String, _
        ByRef lpDevMode As IntPtr, ByVal hWnd As Integer, ByVal dwFlags As Integer, _
        ByVal lParam As Integer) As Integer
        End Function
     
    Public Sub SetAsPrimaryMonitor(id As UInteger)
            Dim device = New DISPLAY_DEVICE()
            Dim deviceMode = New DEVMODE()
            device.cb = Marshal.SizeOf(device)
     
            EnumDisplayDevices(Nothing, id, device, 0)
            EnumDisplaySettings(device.DeviceName, -1, deviceMode)
            Dim offsetx = deviceMode.dmPositionX
            Dim offsety = deviceMode.dmPositionY
            deviceMode.dmPositionX = 0
            deviceMode.dmPositionY = 0
     
            ChangeDisplaySettingsEx(device.DeviceName, deviceMode, 0, (CDS_SET_PRIMARY Or CDS_UPDATEREGISTRY Or CDS_NORESET), 0)
     
            device = New DISPLAY_DEVICE()
            device.cb = Marshal.SizeOf(device)
     
            ' Update remaining devices
            Dim otherid As UInteger = 0
            While EnumDisplayDevices(Nothing, otherid, device, 0)
     
                If device.StateFlags = DisplayDeviceStateFlags.AttachedToDesktop AndAlso otherid <> id Then
                    device.cb = Marshal.SizeOf(device)
                    Dim otherDeviceMode = New DEVMODE()
     
                    EnumDisplaySettings(device.DeviceName, -1, otherDeviceMode)
     
                    otherDeviceMode.dmPositionX -= offsetx
                    otherDeviceMode.dmPositionY -= offsety
     
                    ChangeDisplaySettingsEx(device.DeviceName, otherDeviceMode, 0, (CDS_UPDATEREGISTRY Or CDS_NORESET), 0)
                End If
     
                device.cb = Marshal.SizeOf(device)
                otherid += 1
            End While
     
            ' Apply settings
            ChangeDisplaySettingsEx(Nothing, 0, 0, CDS_NONE, 0)
        End Sub
     
        Private Sub Button1_Click_1(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
            SetAsPrimaryMonitor(0)
        End Sub
    End Class


    mardi 19 janvier 2016 10:21

Toutes les réponses

  • Votre code semble compliqué. Il y a des exemples d'utilisation de classe pour faire le changement de moniteur

    Public Class Monitor
    
    Shared Sub New()
        buildMonitorArray()
    End Sub
    
    Public Shared totalMonitors As Integer = System.Windows.Forms.Screen.AllScreens.Count
    
    Private Shared xPositionForMonitors As New Dictionary(Of Integer, Integer)
    Private Shared yPositionForMonitors As New Dictionary(Of Integer, Integer)
    
    Public Shared Sub buildMonitorArray()
        For m As Integer = 0 To (totalMonitors - 1)
            xPositionForMonitors.Add(m, System.Windows.Forms.Screen.AllScreens(m).WorkingArea.Location.X)
            yPositionForMonitors.Add(m, System.Windows.Forms.Screen.AllScreens(m).WorkingArea.Location.Y)
        Next
    End Sub
    
    Public Shared Sub moveToNextMonitor(targWindow As Form)
        Dim newMonitorIndex As Integer = identifyCurrentMonitor(targWindow)
        Dim originalState = targWindow.WindowState
        Try
            If originalState <> FormWindowState.Normal Then
                targWindow.WindowState = FormWindowState.Normal
            End If
            targWindow.SetDesktopLocation(xPositionForMonitors(newMonitorIndex) + 1, 0)
        Finally
            targWindow.WindowState = originalState
        End Try
    End Sub
    
    Private Shared Function identifyCurrentMonitor(targWindow As Form) As Integer
        Dim currentMonitorIndex As Integer
        Dim newMonitorIndex As Integer
        For c As Integer = 0 To (totalMonitors - 1)
            If targWindow.Location.X + 10 > xPositionForMonitors(c) Then
                currentMonitorIndex = c
            End If
        Next
    
        newMonitorIndex = currentMonitorIndex + 1
        If newMonitorIndex = totalMonitors Then newMonitorIndex = 0
        Return newMonitorIndex
    End Function
    
    End Class

    Voyez: http://stackoverflow.com/questions/27368391/vb-net-move-form-to-next-monitor-and-maximise
    Mais il me semble que l'objet Screens pourrait être utilisé, essayez ceci:

        Dim MesEcrans() As Screen = Screen.AllScreens
        Dim EcranCible As Screen = Nothing
    
            If MesEcrans.Count = 1 Then
                'Un seul écran
                EcranCible = Screen.PrimaryScreen
            Else
                'Plusieurs écrans
                For IndexEcran = 0 To MesEcrans.Count - 1
                    If MesEcrans(IndexEcran).Primary = False Then
                        EcranCible = MesEcrans(IndexEcran)
                        Exit For
                    End If
    
                Next
    
            End If
            If EcranCible IsNot Nothing Then
                Me.Location = EcranCible.Bounds.Location
            End If


    Cyrille Precetti
    Bonne Année! Happy New Year!

    mardi 19 janvier 2016 11:17
  • Merci pour votre réponse...et pour le code posté!

    Je dois cependant vous préciser qu'il ne s'agit pas ici de déplacer une fenêtre sur un autre écran...mais de modifier l'affichage principal au niveau du système!


    • Modifié jmdeb mardi 19 janvier 2016 11:22
    mardi 19 janvier 2016 11:20
  • Ah Oy....

    Je n'avais pas saisie la nuance...

    Avez-vous regardé cet exemple en C#?:http://www.codeproject.com/Articles/38903/Set-Primary-Display-ChangeDisplaySettingsEx

    Je ne vois pas où vous trouvé le 0 dans l'appel

    SetAsPrimaryMonitor(0)

     quelle est la logique de cet id d'écran = 0 ?


    Cyrille Precetti
    Bonne Année! Happy New Year!

    mardi 19 janvier 2016 18:31
  • Ah oui, pardon, c'est uniquement pour tester le code!

    SetAsPrimaryMonitor(0) ou SetAsPrimaryMonitor(1)

    devant s'adresser à un ou l'autre des 2 écrans!

    mardi 19 janvier 2016 18:43
  • J'essaye de comprendre la logique de votre code:

    vous faites While enumDisplayDevices(Nothing,otherid,device,0)

    Pourriez-vous expliciter le test comme While EnumDisplayDevices() >0

    et faire un debug.print sur les devices trouvés, pour être sur que vous avez accès aux écrans que vous voulez.

    Vous finissez avec

    ChangeDisplaySettingsEx(Nothing, 0, 0, CDS_NONE, 0)

    cela ne devrait pas plutôt être quelque chose comme

    ChangeDisplaySettingsEx(NouveauDisplayPrincipal_Name,DevMode, 0,CDS_SET_PRIMARY,0)


    Cyrille Precetti
    Bonne Année! Happy New Year!

    mardi 19 janvier 2016 21:08
  • Bonjour, jmdeb,

    Le thread est-il toujours d'actualité ?
    Merci de nous tenir au courant.

    Cordialement,
    Teodora


    Votez! Appel à la contribution TechNet Community Support. LE CONTENU EST FOURNI "TEL QUEL" SANS GARANTIE D'AUCUNE SORTE, EXPLICITE OU IMPLICITE. S'il vous plaît n'oubliez pas de "Marquer comme réponse" les réponses qui ont résolu votre problème. C'est une voie commune pour reconnaître ceux qui vous ont aidé, et rend plus facile pour les autres visiteurs de trouver plus tard la résolution.

    mardi 2 février 2016 17:21
    Modérateur