Screen Resolution

Is there any way to change the screen resolution through code in visual basic?
DavrosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JuiletteCommented:
Display Properties (Settings):  
    rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,3
0
watyCommented:
Here is the link to do that :
http://www.mvps.org/vbnet/code/system/displaychange.htm

Option Explicit

Public Declare Function EnumDisplaySettings Lib "user32" _
    Alias "EnumDisplaySettingsA" _
   (ByVal lpszDeviceName As Long, _
    ByVal iModeNum As Long, _
    lpDevMode As Any) As Boolean
         
Public Declare Function GetDeviceCaps Lib "gdi32" _
   (ByVal hdc As Long, _
    ByVal nIndex As Long) As Long

Public Declare Function ChangeDisplaySettings Lib "user32" _
    Alias "ChangeDisplaySettingsA" _
   (lpDevMode As Any, _
    ByVal dwflags As Long) As Long
         
Public Declare Function SetMenuDefaultItem Lib "user32" _
   (ByVal hMenu As Long, _
    ByVal uItem As Long, _
    ByVal fByPos As Long) As Long
 
Public Declare Function GetMenu Lib "user32" _
   (ByVal hWnd As Long) As Long
   
Public Declare Function GetSubMenu Lib "user32" _
   (ByVal hMenu As Long, _
   ByVal nPos As Long) As Long
         
Public Const LOGPIXELSX As Long = 88
Public Const LOGPIXELSY As Long = 90
Public Const BITSPIXEL As Long = 12
Public Const HORZRES As Long = 8
Public Const VERTRES As Long = 10

Public Const CCDEVICENAME As Long = 32
Public Const CCFORMNAME As Long = 32

Public Const DM_GRAYSCALE As Long = &H1
Public Const DM_INTERLACED As Long = &H2

Public Const DM_BITSPERPEL As Long = &H40000
Public Const DM_PELSWIDTH As Long = &H80000
Public Const DM_PELSHEIGHT As Long = &H100000
Public Const DM_DISPLAYFLAGS As Long = &H200000

Public Const CDS_UPDATEREGISTRY As Long = &H1
Public Const CDS_TEST As Long = &H2
Public Const CDS_FULLSCREEN As Long = &H4
Public Const CDS_GLOBAL As Long = &H8
Public Const CDS_SET_PRIMARY As Long = &H10
Public Const CDS_NORESET As Long = &H10000000
Public Const CDS_SETRECT As Long = &H20000000
Public Const CDS_RESET As Long = &H40000000
Public Const CDS_FORCE As Long = &H80000000

'Return values for ChangeDisplaySettings
'Public Const DISP_CHANGE_SUCCESSFUL = 0
'Public Const DISP_CHANGE_RESTART = 1
'Public Const DISP_CHANGE_FAILED = -1
'Public Const DISP_CHANGE_BADMODE = -2
'Public Const DISP_CHANGE_NOTUPDATED = -3
'Public Const DISP_CHANGE_BADFLAGS = -4
'Public Const DISP_CHANGE_BADPARAM = -5

Public 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



Option Explicit

'vars set in load
Dim currHRes As Long
Dim currVRes As Long
Dim currBPP As Long

'var set in mnuModes
Dim currMenuItem As Long

'array of valid resolutions & colour depths
Dim resArray() As Long
   
'const for the members of the array
'i.e. resArray(resWidth, Index) = 1024
'i.e. resArray(resHeight, Index) = 768
'i.e. resArray(resDepth, Index)= 16  'Bits per pixel
Const resWidth = 1
Const resHeight = 2
Const resDepth = 3


Private Sub Form_Load()

  'retrieves the current screen resolution for
  'later comparison against DEVMODE values in
  'CompareSettings.
   currHRes = GetDeviceCaps(hdc, HORZRES)
   currVRes = GetDeviceCaps(hdc, VERTRES)
   currBPP = GetDeviceCaps(hdc, BITSPIXEL)
   
   Dim maxItems As Long
   InitializeDisplayMenu maxItems
   FinalizeDisplayMenu maxItems
   
End Sub


Private Sub FinalizeDisplayMenu(maxItems As Long)

  'This adds a separator and a final menu item,
  'providing the ability to open the control panel
  'display settings page from the app.

   If maxItems > 0 Then
   
      Dim hMenu As Long
      Dim r As Long
     
     'add the separator
      maxItems = maxItems + 1
      Load mnuModes(maxItems)
      mnuModes(maxItems).Caption = "-"
     
     'add the final item
      maxItems = maxItems + 1
      Load mnuModes(maxItems)
      mnuModes(maxItems).Caption = "Show Display Settings"
     
     'finally, bold the newly-added menuitem
      hMenu = GetSubMenu(GetMenu(Me.hWnd), 0)
      Call SetMenuDefaultItem(hMenu, maxItems - 1, True)
   
   End If
       
End Sub

Private Sub InitializeDisplayMenu(maxItems As Long)

   Dim DM As DEVMODE
   Dim dMode As Long
   
  '36 should be enough to hold your settings.
  'It's trimmed back at the end of this routine.
   ReDim resArray(1 To 3, 0 To 35)
   
  'set the DEVMODE flags and structure size
   DM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
   DM.dmSize = LenB(DM)
   
  'The first mode is 0
   dMode = 0
     
  'call the API to retrieve the values for the
  'specified dMode
   Do While EnumDisplaySettings(0&, dMode, DM) > 0
   
     'if the BitsPerPixel is greater than 4
     '(16 colours), then add the item to a menu
      If DM.dmBitsPerPel >= 4 Then
         Call MenuAdd(DM, resArray(), maxItems)
      End If
     
     'increment and call again. Continue until
     'EnumDisplaySettings returns 0 (no more settings)
      dMode = dMode + 1
   
   Loop
   
  'trim back the resArray to fit the number of actual entries.
   ReDim Preserve resArray(1 To 3, 0 To maxItems)
   
End Sub


Private Function CompareSettings(DM As DEVMODE) As Long
   
  'compares the current screen resolution with
  'the current DEVMODE values.   Returns TRUE if
  'the horizontal and vertical resolutions, and
  'the bits per pixel colour depth, are the same.
 
   CompareSettings = (DM.dmBitsPerPel = currBPP) And _
                      DM.dmPelsHeight = currVRes And _
                      DM.dmPelsWidth = currHRes
   
End Function


Private Sub MenuAdd(DM As DEVMODE, resArray() As Long, mnuCount As Long)
 
   Dim mType As String
   
  'used to determine when the colour depth has
  'changed, so we can add a separator to the menu.
   Static lastBitsPerPel As Long
   
  'select the appropriate text string based on
  'the colour depth
   Select Case DM.dmBitsPerPel
      Case 4:      mType = "16 Color"
      Case 8:      mType = "256 Color"
      Case 16:     mType = "High Color"
      Case 24, 32: mType = "True Color"
   End Select

  'if this is the first item, we can't load the menu
  'array item, and it will not require a separator.
   If mnuCount > 0 Then
   
     'load a new menu item to the array
      Load mnuModes(mnuCount)
   
     'determine if the colour depth has changed. If so,
     'make the caption a separator, and load a new item
     'to hold the item.
      If lastBitsPerPel <> DM.dmBitsPerPel Then
     
         mnuModes(mnuCount).Caption = "-"
         mnuCount = mnuCount + 1
         Load mnuModes(mnuCount)
     
      End If
   End If
   
  'create the menu caption
   mnuModes(mnuCount).Caption = DM.dmPelsWidth & "x" & _
                                DM.dmPelsHeight & "  [" & _
                                DM.dmBitsPerPel & " bit " & _
                                mType & "]"
   
  'see if this is the current resolution,
  'and if so, check the menu item
   mnuModes(mnuCount).Checked = CompareSettings(DM)
   If mnuModes(mnuCount).Checked Then currMenuItem = mnuCount
   
   resArray(resWidth, mnuCount) = DM.dmPelsWidth
   resArray(resHeight, mnuCount) = DM.dmPelsHeight
   resArray(resDepth, mnuCount) = DM.dmBitsPerPel
   
  'save the current DEVMODE value for depth
  'and increment the menu item count, ready for
  'the next call
   lastBitsPerPel = DM.dmBitsPerPel
   mnuCount = mnuCount + 1
   
End Sub


Private Sub cmdLoadMenu_Click()

   Dim maxItems As Long
   
   InitializeDisplayMenu maxItems
   cmdLoadMenu.Enabled = False
   
   FinalizeDisplayMenu maxItems
   
End Sub


Private Sub mnuModes_Click(Index As Integer)

   Dim DM As DEVMODE
   
   Select Case Index
   
      Case mnuModes.Count
     
        'show the display control panel
         Call Shell("rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,3", 1)
     
      Case Else
       
        'change the current resolution, no prompting
        'BE CAREFUL .. you could set your system to a
        'setting which renders the display difficult to read.
       
         With DM
         
            .dmPelsWidth = resArray(resWidth, Index)
            .dmPelsHeight = resArray(resHeight, Index)
            .dmBitsPerPel = resArray(resDepth, Index)
            .dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
            .dmSize = LenB(DM)
         End With
         
         If ChangeDisplaySettings(DM, CDS_FORCE) <> 0 Then
         
             MsgBox "Error! Perhaps your hardware is not up to the task?"
             
         End If
         
        'indicate the current menu selection
         mnuModes(currMenuItem).Checked = False
         mnuModes(Index).Checked = True
         currMenuItem = Index
   
   End Select

End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
watyCommented:
Also, this could be useful for you :
http://www.mvps.org/vbnet/code/system/systemparaminfo.htm

http://www.mvps.org/vbnet/code/system/displayenum.htm




' #VBIDEUtils#************************************************************
' * Programmer Name  : Mike Dixon
' * Web Site         : www.microsoft.com
' * E-Mail           : mikedix@microsoft.com
' * Date             : 05/10/1999
' * Time             : 11:18
' **********************************************************************
' * Comments         : Support Multi-Monitor up to 9 under Win98
' *
' *
' **********************************************************************
' --------------------------------------------------------------------------
'               Copyright (C) 1998 Microsoft Corporation                   '
' --------------------------------------------------------------------------
' You have a royalty-free right to use, modify, reproduce and distribute   '
' the Sample Application Files (and/or any modified version) in any way    '
' you find useful, provided that you agree that Microsoft has no warranty, '
' obligations or liability for any Sample Application Files.               '
' --------------------------------------------------------------------------
' Written by Mike Dixon (mikedix@microsoft.com)                            '
' --------------------------------------------------------------------------

'Virtual Desktop sizes
Const SM_XVIRTUALSCREEN = 76    'Virtual Left
Const SM_YVIRTUALSCREEN = 77    'Virtual Top
Const SM_CXVIRTUALSCREEN = 78   'Virtual Width
Const SM_CYVIRTUALSCREEN = 79   'Virtual Height

Const SM_CMONITORS = 80         'Get number of monitors
Const SM_SAMEDISPLAYFORMAT = 81

'Constants for the return value when finding a monitor
Const MONITOR_DEFAULTTONULL = &H0       'If the monitor is not found, return 0
Const MONITOR_DEFAULTTOPRIMARY = &H1    'If the monitor is not found, return the primary monitor
Const MONITOR_DEFAULTTONEAREST = &H2    'If the monitor is not found, return the nearest monitor
Const MONITORINFOF_PRIMARY = 1

'Rectangle structure, for determining
'monitors at a given position
Private Type RECT
   Left    As Long
   Top     As Long
   Right   As Long
   Bottom  As Long
End Type

'Structure for the position of a monitor
Private Type tagMONITORINFO
   cbSize      As Long 'Size of structure
   rcMonitor   As RECT 'Monitor rect
   rcWork      As RECT 'Working area rect
   dwFlags     As Long 'Flags
End Type

Public Monitors As New Collection

Private Declare Function GetSystemMetrics Lib "user32" ( _
   ByVal nIndex As Long) As Long

'These API's are not present in Pre Windows 98 and
'Pre Windows NT 5 operating systems, you will need
'to trap for errors when using them.
'(Err.Number 453 Can't find DLL entry point...
Private Declare Function GetMonitorInfo Lib "user32" _
   Alias "GetMonitorInfoA" ( _
   ByVal hMonitor As Long, _
   MonInfo As tagMONITORINFO) As Long

Private Declare Function MonitorFromWindow Lib "user32" ( _
   ByVal hwnd As Long, _
   dwFlags As Long) As Long

Private Declare Function MonitorFromRect Lib "user32" ( _
   rc As RECT, _
   ByVal dwFlags As Long) As Long

'==================================================================================================
'Public Members
'==================================================================================================
Private Sub Class_Initialize()
   'Load the monitors collection
   Refresh
End Sub

Public Property Get DesktopLeft() As Long
   DesktopLeft = GetSystemMetrics2(SM_XVIRTUALSCREEN, 0)
End Property

Public Property Get DesktopTop() As Long
   DesktopTop = GetSystemMetrics2(SM_YVIRTUALSCREEN, 0)
End Property

Public Property Get DesktopWidth() As Long
   DesktopWidth = GetSystemMetrics2(SM_CXVIRTUALSCREEN, Screen.Width  Screen.TwipsPerPixelX)
End Property

Public Property Get DesktopHeight() As Long
   DesktopHeight = GetSystemMetrics2(SM_CYVIRTUALSCREEN, Screen.Height  Screen.TwipsPerPixelY)
End Property

Public Function GetMonitorFromWindow(hwnd As Long, dwFlags As Long) As Long
   '=====================================================
   'Returns a monitor handle that the Window (hwnd) is in
   '=====================================================
   Dim lReturn As Long

   On Error GoTo GetMonitorFromWindow_Err
   lReturn = MonitorFromWindow(hwnd, dwFlags)
   GetMonitorFromWindow = lReturn
   Exit Function
GetMonitorFromWindow_Err:
   If Err.Number = 453 Then
      'Non-Multimonitor OS, return -1
      GetMonitorFromWindow = -1
   End If
End Function

Public Function CenterFormOnMonitor(FormToCenter As Form, Optional ReferenceForm As Variant) As Boolean
   '====================================================================
   'Centers the FormToCenter on the monitor that the ReferenceForm is on
   'or the primary monitor if the ReferenceForm is ommited
   '====================================================================
   Dim lMonitor        As Long
   Dim lReturn         As Long
   Dim MonitorInfo     As tagMONITORINFO
   Dim lMonitorWidth   As Long
   Dim lMonitorHeight  As Long

   On Error GoTo CenterFormOnMonitor_Err

   'Get the handle to the monitor that the reference form is on
   If IsMissing(ReferenceForm) Then
      lMonitor = GetMonitorFromXYPoint(1, 1, MONITOR_DEFAULTTOPRIMARY)
   Else
      lMonitor = GetMonitorFromWindow(ReferenceForm.hwnd, MONITOR_DEFAULTTOPRIMARY)
   End If

   'If we get a valid lMonitor
   If lMonitor Then

      'init the structure
      MonitorInfo.cbSize = Len(MonitorInfo)

      'Get the monitor information
      lReturn = GetMonitorInfo(lMonitor, MonitorInfo)
      'If the Call does not fail then center the form over that monitor
      If lReturn Then
         With MonitorInfo
            lMonitorWidth = (.rcWork.Right - .rcWork.Left) * Screen.TwipsPerPixelX
            lMonitorHeight = (.rcWork.Bottom - .rcWork.Top) * Screen.TwipsPerPixelY
            FormToCenter.Move ((lMonitorWidth - FormToCenter.Width)  2) + .rcMonitor.Left * Screen.TwipsPerPixelX, ((lMonitorHeight - FormToCenter.Height)  2) + MonitorInfo.rcMonitor.Top * Screen.TwipsPerPixelX
         End With
      End If
   Else
      'There was not a monitor found, center on default screen
      FormToCenter.Move (Screen.Width - FormToCenter.Width)  2, (Screen.Height - FormToCenter.Height)  2
   End If
   Exit Function
CenterFormOnMonitor_Err:
   If Err.Number = 453 Then
      'Non-Multimonitor OS
      FormToCenter.Move (Screen.Width - FormToCenter.Width)  2, (Screen.Width - FormToCenter.Width)  2
   End If
End Function

Public Function GetMonitorFromXYPoint(x As Long, y As Long, dwFlags As Long) As Long
   '==========================================
   'Gets a monitor handle from the xy point
   'Workaround for the GetMonitorFromPoint API
   'is to use the GetMonitorFromRect API and
   'build a rect instead
   '==========================================
   Dim lReturn As Long
   Dim rcRect As RECT

   'Transfer the x y into a rect 1 pixel square
   With rcRect
      .Top = y
      .Left = x
      .Right = x + 1
      .Bottom = y + 1
   End With
   On Error Resume Next
   lReturn = MonitorFromRect(rcRect, dwFlags)
   If Err.Number = 0 Then
      GetMonitorFromXYPoint = lReturn
   Else
      GetMonitorFromXYPoint = -1
   End If
End Function

Public Sub Refresh()
   '=====================================================
   'Iterate through the Virtual Desktop and enumerate the
   'Monitors that intersect each 640x480 grid section
   '=====================================================
   Dim lMonitors       As Long
   Dim cMonitor        As clsMonitor
   Dim lLoop           As Long
   Dim lLoop2          As Long
   Dim lMonitor        As Long

   On Error GoTo Refresh_Err

   Set Me.Monitors = Nothing

   'Find Out How Many monitors there are
   lMonitors = GetSystemMetrics(SM_CMONITORS)

   If lMonitors = 0 Then
      'Non multimonitor OS, just do the screen size
      ClearMonitorsCollection
      Set cMonitor = New clsMonitor
      With cMonitor
         .Handle = 0
         .Bottom = Screen.Height  Screen.TwipsPerPixelY
         .Left = 0
         .Right = Screen.Width  Screen.TwipsPerPixelX
         .Top = 0
         .WorkBottom = .Bottom
         .WorkLeft = 0
         .WorkRight = .Right
         .WorkTop = 0
         .Width = .Right
         .Height = .Bottom
      End With
      'Add the monitor to the monitors collection
      Monitors.Add Item:=cMonitor, Key:=CStr(0)
   Else

      'Loop through an imaginary grid of 640x480 cells across the virtual desktop
      'testing each for the monitor it is on, then try to add that monitor to the
      'collection, if it fails, it is a duplicate, so just keep going.
      For lLoop = DesktopTop To DesktopHeight Step 480
         For lLoop2 = DesktopLeft To DesktopWidth Step 640
            lMonitor = GetMonitorFromXYPoint(lLoop2 + 320, lLoop + 240, 0)
            If lMonitor <> 0 Then
               Set cMonitor = New clsMonitor
               Call GetMonitorInformation(lMonitor, cMonitor)
               Monitors.Add Item:=cMonitor, Key:=CStr(lMonitor)
            End If
         Next
      Next
   End If
   Exit Sub
Refresh_Err:
   'Duplicate in the collection, so
   'just ignore it and look for the next one
   If Err.Number = 457 Then Resume Next
End Sub

Public Function ShowMonitorDialog(Prompt As String, Caption As String, Optional OwnerForm As Variant) As Long
   '===========================================
   'Shows the Monitor Selection Dialog,
   'returns a selected monitor or 0 if canceled
   '===========================================
   Load frmMonitor
   With frmMonitor
      Set .cMonitorClass = Me
      If IsMissing(OwnerForm) Then
         'The form will be centered on the default (primary) monitor
      Else
         'The form will be centered on the monitor that Ownerform is on
         .Owner = OwnerForm
      End If
      .DialogCaption = Caption
      .Prompt = Prompt
      .ShowDialog
      ShowMonitorDialog = .DialogResult
   End With
   Unload frmMonitor
   Set frmMonitor = Nothing
End Function

'==================================================================================================
'Private Members
'==================================================================================================
Private Function GetSystemMetrics2(lItem As Long, lDefault As Long) As Long
   '===============================================
   'Calls GetSystemMetrics if multi-monitor capable
   'Otherwise return the default value passed in
   '===============================================
   If GetSystemMetrics(SM_CMONITORS) = 0 Then
      'No multi monitor, return default
      GetSystemMetrics2 = lDefault
   Else
      'Get the desired metric
      GetSystemMetrics2 = GetSystemMetrics(lItem)
   End If
End Function

Private Function GetMonitorInformation(hMonitor As Long, cMon As clsMonitor) As Long
   '======================================================
   'Fills in the cMon class passed in with the information
   '======================================================
   Dim MonitorInfo As tagMONITORINFO
   Dim lReturn     As Long
   Dim lMonitor    As Long

   On Error GoTo GetMonitorInformation_Err
   MonitorInfo.cbSize = Len(MonitorInfo)
   lReturn = GetMonitorInfo(hMonitor, MonitorInfo)
   With cMon
      .Handle = hMonitor
      .Left = MonitorInfo.rcMonitor.Left
      .Right = MonitorInfo.rcMonitor.Right
      .Top = MonitorInfo.rcMonitor.Top
      .Bottom = MonitorInfo.rcMonitor.Bottom

      .WorkLeft = MonitorInfo.rcWork.Left
      .WorkRight = MonitorInfo.rcWork.Right
      .WorkTop = MonitorInfo.rcWork.Top
      .WorkBottom = MonitorInfo.rcWork.Bottom

      .Height = MonitorInfo.rcMonitor.Bottom - MonitorInfo.rcMonitor.Top
      .Width = MonitorInfo.rcMonitor.Right - MonitorInfo.rcMonitor.Left
   End With
   GetMonitorInformation = lReturn
   Exit Function
GetMonitorInformation_Err:
   If Err.Number = 453 Then
      'Non-Multimonitor OS, return -1
      GetMonitorInformation = -1
   End If
End Function

Private Sub ClearMonitorsCollection()
   '==============================
   'Clears the monitors collection
   '==============================
   Dim cMonitors   As clsMonitor
   Dim lCount      As Long
   Dim lLoop       As Long

   lCount = Monitors.Count
   On Error Resume Next
   For lLoop = 0 To lCount Step -1
      Monitors.Remove lLoop
   Next
End Sub
0
JuiletteCommented:
'change the screen resolution of a computer
'this code changes it to 480 X 640  can be adjusted.

'bas module code

Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
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 typDevMODE
    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

Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lptypDevMode As Any) As Boolean
Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lptypDevMode As Any, ByVal dwFlags As Long) As Long
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long


'        for event code
'''''''''''''''''''''''''''''''''''''''''''''''

Dim typDevM As typDevMODE
Dim lngResult As Long
Dim intAns    As Integer

' Retrieve info about the current graphics mode
' on the current display device.
lngResult = EnumDisplaySettings(0, 0, typDevM)

' Set the new resolution. Don't change the color
' depth so a restart is not necessary.
With typDevM
    .dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
    .dmPelsWidth = 640  'ScreenWidth (640,800,1024, etc)
    .dmPelsHeight = 480 'ScreenHeight (480,600,768, etc)
End With

' Change the display settings to the specified graphics mode.
lngResult = ChangeDisplaySettings(typDevM, CDS_TEST)
Select Case lngResult
    Case DISP_CHANGE_RESTART
        intAns = MsgBox("You must restart your computer to apply these changes." & _
            vbCrLf & vbCrLf & "Do you want to restart now?", _
            vbYesNo + vbSystemModal, "Screen Resolution")
        If intAns = vbYes Then Call ExitWindowsEx(EWX_REBOOT, 0)
    Case DISP_CHANGE_SUCCESSFUL
        Call ChangeDisplaySettings(typDevM, CDS_UPDATEREGISTRY)
        MsgBox "Screen resolution changed", vbInformation, "Resolution Changed"
    Case Else
        MsgBox "Mode not supported", vbSystemModal, "Error"
End Select
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.