Im using the following code to change the display resolution:
This code works perfctly on my pc but on my friend's pc i'm having problem.
The problem is that , my friend's pc does not support higher refresh rate on 1024x 768 resolution.
So when the previous resolution is set to 800x600 @ 60 Hertz this code works fine. But if the resolution is 800x600@ 85 Hertz this code does not work.
Is there a way that the code retry to change the resoluton with lower refresh rate like 75 Htz if this fails then try 70 ..... and 60Htz and if all tries fail then give up else set the resolution at refresh rate at highest possible rate.
Code:
Option Compare Database
Option Explicit
'ghudson 08/30/2002
'I am trying to reduce the repetitive code but it is not working.
'http://www.vbcode.com/asp/showsn.asp?theID=6142
'*** These two lines to be added if OS is Windows 2000 ***
'*** in DEVMODE declaration ***
' dmPanningWidth As Long
' dmPanningHeight 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
dmPanningWidth As Long
dmPanningHeight As Long
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
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 should be numeric (e.g. 1024 or 800 or 640)
dm.dmPelsHeight = vHeight ' Height should be numeric (e.g. 768 or 600 or 480)
RetVal = ChangeDisplaySettings(dm, CDS_TEST)
If RetVal <> DISP_CHANGE_SUCCESSFUL Then
Debug.Print "Cannot change to that resolution!"
Else
RetVal = ChangeDisplaySettings(dm, CDS_UPDATEREGISTRY)
Select Case RetVal
Case DISP_CHANGE_SUCCESSFUL
Debug.Print "Resolution successfully changed!"
Case DISP_CHANGE_RESTART
Debug.Print "A reboot is necessary before the 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
This code works perfctly on my pc but on my friend's pc i'm having problem.
The problem is that , my friend's pc does not support higher refresh rate on 1024x 768 resolution.
So when the previous resolution is set to 800x600 @ 60 Hertz this code works fine. But if the resolution is 800x600@ 85 Hertz this code does not work.
Is there a way that the code retry to change the resoluton with lower refresh rate like 75 Htz if this fails then try 70 ..... and 60Htz and if all tries fail then give up else set the resolution at refresh rate at highest possible rate.