Cambiare la Risoluzione Video

Un grosso problema per molti programmatori VB č come cambiare la risoluzione dello schermo,  anche perche nel visualizzatore delle Api le variabili per EnumDisplaySettings e ChangeDisplaySettings mancano.

Codice da inserire in un modulo Bas:

Option Explicit

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

Declare Function ExitWindowsEx Lib "user32" _
(ByVal uFlags As Long, ByVal dwReserved As Long) As Long

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 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

Public Sub CambiaRisoluzione(larghezza As Integer, altezza As Integer, profondita As Integer)
Dim DevM As DEVMODE
Dim erg&
Dim an As Integer

'Ottiene le informazioni da DevM
erg& = EnumDisplaySettings(0&, 0&, DevM)

'Per cambiare la profonditą, dovrebbe essere necessario
'il riavvio di Windows

DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
DevM.dmPelsWidth = larghezza
'ScreenWidth
DevM.dmPelsHeight = altezza
'ScreenHeight
DevM.dmBitsPerPel = profondita
'(dovrebbe essere 8, 16, 32 o almeno 4)

'Ora cambia la risoluzione e controlla che sia possibile farlo
erg& = ChangeDisplaySettings(DevM, CDS_TEST)

'Controlla se funziona
Select Case erg&
    Case DISP_CHANGE_RESTART
        an = MsgBox("Vuoi riavviare il Sistema?", vbYesNo + vbSystemModal, "Info")
        If an = vbYes Then
            erg& = ExitWindowsEx(EWX_REBOOT, 0&)
        End If
    Case DISP_CHANGE_SUCCESSFUL
        erg& = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
        MsgBox "Modifica ok", vbOKOnly + vbSystemModal, "Eseguito!"
    Case Else
        MsgBox "Modalitą non supportata", vbOKOnly + vbSystemModal, "Errore"
End Select
End Sub

Per provare la funzionalitą di questo modulo, nel Form inseriamo due ListBox ed un CommandButton.
Ed infine inseriamo, nel codice del Form, questo esempio:

Option Explicit

Private Sub Command1_Click()
Dim larghezza As Integer
Dim altezza As Integer
Dim profondita As Integer
Dim sep As Integer
Dim temp As String

temp = List1.List(List1.ListIndex)
sep = InStr(List1.List(List1.ListIndex), "x")
'se č stato selezionato un elemento della List1 prosegue.
If sep > 0 Then
    altezza = Val(Left$(temp, sep - 1))
    larghezza = Val(Mid$(temp, sep + 1))
    profondita = Val(List2.List(List2.ListIndex))
    'se č stato selezionato un elemento della List2 prosegue.
    If profondita > 0 Then
        CambiaRisoluzione altezza, larghezza, profondita
    End If
End If
End Sub

Private Sub Form_Load()
With List1
    .AddItem "640x480"
    .AddItem "800x600"
    .AddItem "1024x768"
    .AddItem "1152x864"
    .AddItem "1280x960"
    .AddItem "1280x1024"
    .AddItem "1600x1200"
End With
With List2
    .AddItem "8 bit"
    .AddItem "16 bit"
    .AddItem "24 bit"
    .AddItem "32 bit"
End With
Command1.Caption = "Cambia Risoluzione"
List1.ListIndex = 1
'(800x600)
List2.ListIndex = 3 '(32 bit)
End Sub

Testato su: Windows 98, Windows Me, Windows 2000 Professional