Change screen resolution and color depth
By: Lorenzo Dalla Vecchia Date: 18/05/2001

This tip describes how to chenge the screen resolution (width and height of the Desktop in pixels) with Visual Basic code.
This is useful when you have created a program that fits in one resolution and you want to be sure it runs on that resolution. This is especially true for games.

Another feature of the following code is that you can change color depth (the number of color the screen is able to display): it is not pleasant to see your colorful 32-bit color game run at 8-bit depth.

As always, cut and paste the following API and type delarations into a basic module.

Declare Function EnumDisplaySettings Lib "user32" _
Alias "EnumDisplaySettingsA" _
(ByVal lpszDeviceName As Long, _
ByVal iModeNum As Long, _
lpDevMode As Any) As Boolean
Declare Function ChangeDisplaySettings Lib "user32" _
Alias "ChangeDisplaySettingsA" _
(lpDevMode As Any, ByVal dwFlags As Long) As Long

Public Const CCDEVICENAME = 32
Public Const CCFORMNAME = 32
Public Const DM_BITSPERPEL = &H40000
Public Const DM_PELSWIDTH = &H80000
Public Const DM_PELSHEIGHT = &H100000
Public Const CDS_UPDATEREGISTRY = &H1
Public Const CDS_TEST = &H4
Public Const DISP_CHANGE_SUCCESSFUL = 0
Public Const DISP_CHANGE_RESTART = 1

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

After you have copied the declarations, use the following code (in a command button, for example) to change the resolution and/or the color depth:

Dim DevM As DEVMODE
Dim RetVal As Long
Dim Answer As Integer

'Fill DevM with information
RetVal = EnumDisplaySettings(0&, 0&, DevM)

'New mode information

DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or _
DM_BITSPERPEL
DevM.dmPelsWidth = with      'Screen width in pixels
DevM.dmPelsHeight = height  'Screen height in pixels
DevM.dmBitsPerPel = depth    'Color depth in bits

'Test if the mode is supported
RetVal = ChangeDisplaySettings(DevM, CDS_TEST)
Select Case RetVal
    Case DISP_CHANGE_RESTART
        'A reboot is required
        MsgBox "You have to restart Windows."
    Case DISP_CHANGE_SUCCESSFUL
        'OK, now actually change the mode
        RetVal = ChangeDisplaySettings(DevM, _

         CDS_UPDATEREGISTRY)
        MsgBox "New screen mode applied."
    Case Else
        'Mode not supported
        MsgBox "Can't proceed: the mode is not supported."
End Select

with and height are told in pixels.
For
depth, enter the number in bits; the number colors is obtained with 2depth. For example, 216=65,536.
If you don't need to change the color depth, you can substitute part of the above code with the following:

Width Height
640 480
800 600
1024 768
1280 1024
1600 1200
Bits # colors
4 16
8 256
16 65,536
24 16,777,216
32 4,294,967,296
'New mode information
DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
DevM.dmPelsWidth = with      'Screen width in pixels
DevM.dmPelsHeight = height  'Screen height in pixels

Or, if you wanter to change the color depth only...

'New mode information
DevM.dmFields = DM_BITSPERPEL
DevM.dmBitsPerPel = depth    'Color depth in bits

The group of the three values (with, height, depth) constitute a display mode. Not all possible combinations are supported by monitors and video cards. The code of the tip also takes care of testing the mode before applying it. Some computers may have to reboot before the changes can take effect: the code informs you about that. The tables on the right lists some common resolutions and color depths: keep in mind that not all depths are valid for each resolution.

Click Here!


Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: www.inwind.it