Declare Function ChangeDisplaySettings Lib "user32.dll" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long
ChangeDisplaySettings changes the current display settings. This function can change the current screen resolution and color depth, among other things. Typically, a call to EnumDisplaySettings should preceed this function, in order to adjust only the desired settings.
The function returns one of the following flags:
When specifying zero for lpDevMode, use the expression ByVal CLng(0).
Const CDS_UPDATEREGISTRY = &H1
Const CDS_TEST = &H2
Const CDS_FULLSCREEN = &H4
Const CDS_GLOBAL = &H8
Const CDS_SET_PRIMARY = &H10
Const CDS_RESET = &H40000000
Const CDS_SETRECT = &H20000000
Const CDS_NORESET = &H10000000
Const DISP_CHANGE_SUCCESSFUL = 0
Const DISP_CHANGE_RESTART = 1
Const DISP_CHANGE_FAILED = -1
Const DISP_CHANGE_BADMODE = -2
Const DISP_CHANGE_NOTUPDATED = -3
Const DISP_CHANGE_BADFLAGS = -4
Const DISP_CHANGE_BADPARAM = -5
Change the current screen resolution to 800x600 without changing the color depth and save the changes to the registry. First test to make sure that the hardware supports the new resolution. If a reboot is necessary, inform the user. To use this example, place a command button named Command1 of a form window.
' This code is licensed according to the terms and conditions listed here.
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
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
' The following only appear in Windows 95, 98, 2000
dmICMMethod As Long
dmICMIntent As Long
dmMediaType As Long
dmDitherType As Long
dmReserved1 As Long
dmReserved2 As Long
' The following only appear in Windows 2000
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
' *** Place the following code inside the form window.
Private Sub Command1_Click()
Dim dm As DEVMODE ' display settings
Dim retval As Long ' return value
' Initialize the structure that will hold the settings.
dm.dmSize = Len(dm)
' Get the current display settings.
retval = EnumDisplaySettings(vbNullString, ENUM_CURRENT_SETTINGS, dm)
' Change the resolution settings to 800x600.
dm.dmPelsWidth = 800
dm.dmPelsHeight = 600
' Test to make sure the changes are possible.
retval = ChangeDisplaySettings(dm, CDS_TEST)
If retval <> DISP_CHANGE_SUCCESSFUL Then
Debug.Print "Cannot change to that resolution!"
Else
' Change and save to the new settings.
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
End Sub
Back to the Function list.
Back to the Reference section.
Last Modified: January 21, 2001
This page is copyright © 2001 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/c/changedisplaysettings.html