'*****************************************************************
' DECLARATIONS SECTION
'*****************************************************************
Option Explicit
Type RECT
x1 As Long
y1 As Long
x2 As Long
y2 As Long
End Type
' NOTE: The following declare statements are case sensitive.
Declare Function GetDesktopWindow Lib "User32" () As Long
Declare Function GetWindowRect Lib "User32" _
(ByVal hWnd As Long, rectangle As RECT) As Long
Public Type DEVMODE
dmDeviceName As String * 32
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 * 32
dmUnusedPadding As Integer
dmBitsPerPixel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
dmICMMethod As Long
dmICMIntent As Long
dmMediaType As Long
dmDitherType As Long
dmReserved1 As Long
dmReserved2 As Long
Rem dmPanningWidth As Long 'remove the Rem if your OS is Windows 2000, if needed
Rem dmPanningHeight As Long 'remove the Rem if your OS is Windows 2000, if needed
End Type
Public Declare Function EnumDisplaySettings Lib "user32.dll" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As String, ByVal iModeNum As Long, lpDevMode As DEVMODE) As Long
Public Const ENUM_CURRENT_SETTINGS = -1
Public Declare Function ChangeDisplaySettings Lib "user32.dll" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long
Public Const CDS_UPDATEREGISTRY = &H1
Public Const CDS_TEST = &H2
Public Const DISP_CHANGE_SUCCESSFUL = 0
Public Const DISP_CHANGE_RESTART = 1
'*****************************************************************
' FUNCTION: GetScreenResolution()
'
' PURPOSE:
' To determine the current screen size or resolution.
'
' RETURN:
' The current screen resolution. Typically one of the following:
' 640 x 480
' 800 x 600
' 1024 x 768
'
'*****************************************************************
Function GetScreenResolution() As String
Dim R As RECT
Dim hWnd As Long
Dim RetVal As Long
hWnd = GetDesktopWindow()
RetVal = GetWindowRect(hWnd, R)
GetScreenResolution = (R.x2 - R.x1) & "x" & (R.y2 - R.y1)
End Function
Public Function ChangeResolution(vWidth As Variant, vHeight As Variant)
On Error GoTo Err_ChangeResolution
Dim dm As DEVMODE
Dim RetVal As Long
dm.dmSize = Len(dm)
RetVal = EnumDisplaySettings(vbNullString, ENUM_CURRENT_SETTINGS, dm)
dm.dmPelsWidth = vWidth ' Width must be numeric (e.g. 1024 or 800 or 640)
dm.dmPelsHeight = vHeight ' Height must be numeric (e.g. 768 or 600 or 480)
RetVal = ChangeDisplaySettings(dm, CDS_TEST)
If RetVal <> DISP_CHANGE_SUCCESSFUL Then
Debug.Print "Unable to change to the requested resolution of " & vWidth & "x" & vHeight
Else
RetVal = ChangeDisplaySettings(dm, CDS_UPDATEREGISTRY)
Select Case RetVal
Case DISP_CHANGE_SUCCESSFUL
Debug.Print "Resolution successfully changed to " & vWidth & "x" & vHeight
Case DISP_CHANGE_RESTART
Debug.Print "A reboot is necessary before the resolution changes will take effect."
Case Else
Debug.Print "Unable to change resolution!"
End Select
End If
Exit_ChangeResolution:
Exit Function
Err_ChangeResolution:
MsgBox Err.Number, Err.Description
Resume Exit_ChangeResolution
End Function