' Déclaration des fonctions nécessaires
Declare PtrSafe Function EnumDisplayMonitors Lib "user32" ( _
ByVal hdc As LongPtr, ByVal lprcClip As LongPtr, _
ByVal lpfnEnum As LongPtr, ByVal dwData As Long) As Long
Declare PtrSafe Function GetMonitorInfo Lib "user32" Alias "GetMonitorInfoA" ( _
ByVal hMonitor As LongPtr, ByRef lpmi As monitorInfo) As Long
Declare PtrSafe Function EnumDisplayDevices Lib "user32" Alias "EnumDisplayDevicesA" ( _
ByVal lpDevice As String, ByVal iDeviceNum As Long, _
ByRef lpDisplayDevice As DISPLAY_DEVICE, ByVal dwFlags As Long) As Long
' Déclaration de types et structures
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Type monitorInfo
cbSize As Long
rcMonitor As RECT ' Dimensions du moniteur
rcWork As RECT ' Dimensions de l'espace de travail
dwFlags As Long ' 1 = Moniteur principal
End Type
Type DISPLAY_DEVICE
cb As Long
DeviceName As String * 32
DeviceString As String * 128
StateFlags As Long
StateImage As Long
DeviceID As String * 128
DeviceKey As String * 128
End Type
Type DataMonitor
HandleMonitor As Long
index As Long
Width As Long
Height As Long
WorkWidth As Long
WorkHeight As Long
AsTaskbar As Boolean
TaskBarWeight As Long
IsPrincipal As Boolean
DeviceIndex As Long
DeviceName As String
End Type
' Constantes
Const MONITORINFOF_PRIMARY As Long = &H1
Public MesMonitors() As DataMonitor
Dim x As Long
Sub ListMonitors()
x = 0
ReDim MesMonitors(1 To 1) ' Initialisation du tableau
Dim result As Long
result = EnumDisplayMonitors(0, 0, AddressOf MonitorEnumProc, 0)
MsgBox MesMonitors(1).TaskBarWeight
End Sub
' Fonction de rappel pour EnumDisplayMonitors
Function MonitorEnumProc(ByVal hMonitor As LongPtr, ByVal hdcMonitor As LongPtr, _
ByRef lprcMonitor As RECT, ByVal dwData As Long) As Long
Dim monitorInfo As monitorInfo
monitorInfo.cbSize = Len(monitorInfo)
' Incrémenter l'index et redimensionner le tableau
x = x + 1
ReDim Preserve MesMonitors(1 To x)
' Récupérer les informations du moniteur
If GetMonitorInfo(hMonitor, monitorInfo) Then
With MesMonitors(x)
.index = x
.Width = monitorInfo.rcMonitor.Right - monitorInfo.rcMonitor.Left
.Height = monitorInfo.rcMonitor.Bottom - monitorInfo.rcMonitor.Top
.WorkWidth = monitorInfo.rcWork.Right - monitorInfo.rcWork.Left
.WorkHeight = monitorInfo.rcWork.Bottom - monitorInfo.rcWork.Top
.AsTaskbar = monitorInfo.rcMonitor.Right * monitorInfo.rcMonitor.Bottom <> monitorInfo.rcWork.Right * monitorInfo.rcWork.Bottom
.IsPrincipal = (monitorInfo.dwFlags = MONITORINFOF_PRIMARY)
.HandleMonitor = hMonitor
If monitorInfo.rcMonitor.Bottom <> monitorInfo.rcWork.Bottom Then .TaskBarWeight = Abs(monitorInfo.rcMonitor.Bottom - monitorInfo.rcWork.Bottom)
If monitorInfo.rcMonitor.Right <> monitorInfo.rcWork.Right Then .TaskBarWeight = Abs(monitorInfo.rcMonitor.Right - monitorInfo.rcWork.Right)
' Obtenir le périphérique d'affichage à partir de EnumDisplayDevices
Dim displayDevice As DISPLAY_DEVICE
displayDevice.cb = Len(displayDevice)
' Nous allons utiliser l'index du moniteur pour interroger EnumDisplayDevices
If EnumDisplayDevices("", x - 1, displayDevice, 0) Then
.DeviceIndex = x
.DeviceName = Trim(displayDevice.DeviceName) ' Exemple : DISPLAY1, DISPLAY2, etc.
Else
.DeviceIndex = -1 ' Aucune information trouvée
.DeviceName = "Non trouvé"
End If
' Affichage dans le débogueur
'Debug.Print "Moniteur " & .index & " détecté :"
'Debug.Print " Dimensions totales : (" & monitorInfo.rcMonitor.Left & ", " & monitorInfo.rcMonitor.Top & ") - (" & _
monitorInfo.rcMonitor.Right & ", " & monitorInfo.rcMonitor.Bottom & ")"
End With
End If
' Continuer l'énumération
MonitorEnumProc = 1
End Function