VBA Tipp: Bildschirmauflösung und Wiederholfrequenz einstellen

Aus DBWiki
Wechseln zu: Navigation, Suche

Problem

Ich möchte die Bildschirmauflösung und/oder die Wiederholfrequenz des Bildschirms per Programm einstellen.

Lösung

Das geht mittels folgender API-Aufrufe:

Private Declare Function EnumDisplaySettings Lib _
        "user32" Alias "EnumDisplaySettingsA" _
       (ByVal lpszDeviceName As Long, _
        ByVal iModeNum As Long, lpDevMode As Any) _
        As Boolean
 
Private Declare Function ChangeDisplaySettings Lib _
        "user32" Alias "ChangeDisplaySettingsA" _
        (lpDevMode As Any, ByVal dwFlags As Long) _
        As Long
 
Private Const CCDEVICENAME = 32
Private Const CCFORMNAME = 32
Private Const DM_PELSWIDTH = &H80000
Private Const DM_PELSHEIGHT = &H100000
Private Const DM_DISPLAYFREQUENCY = &H400000
 
Private 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 ChangeScreenResolution(Optional Width, _
                                  Optional Height, _
                                  Optional Freq)
Dim I As Long, DevM As DEVMODE
  I = 0
  Do
    I = I + 1
  Loop Until Not EnumDisplaySettings(0&, I, DevM)
  With DevM
    If Not IsMissing(Width) Then
      .dmFields = .dmFields Or DM_PELSWIDTH
      .dmPelsWidth = Width
    End If
    If Not IsMissing(Height) Then
      .dmFields = .dmFields Or DM_PELSHEIGHT
      .dmPelsHeight = Height
    End If
    If Not IsMissing(Freq) Then
      .dmFields = .dmFields Or DM_DISPLAYFREQUENCY
      .dmDisplayFrequency = Freq
    End If
  End With
  ChangeDisplaySettings DevM, 0&
End Sub

Aufruf

Folgendermaßen stellt man eine Bildschirmauflösung von 800 x 600 und eine Wiederholfrequenz von 75 Hz ein:

ChangeScreenResolution 800, 600, 75