June 22, 2018
Hot Topics:

Screen Resolutions

  • November 19, 2002
  • By Steve Anderson
  • Send Email »
  • More Articles »

First, I must emphasise that some of the constants required to do this are undocumented in the VB API viewer, but are listed below and in the sample projects.

To change the screen mode, you must enumerate the possible screen modes, as described earlier, then select the closest match. You then can use the ChangeDisplaySettings API function to actually change the screen resolution:


This API function changes the display settings on the current machine. There are two parameters that are passed which are:

lpDevMode – This acts as a pointer to the DevMode structure that contains information that you want to change. You can set the following parameters of the DevMode structure that will be changed

Meaning Description
dmBitsPerPel This is the number of colours that you want to use or the bits per pixel as I explained earlier on in this article, which can either be 4, 6, 8, 16, 24 or 32 depending on the modes supported.
dmPelsWidth This is the width of the screen in pixels or otherwise known as the x-axis of the screen.
dmPelsHeight This is the height of the screen in pixels or otherwise known as the y-axis of the screen.

This function demonstrates how to change the screen resolution. It enumerates through the available modes, checking each one. When the correct one is found, it is selected. Don’t forget to add the earlier declarations to the ones here:

Private Declare Function ChangeDisplaySettings _
    Lib "user32" _
Alias "ChangeDisplaySettingsA" (lpDevMode As Any, _
ByVal dwflags As Long) As Long

Public Sub ChangeScreenSettings(lWidth As Integer, _
lHeight As Integer, lColors As Integer)

Dim tDevMode As DEVMODE, lTemp As Long, lIndex As Long
' Declare variables
lIndex = 0

lTemp = EnumDisplaySettings(0&, lIndex, tDevMode)
' Call the API function
If lTemp = 0 Then Exit Do
' If there is no more data or an error occurs
' then return 0 and exit do
lIndex = lIndex + 1
' Increase the index to be enumerated
With tDevMode
If .dmPelsWidth = lWidth And .dmPelsHeight = lHeight _
And .dmBitsPerPel = lColors Then
lTemp = ChangeDisplaySettings(tDevMode, CDS_UPDATEREGISTRY)
Exit Do
End If

End With
' Set the new data
' Change the display type. This depends on the paramter used
' It can either be:
' 0 - Dynamic change if possible
' CDS_UPDATEREGISTRY - Dynamically change if possible
' and if not possible then update registry for change
' on the next boot-up
' CDS_TEST - Test the new settings

Select Case lTemp ' Check for errors
MsgBox "The display settings change was successful", _

MsgBox "The computer must be restarted in order for _
the graphics mode to work", vbQuestion

MsgBox "The display driver failed the specified graphics _
mode", vbCritical

MsgBox "The graphics mode is not supported", vbCritical

MsgBox "Unable to write settings to the registry", vbCritical
' NB. Windows NT Only

MsgBox "An invalid set of flags was passed in", vbCritical

End Select

End Sub

The functions here are demonstrated in the ChangeDispMode demo project.

Page 5 of 6

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date