1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| 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 |
Partager