Detect Maximum Available Video Resolution

I need to know how I can get the maximum available video resolution for the currently selected display device.

Ex. I am using 800x600 resolution, but I want to switch to 1024x768. I need to know if the video adapter will support it before I try to switch.

Anyone know the API call or routine to do this?
LVL 2
orbitaltechAsked:
Who is Participating?
 
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
Here is how you can list supported modes:
Add a command button and a listbox to a form and copy this code:
Option Explicit

Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32

Private Type DEVMODE
    dmDeviceName As String * CCHDEVICENAME
    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 * CCHFORMNAME
    dmUnusedPadding As Integer
    dmBitsPerPel As Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
End Type

Private Const DM_BITSPERPEL = &H40000
Private Const DM_PELSWIDTH = &H80000
Private Const DM_PELSHEIGHT = &H100000
Private Const DM_DISPLAYFLAGS = &H200000
Private Const DM_DISPLAYFREQUENCY = &H400000

Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpInitData As DEVMODE, ByVal dwFlags As Long) As Long
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (lpszDeviceName As Any, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long

Private Const BITSPIXEL = 12

' Flags for ChangeDisplaySettings
Private Const CDS_UPDATEREGISTRY = &H1
Private Const CDS_TEST = &H2
Private Const CDS_FULLSCREEN = &H4
Private Const CDS_GLOBAL = &H8
Private Const CDS_SET_PRIMARY = &H10
Private Const CDS_RESET = &H40000000
Private Const CDS_SETRECT = &H20000000
Private Const CDS_NORESET = &H10000000

' Return values for ChangeDisplaySettings
Private Const DISP_CHANGE_SUCCESSFUL = 0
Private Const DISP_CHANGE_RESTART = 1
Private Const DISP_CHANGE_FAILED = -1
Private Const DISP_CHANGE_BADMODE = -2
Private Const DISP_CHANGE_NOTUPDATED = -3
Private Const DISP_CHANGE_BADFLAGS = -4
Private Const DISP_CHANGE_BADPARAM = -5

Private Const EWX_LOGOFF = 0
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4

Private D() As DEVMODE
Private lNumModes As Long

Private Sub Command1_Click()
Dim l As Long
Dim Flags As Long
Dim x As Long

    x = List1.ListIndex
    D(x).dmFields = DM_BITSPERPEL Or DM_PELSWIDTH Or DM_PELSHEIGHT
    Flags = CDS_UPDATEREGISTRY
    l = ChangeDisplaySettings(D(x), Flags)
    Select Case l
        Case DISP_CHANGE_RESTART
            l = MsgBox("This change will not take effect until you reboot the system. Reboot now?", vbYesNo)
            If l = vbYes Then
                Flags = 0
                l = ExitWindowsEx(EWX_REBOOT, Flags)
            End If
        Case DISP_CHANGE_SUCCESSFUL
        Case Else
            MsgBox "Error changing resolution! Returned: " & l
    End Select
End Sub

Private Sub Form_Load()
Dim l As Long
Dim lMaxModes As Long
Dim lBits As Long
Dim lWidth As Long
Dim lHeight As Long

    lBits = GetDeviceCaps(hdc, BITSPIXEL)
    lWidth = Screen.Width \ Screen.TwipsPerPixelX
    lHeight = Screen.Height \ Screen.TwipsPerPixelY
    lMaxModes = 8
    ReDim D(0 To lMaxModes) As DEVMODE
    lNumModes = 0
    l = EnumDisplaySettings(ByVal 0, lNumModes, D(lNumModes))
    Do While l
        List1.AddItem D(lNumModes).dmPelsWidth & "x" & D(lNumModes).dmPelsHeight & "x" & D(lNumModes).dmBitsPerPel
        If lBits = D(lNumModes).dmBitsPerPel And _
            lWidth = D(lNumModes).dmPelsWidth And _
            lHeight = D(lNumModes).dmPelsHeight Then
            List1.ListIndex = List1.NewIndex
        End If
        lNumModes = lNumModes + 1
        If lNumModes > lMaxModes Then
            lMaxModes = lMaxModes + 8
            ReDim Preserve D(0 To lMaxModes) As DEVMODE
        End If
        l = EnumDisplaySettings(ByVal 0, lNumModes, D(lNumModes))
    Loop
    lNumModes = lNumModes - 1
End Sub
0
 
RuchiCommented:
go to
http://www.matthart.com/vbhelp/vbmain.htm#CHANGEREZ
"How to change the video resolution in Windows 95 via code"
0
 
Erick37Commented:
See:

"Changing the screen resolution"
http://vb-world.net/tips/tip67.html
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
RuchiCommented:
0
 
Erick37Commented:
And Also:

"Changing the Display Resolution from Visual Basic"
http://www.mvps.org/vbnet/code/enums/enumdisplaychange.htm
0
 
orbitaltechAuthor Commented:
Mabye I should be more clear....

I already know how to change the resolution.

I need to know how to detect the maximum available resloution available for the display adapter.

The EnumDisplaySettings is probably what I want to do, but I can't figure out how to loop through the display modes using it.

What I need is some code or some help with actually retreiving the max resolution using the API

ANy other suggestions?
0
 
orbitaltechAuthor Commented:
emoreau,

Give me a sec to try your proposal.. Give me about 1 hour

Thanx
0
 
RuchiCommented:
The correct address is
http://blackbeltvb.com/index.htm?free/changrez.htm 
"Utilizes API calls to enumerate possible video settings. Switches the system to the selected setting and reboots if necessary. pre-selection. Uses the following API functions:"

http://www5.50megs.com/andreavb/tip090002.html
"Get the current Video settings: Colors, Resolution and Pixels per Inch... "
0
 
hesCommented:
emoreau's suggestion does return all the available resolutions
0
 
Éric MoreauSenior .Net ConsultantCommented:
emoreau's suggestion returns available resolutions for your hardware settings!
0
 
hesCommented:
:) That's what I ment emoreau, should have typed it clearer < smile >
0
 
orbitaltechAuthor Commented:
emoreau,

Give me a sec to try your proposal.. Give me about 1 hour

Thanx
0
 
orbitaltechAuthor Commented:
Thank you.

 I only needed to enumerate through the currently select video adapter and what you gave me worked.
0
 
orbitaltechAuthor Commented:
Question?

After i change the resolution fo the display adapter, I noticed a few things (at least on NT 4.0 this happens)

#1) If you right-click on the desktop and select properties and select settings, the resolution slider is still set to the old resolution. (Mabye a reboot fixes this?)

#2) if I use
screen.width / screen.twipsperpixelX
screen.height / screen.twipsperpixelY

the return values are both the same (in my case 768)

Anybody know why this happens? Is this a confirmed bug in NT 4.0?
0
 
Éric MoreauSenior .Net ConsultantCommented:
Try this after changing resolution:

Private Declare Function SystemParametersInfo _
                Lib "user32" _
                Alias "SystemParametersInfoA" _
                (ByVal uAction As Long, _
                 ByVal uParam As Long, _
                 lpvParam As Any, _
                 ByVal fuWinIni As Long _
                ) As Long

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
   
Private Const SPI_GETWORKAREA = 48

Private Sub MDIForm_Load()
Dim WA As RECT

'Récupère les dimensions de l'application
lngReturn = SystemParametersInfo(SPI_GETWORKAREA, 0&, WA, 0&)
WA.Left = WA.Left * Screen.TwipsPerPixelX
WA.Right = WA.Right * Screen.TwipsPerPixelX
WA.Top = WA.Top * Screen.TwipsPerPixelY
WA.Bottom = WA.Bottom * Screen.TwipsPerPixelY
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.