|
APIs: Détecter les résolutions graphiques disponibles |
Author(s) Dev Ashish |
|
(Q) Comment déterminer les diverses résolution d'écran et
possiblement changer pour l'une d'entre elles en cours d'exécution?
(A) Les fonctions suivantes permettent d'énumérer les diverses résolutions, de même
que de changer la résolution en cours d'exécution.
Note: Ce code ne doit pas être un
palliatif
pour s'éviter de redimensionner les formulaires de façon à mieux remplir l'espace
visuel disponible. Changer le choix que l'usager a exprimé n'est pas de bon aloi et est
généralement découragé.
Private Const CCDEVICENAME = 32
Private Const CCFORMNAME = 32
Private Const DM_PELSWIDTH = &H80000
Private Const DM_PELSHEIGHT = &H100000
Private Type DEVMODE
dmDeviceName As String * CCDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
Private Declare Function apiEnumDisplaySettings Lib "user32" _
Alias "EnumDisplaySettingsA" _
(ByVal lpszDeviceName As Long, _
ByVal iModeNum As Long, _
lpDevMode As Any) _
As Boolean
Private Declare Function apiChangeDisplaySettings Lib "user32" _
Alias "ChangeDisplaySettingsA" _
(lpDevMode As Any, _
ByVal dwflags As Long) _
As Long
Function fEnumDisplay() As Collection
Dim collRes As Collection
Dim boolRet As Boolean
Dim tDevMode As DEVMODE
Dim lngMode As Long
Set collRes = New Collection
Do
boolRet = apiEnumDisplaySettings(0&, lngMode&, tDevMode)
With tDevMode
collRes.Add .dmPelsWidth & "x" & _
.dmPelsHeight & " @ " & .dmBitsPerPel & " bit", _
lngMode & vbNullString
End With
lngMode = lngMode + 1
Loop Until boolRet = False
Set fEnumDisplay = collRes
Set collRes = Nothing
End Function
Function fChangeRes(intX As Integer, intY As Integer) As Boolean
Dim tDevMode As DEVMODE
Dim boolCanChange As Boolean
Dim boolRet As Boolean
Dim lngRet As Long, lngMode As Long
On Error GoTo Err_Handler
Do
boolRet = apiEnumDisplaySettings(0&, lngMode&, tDevMode)
With tDevMode
If .dmPelsWidth = intX And .dmPelsHeight = intY Then
boolCanChange = True
End If
End With
lngMode = lngMode + 1
Loop Until boolRet = False
If boolCanChange Then
With tDevMode
.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
.dmPelsWidth = intX
.dmPelsHeight = intY
End With
lngRet = apiChangeDisplaySettings(tDevMode, 0&)
End If
fChangeRes = boolCanChange
exit_Handler:
Exit Function
Err_Handler:
fChangeRes = False
Resume exit_Handler
End Function
|