' change the screen resolution mode
'
' returns True if the requested resolution mode is among those
' supported by the display adapter (otherwise it doesn't even
' try to change the screen resolution)
Function ChangeScreenResolution(ByVal Width As Long, ByVal Height As Long, _
ByVal NumColors As Long, Optional Frequency As Long) As Boolean
Dim lpDevMode As DEVMODE
Dim index As Long
' set the DEVMODE flags and structure size
lpDevMode.dmSize = Len(lpDevMode)
lpDevMode.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
' retrieve info on the Nth display mode, exit if no more
Do While EnumDisplaySettings(0, index, lpDevMode) > 0
' check whether this is the mode we're looking for
If lpDevMode.dmPelsWidth = Width And lpDevMode.dmPelsHeight = Height _
And 2 ^ lpDevMode.dmBitsPerPel = NumColors Then
' check that the frequency is also the one we're looking for
If Frequency = 0 Or Frequency = lpDevMode.dmDisplayFrequency Then
' try changing the resolution
If ChangeDisplaySettings(lpDevMode, CDS_FORCE) = 0 Then
' zero means success
ChangeScreenResolution = True
Exit Do
End If
End If
End If
' skip to next screen mode
index = index + 1
Loop
End Function