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
|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. Dont 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 Do 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 Loop ' 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 Case DISP_CHANGE_SUCCESSFUL MsgBox "The display settings change was successful", _ vbInformation Case DISP_CHANGE_RESTART MsgBox "The computer must be restarted in order for _ the graphics mode to work", vbQuestion Case DISP_CHANGE_FAILED MsgBox "The display driver failed the specified graphics _ mode", vbCritical Case DISP_CHANGE_BADMODE MsgBox "The graphics mode is not supported", vbCritical Case DISP_CHANGE_NOTUPDATED MsgBox "Unable to write settings to the registry", vbCritical ' NB. Windows NT Only Case DISP_CHANGE_BADFLAGS 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