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
 
 
 
 
 
TitleChange display mode
Keywordschange display mode, color resolution, resolution, change display size
CategoriesWindows
 
Use the ChangeDisplaySettings API function with the CDS_TEST parameter to see if the change will work. If the routine doesn't return an error, use ChangeDisplaySettings with the CDS_UPDATEREGISTRY parameter to make the change.
 
Private Sub cmdSetSize_Click()
Dim txt As String
Dim wid As Integer
Dim hgt As Integer
Dim bpp As Integer
Dim dev_mode As DEVMODE
Dim result As Long

    ' Get the desired values.
    txt = cboDisplaySize.Text
    wid = CLng(Split(txt, "x")(0))
    txt = Split(txt, "x")(1)
    hgt = CLng(Split(txt, "(")(0))
    txt = Split(txt, "(")(1)
    bpp = CLng(Split(txt, " ")(0))

    ' Test the desired mode.
    If SetDisplayMode(wid, hgt, bpp, CDS_TEST) = _
        DISP_CHANGE_SUCCESSFUL Then
        ' The test worked. Make the change.
        SetDisplayMode wid, hgt, bpp, CDS_UPDATEREGISTRY
    End If
End Sub

' Set the indicated display size and color depth.
Private Function SetDisplayMode(ByVal wid As Long, ByVal _
    hgt As Long, ByVal bpp As Long, ByVal change_flags As _
    Long) As Long
Dim dev_mode As DEVMODE
Dim result As Long

    ' Set the desired mode.
    With dev_mode
        .dmSize = Len(dev_mode)
        .dmDriverExtra = 0
        .dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or _
            DM_BITSPERPEL
        .dmPelsWidth = wid
        .dmPelsHeight = hgt
        .dmBitsPerPel = bpp
    End With

    ' Make the change.
    result = ChangeDisplaySettings(dev_mode, change_flags)
    Select Case result
        Case DISP_CHANGE_BADFLAGS
            MsgBox "Invalid flags in ChangeDisplaySettings"
        Case DISP_CHANGE_BADMODE
            MsgBox "Graphics mode not supported"
        Case DISP_CHANGE_BADPARAM
            MsgBox "Invalid parameter to " & _
                "ChangeDisplaySettings"
        Case DISP_CHANGE_FAILED
            MsgBox "Display driver failed in specific " & _
                "graphics mode in ChangeDisplaySettings"
        Case DISP_CHANGE_NOTUPDATED
            MsgBox "Unable to write changes to Registry in " & _
                "ChangeDisplaySettings"
        Case DISP_CHANGE_RESTART
            MsgBox "Restart required before changes can " & _
                "take effect"
        Case DISP_CHANGE_SUCCESSFUL
            ' It worked.
    End Select

    SetDisplayMode = result
End Function
 
I believe this process sends the message WM_DISPLAYCHANGE to all toplevel windows so they can rearrange themselves for the new display mode. The windows don't always seem to do this properly, however. For example, in some tests the Visual Basic development environment did not resize itself to fill the available display area.

See also Change display resolution in Windows XP.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated