VB Code:
  1. Option Explicit
  2. Const WM_DISPLAYCHANGE = &H7E
  3. Const HWND_BROADCAST = &HFFFF&
  4.  
  5. Const CCDEVICENAME = 32
  6. Const CCFORMNAME = 32
  7. Const DM_BITSPERPEL = &H40000
  8. Const DM_PELSWIDTH = &H80000
  9. Const DM_PELSHEIGHT = &H100000
  10. Const CDS_UPDATEREGISTRY = &H1
  11. Const CDS_TEST = &H4
  12. Const DISP_CHANGE_SUCCESSFUL = 0
  13. Const DISP_CHANGE_RESTART = 1
  14. Const BITSPIXEL = 12
  15.  
  16. Private Type DEVMODE
  17. dmDeviceName As String * CCDEVICENAME
  18. dmSpecVersion As Integer
  19. dmDriverVersion As Integer
  20. dmSize As Integer
  21. dmDriverExtra As Integer
  22. dmFields As Long
  23. dmOrientation As Integer
  24. dmPaperSize As Integer
  25. dmPaperLength As Integer
  26. dmPaperWidth As Integer
  27. dmScale As Integer
  28. dmCopies As Integer
  29. dmDefaultSource As Integer
  30. dmPrintQuality As Integer
  31. dmColor As Integer
  32. dmDuplex As Integer
  33. dmYResolution As Integer
  34. dmTTOption As Integer
  35. dmCollate As Integer
  36. dmFormName As String * CCFORMNAME
  37. dmUnusedPadding As Integer
  38. dmBitsPerPel As Integer
  39. dmPelsWidth As Long
  40. dmPelsHeight As Long
  41. dmDisplayFlags As Long
  42. dmDisplayFrequency As Long
  43. End Type
  44.  
  45. Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
  46. Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long
  47. Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
  48. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  49. Dim OldX As Long, OldY As Long, nDC As Long
  50. Sub ChangeRes(X As Long, Y As Long, Bits As Long)
  51. Dim DevM As DEVMODE, ScInfo As Long, erg As Long, an As VbMsgBoxResult
  52. 'Get the info into DevM
  53. erg = EnumDisplaySettings(0&, 0&, DevM)
  54. 'This is what we're going to change
  55. DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
  56. DevM.dmPelsWidth = X 'ScreenWidth
  57. DevM.dmPelsHeight = Y 'ScreenHeight
  58. DevM.dmBitsPerPel = Bits '(can be 8, 16, 24, 32 or even 4)
  59. 'Now change the display and check if possible
  60. erg = ChangeDisplaySettings(DevM, CDS_TEST)
  61. 'Check if succesfull
  62. Select Case erg&
  63.  
  64. Case DISP_CHANGE_SUCCESSFUL
  65. erg = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
  66. ScInfo = Y * 2 ^ 16 + X
  67. 'Notify all the windows of the screen resolution change
  68. SendMessage HWND_BROADCAST, WM_DISPLAYCHANGE, ByVal Bits, ByVal ScInfo
  69. Case Else
  70. MsgBox "Mode not supported", vbOKOnly + vbSystemModal, "Error"
  71. End Select
  72.  
  73. End Sub
  74.  
  75. Private Sub Form_Load()
  76. Dim nDC As Long
  77. 'retrieve the screen's resolution
  78. OldX = Screen.Width / Screen.TwipsPerPixelX
  79. OldY = Screen.Height / Screen.TwipsPerPixelY
  80. 'Create a device context, compatible with the screen
  81.  
  82. 'Change the screen's resolution
  83. ChangeRes 640, 480, GetDeviceCaps(Me.hdc, BITSPIXEL)
  84. End Sub
  85.  
  86. Private Sub Form_Unload(Cancel As Integer)
  87. 'restore the screen resolution
  88. ChangeRes OldX, OldY, GetDeviceCaps(nDC, BITSPIXEL)
  89. 'delete our device context
  90. End Sub