?
Solved

Detect Maximum Available Video Resolution

Posted on 2000-04-13
15
Medium Priority
?
461 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:orbitaltech
  • 5
  • 3
  • 3
  • +2
15 Comments
 
LVL 9

Expert Comment

by:Ruchi
ID: 2712742
go to
http://www.matthart.com/vbhelp/vbmain.htm#CHANGEREZ
"How to change the video resolution in Windows 95 via code"
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 400 total points
ID: 2712745
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2712746
See:

"Changing the screen resolution"
http://vb-world.net/tips/tip67.html
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:Ruchi
ID: 2712753
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2712767
And Also:

"Changing the Display Resolution from Visual Basic"
http://www.mvps.org/vbnet/code/enums/enumdisplaychange.htm
0
 
LVL 2

Author Comment

by:orbitaltech
ID: 2712775
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
 
LVL 2

Author Comment

by:orbitaltech
ID: 2712779
emoreau,

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

Thanx
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2712792
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
 
LVL 20

Expert Comment

by:hes
ID: 2712812
emoreau's suggestion does return all the available resolutions
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 2712975
emoreau's suggestion returns available resolutions for your hardware settings!
0
 
LVL 20

Expert Comment

by:hes
ID: 2713020
:) That's what I ment emoreau, should have typed it clearer < smile >
0
 
LVL 2

Author Comment

by:orbitaltech
ID: 2713038
emoreau,

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

Thanx
0
 
LVL 2

Author Comment

by:orbitaltech
ID: 2713117
Thank you.

 I only needed to enumerate through the currently select video adapter and what you gave me worked.
0
 
LVL 2

Author Comment

by:orbitaltech
ID: 2713464
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 2713539
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question