Setting Primary Display

I'm trying to set the Primary Display...without success!
Here's the code I'm working on, would you please help me see what I'm doing wrong there?
There's no compilation simply doesn't give the expected result!
Thank you!!

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_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
    End Sub
End Class
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!