Solved

Detect Maximum Available Video Resolution

Posted on 2000-04-13
15
412 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 69

Accepted Solution

by:
Éric Moreau earned 100 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 69

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 69

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now