Visual Basic Screen & System Metrics
GetDeviceCaps: Display Resolution, Refresh Rate and Colour Depth
     
Posted:   Sunday April 18, 1998
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB5, Windows 98
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

ChangeDisplaySettings: Change Display Resolution
EnumDisplaySettings: Enumerate Available Display Resolutions
     
 Prerequisites
None. Note that some settings are Windows version-specific.

Some Visual Basic users may need detailed information about the user's display settings in order to optimize the visual aspects of their applications.  By using the GetDeviceCaps() API, settings from the useful to the obscure can be obtained in a one-line call . 

The code below details retrieving the most popular requests - colour depth and resolution, and the comments section at the end of the page contains constants for all the available display-related settings.

 BAS Module Code
None.

 Form Code
Add a command button and four labels (Label1 - Label4)to a form, and add the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function GetDeviceCaps Lib "gdi32" _
  (ByVal hdc As Long, _
   ByVal nIndex As Long) As Long

Private Const HORZRES As Long = 8
Private Const VERTRES As Long = 10
Private Const BITSPIXEL As Long = 12
Private Const VREFRESH As Long = 116


Private Sub Command1_Click()

   Dim currHRes As Long
   Dim currVRes As Long
   Dim currBPP As Long
   Dim currVFreq As Long
   Dim sBPPtype As String
   Dim sFreqtype As String
   
  'get the system settings
   currHRes = GetDeviceCaps(hdc, HORZRES)
   currVRes = GetDeviceCaps(hdc, VERTRES)
   currBPP = GetDeviceCaps(hdc, BITSPIXEL)
   currVFreq = GetDeviceCaps(hdc, VREFRESH)
   
   
  'pretty up the descriptions a tad
   Select Case currBPP
      Case 4:      sBPPtype = "(16 Color)"
      Case 8:      sBPPtype = "(256 Color)"
      Case 16:     sBPPtype = "(High Color)"
      Case 24, 32: sBPPtype = "(True Color)"
   End Select
   
   Select Case currVFreq
      Case 0, 1:   sFreqtype = "(Hardware default)"
      Case Else:   sFreqtype = "(User-selected)"
   End Select
      
   Label1.Caption = currHRes & " pixels"
   Label2.Caption = currVRes & " pixels"
   Label3.Caption = currBPP & " bits per pixel  " & sBPPtype
   Label4.Caption = currVFreq & " hz  " & sFreqtype
   
End Sub
 Comments
These are all the available constants from the Windows header file pertaining to display settings obtainable through GetDeviceCaps().

Private Const DRIVERVERSION  As Long = 0     'Device driver version
Private Const TECHNOLOGY     As Long = 2     'Device classification
Private Const HORZSIZE       As Long = 4     'Horizontal size in millimeters
Private Const VERTSIZE       As Long = 6     'Vertical size in millimeters
Private Const HORZRES        As Long = 8     'Horizontal width in pixels
Private Const VERTRES        As Long = 10    'Vertical height in pixels
Private Const BITSPIXEL      As Long = 12    'Number of bits per pixel
Private Const PLANES         As Long = 14    'Number of planes
Private Const NUMBRUSHES     As Long = 16    'Number of brushes the device has
Private Const NUMPENS        As Long = 18    'Number of pens the device has
Private Const NUMMARKERS     As Long = 20    'Number of markers the device has
Private Const NUMFONTS       As Long = 22    'Number of fonts the device has
Private Const NUMCOLORS      As Long = 24    'Number of colors the device supports
Private Const PDEVICESIZE    As Long = 26    'Size required for device descriptor
Private Const CURVECAPS      As Long = 28    'Curve capabilities
Private Const LINECAPS       As Long = 30    'Line capabilities
Private Const POLYGONALCAPS  As Long = 32    'Polygonal capabilities
Private Const TEXTCAPS       As Long = 34    'Text capabilities
Private Const CLIPCAPS       As Long = 36    'Clipping capabilities
Private Const RASTERCAPS     As Long = 38    'Bitblt capabilities
Private Const ASPECTX        As Long = 40    'Length of the X leg
Private Const ASPECTY        As Long = 42    'Length of the Y leg
Private Const ASPECTXY       As Long = 44    'Length of the hypotenuse
Private Const SHADEBLENDCAPS As Long = 45    'Shading and blending caps (IE5)
Private Const LOGPIXELSX     As Long = 88    'Logical pixels/inch in X
Private Const LOGPIXELSY     As Long = 90    'Logical pixels/inch in Y
Private Const SIZEPALETTE    As Long = 104   'Number of entries in physical palette
Private Const NUMRESERVED    As Long = 106   'Number of reserved entries in palette
Private Const COLORRES       As Long = 108   'Actual color resolution
Private Const VREFRESH       As Long = 116   'Current vertical refresh rate of the
                                             'display device (for displays only) in Hz
Private Const DESKTOPVERTRES As Long = 117   'Horizontal width of entire desktop in pixels (NT5)
Private Const DESKTOPHORZRES As Long = 118   'Vertical height of entire desktop in pixels (NT5)
Private Const BLTALIGNMENT   As Long = 119   'Preferred blt alignment


Private Sub Command1_Click()

'get the system settings
Print GetDeviceCaps(hdc, DRIVERVERSION)  'Device driver version
Print GetDeviceCaps(hdc, TECHNOLOGY)     'Device classification
Print GetDeviceCaps(hdc, HORZSIZE)       'Horizontal size in millimetres
Print GetDeviceCaps(hdc, VERTSIZE)       'Vertical size in millimetres
Print GetDeviceCaps(hdc, HORZRES)        'Horizontal width in pixels
Print GetDeviceCaps(hdc, VERTRES)        'Vertical height in pixels
Print GetDeviceCaps(hdc, BITSPIXEL)      'Number of bits per pixel
Print GetDeviceCaps(hdc, PLANES)         'Number of planes
Print GetDeviceCaps(hdc, NUMBRUSHES)     'Number of brushes the device has
Print GetDeviceCaps(hdc, NUMPENS)        'Number of pens the device has
Print GetDeviceCaps(hdc, NUMMARKERS)     'Number of markers the device has
Print GetDeviceCaps(hdc, NUMFONTS)       'Number of fonts the device has
Print GetDeviceCaps(hdc, NUMCOLORS)      'Number of colours the device supports
Print GetDeviceCaps(hdc, PDEVICESIZE)    'Size required for device descriptor
Print GetDeviceCaps(hdc, CURVECAPS)      'Curve capabilities
Print GetDeviceCaps(hdc, LINECAPS)       'Line capabilities
Print GetDeviceCaps(hdc, POLYGONALCAPS)  'Polygonal capabilities
Print GetDeviceCaps(hdc, TEXTCAPS)       'Text capabilities
Print GetDeviceCaps(hdc, CLIPCAPS)       'Clipping capabilities
Print GetDeviceCaps(hdc, RASTERCAPS)     'BitBlt capabilities
Print GetDeviceCaps(hdc, ASPECTX)        'Length of the X leg
Print GetDeviceCaps(hdc, ASPECTY)        'Length of the Y leg
Print GetDeviceCaps(hdc, ASPECTXY)       'Length of the hypotenuse
Print GetDeviceCaps(hdc, SHADEBLENDCAPS) 'Shading and blending caps (IE5)
Print GetDeviceCaps(hdc, LOGPIXELSX)     'Logical pixels/inch in X
Print GetDeviceCaps(hdc, LOGPIXELSY)     'Logical pixels/inch in Y
Print GetDeviceCaps(hdc, SIZEPALETTE)    'Number of entries in physical palette
Print GetDeviceCaps(hdc, NUMRESERVED)    'Number of reserved entries in palette
Print GetDeviceCaps(hdc, COLORRES)       'Actual colour resolution
Print GetDeviceCaps(hdc, VREFRESH)       'Current vertical refresh rate of the
                                         'display device (for displays only) in Hz
Print GetDeviceCaps(hdc, DESKTOPVERTRES) 'Horizontal width of entire desktop in pixels
Print GetDeviceCaps(hdc, DESKTOPHORZRES) 'Vertical height of entire desktop in pixels
Print GetDeviceCaps(hdc, BLTALIGNMENT)   'Preferred blt alignment

End Sub

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter