Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleView and change the system's display mode
Keywordsdisplay mode, pixels, color depth
CategoriesGraphics
 
When the program loads, it uses the EnumDisplaySettings API function to get display information. Subroutine DevModeCaption converts the information into a string giving the display's size in pixels, color depth, refresh frequency, and flags (if these last two values are available).
 
' Load the available device modes.
Private Sub Form_Load()
Dim dev_mode As DEVMODE
Dim mode_num As Long

    ' Get the available modes.
    mode_num = 0
    Do
        ' Stop when the function fails.
        If EnumDisplaySettings(ByVal vbNullString, _
            mode_num, dev_mode) = 0 _
                Then Exit Do

        ' Add this choice to the ComboBox.
        cboMode.AddItem DevModeCaption(dev_mode)
        cboMode.ItemData(cboMode.NewIndex) = mode_num

        mode_num = mode_num + 1
    Loop

    ' Make sure we got some modes.
    If mode_num = 0 Then
        MsgBox "EnumDisplaySettings returned no display " & _
            "modes."
        Exit Sub
    End If

    ' Get the current mode.
    If EnumDisplaySettings(ByVal vbNullString, _
        ENUM_CURRENT_SETTINGS, dev_mode) = 0 _
    Then
        MsgBox "Unable to get the current mode."
        Exit Sub
    End If

    ' Select the current mode.
    cboMode.Text = DevModeCaption(dev_mode)
End Sub

' Return the caption for this device mode information.
Private Function DevModeCaption(dev_mode As DEVMODE) As _
    String
    DevModeCaption = _
        Format$(dev_mode.dmPelsWidth) & " x " & _
        Format$(dev_mode.dmPelsHeight) & " (" & _
        Format$(dev_mode.dmBitsPerPel) & ") " & _
        ", Freq: " & _
        Format$(dev_mode.dmDisplayFrequency) & _
        " Flags: " & _
        Format$(dev_mode.dmDisplayFlags)
End Function
 
If you select a new display mode and click the Set Mode button, the program uses the ChangeDisplaySettings API function to change the display. This may require a reboot.
 
' Select the indicated mode.
Private Sub cmdSetMode_Click()
Dim dev_mode As DEVMODE
Dim mode_num As Long

    ' Refetch information about this mode.
    mode_num = cboMode.ItemData(cboMode.ListIndex)
    If EnumDisplaySettings(ByVal vbNullString, _
        mode_num, dev_mode) = 0 _
    Then
        MsgBox "Error refetching mode data."
        Exit Sub
    End If

    ' Confirm.
    If MsgBox("Do you want to select the mode " & _
        DevModeCaption(dev_mode), vbYesNo) = vbNo _
            Then Exit Sub

    ' Select the mode.
    dev_mode.dmFields = _
        DM_PELSWIDTH Or _
        DM_PELSHEIGHT Or _
        DM_BITSPERPEL
    dev_mode.dmSize = Len(dev_mode)
    dev_mode.dmDriverExtra = 0

    ' Test the change.
    Select Case ChangeDisplaySettings(dev_mode, CDS_TEST)
        Case DISP_CHANGE_RESTART
            If MsgBox("The system must reboot to make this " & _
                "change. Do you want to reboot?", _
                vbYesNo) = vbYes _
            Then
                If ChangeDisplaySettings(dev_mode, _
                    CDS_UPDATEREGISTRY) _
                    <> DISP_CHANGE_SUCCESSFUL _
                Then
                    MsgBox "Error setting the new mode."
                Else
                    ExitWindowsEx EWX_REBOOT, 0
                End If
            End If

        Case DISP_CHANGE_SUCCESSFUL
            If ChangeDisplaySettings(dev_mode, _
                CDS_UPDATEREGISTRY) _
                = DISP_CHANGE_SUCCESSFUL _
            Then
                MsgBox "Mode changed."
            Else
                MsgBox "Error setting the new mode."
            End If
        Case Else
            MsgBox "Error setting the new mode."
    End Select
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated