Visual Basic Enumeration/Callback Routines

EnumDisplayMonitors: Enumerating System Monitor Info
     
Posted:   Sunday December 29, 2002
Updated:   Monday December 26, 2011
     
Applies to:   VB5, VB6
Developed with:   VB5, Windows XP
OS restrictions:   Windows 98/Me, Windows 2000/XP
Author:   VBnet - Randy Birch
     

Related:  

EnumDisplaySettings: Enumerate Available Display Resolutions
ChangeDisplaySettings: Change Display Resolution
EnumDisplayDevices: Enumerating System Display Devices
EnumDisplayMonitors: Enumerating System Monitor Info
Core: DesktopsSameColourFormat
Core: GetDesktopMaximumHeight
Core: GetDesktopMaximumWidth
Core: GetMonitorByPoint
Core: GetMontitorByRect
Core: GetMonitorByWindow
Core: GetMonitorCount
       
 Prerequisites
Visual Basic 5/6 supporting AddressOf.

GetSystemMetrics retrieves various system metrics (widths and heights of display elements) and system configuration settings.

EnumDisplayMonitors enumerates display monitors (including invisible pseudo-monitors associated with the mirroring drivers) that intersect a region formed by the intersection of a specified clipping rectangle and the visible region of a device context. Or in English, it provides monitor details in single or multi-monitor setups based on the monitor handle.

Together, these two calls can provide detailed information about the user's monitor and current desktop setup.

 BAS Module Code
Place the following code into the general declarations area of a bas module:

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.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Const SM_XVIRTUALSCREEN = 76    'virtual desktop left
Public Const SM_YVIRTUALSCREEN = 77    'virtual top
Public Const SM_CXVIRTUALSCREEN = 78   'virtual width
Public Const SM_CYVIRTUALSCREEN = 79   'virtual height
Public Const SM_CMONITORS = 80         'number of monitors
Public Const SM_SAMEDISPLAYFORMAT = 81

Public Type RECT
   Left    As Long
   Top     As Long
   Right   As Long
   Bottom  As Long
End Type

Public Type POINTAPI
   x  As Long
   y  As Long
End Type

Public Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (Destination As Any, _
   Source As Any, _
   ByVal Length As Long)
   
Public Declare Function EnumDisplayMonitors Lib "user32" _
  (ByVal hdc As Long, _
   ByVal lprcClip As Long, _
   ByVal lpfnEnum As Long, _
   dwData As Any) As Long

Public Declare Function GetSystemMetrics Lib "user32" _
   (ByVal nIndex As Long) As Long
    

Public Function MonitorEnumProc(ByVal hMonitor As Long, _
                                ByVal hdcMonitor As Long, _
                                ByVal rcMonitor As Long, _
                                dwData As ListBox) As Long

   Dim rc As RECT
   
  'copy the rectangle data from
  'rcMonitor into a RECT type
   CopyMemory rc, ByVal rcMonitor, Len(rc)
   
  'dwData is defined in the MSDN as
  'application-defined data that
  'EnumDisplayMonitors passes directly
  'to the enumeration function.
  '
  'Here we've used it to pass the listbox
  'for display. Note that the declare for
  'EnumDisplayMonitors has dwData defined
  '"dwData as Any". If you want something
  'other than a control passed, or no
  'application-specific data at all,
  'redefine the parameters above to change
  'the byref "dwData As ListBox" to
  '"ByVal dwData as Long".
   With dwData 'our listbox
   
      .AddItem "   Monitor handle :" & vbTab & hMonitor
      .AddItem "   Display rectangle :"
      .AddItem "      desktop left :" & vbTab & rc.Left
      .AddItem "      desktop top :" & vbTab & rc.Top
      .AddItem "      desktop height :" & vbTab & rc.Bottom - rc.Top
      .AddItem "      desktop width :" & vbTab & rc.Right - rc.Left
      .AddItem ""
      
   End With
   
  'return 1 to continue processing
  'monitor information, or return
  '0 to stop. Returning 1 obtains
  'info for all physical and invisible
  'pseudo-monitors associated with
  'mirroring drivers
   MonitorEnumProc = 1

End Function
 Form Code
Add a listbox (List1) and a command button (Command1) to a form, and add the following code:

Option Explicit
Private Sub Form_Load()

   Command1.Caption = "EnumDisplayMonitors"

End Sub
      
Private Sub Command1_Click()

   Dim numMonitors As Long
   Dim cnt As Long
   Dim rc As RECT
   
  'determine number of display
  'monitors on the desktop
   numMonitors = GetSystemMetrics(SM_CMONITORS)
   
   If numMonitors > 0 Then
   
      With List1
         .Clear
         .AddItem "GetSystemMetrics results:"
         .AddItem "   Number of display monitors:" & vbTab & numMonitors
      
        'Check if all display monitors have
        'the same color format. Returns True (1)
        'if they do, or FALSE otherwise. Note
        'that two displays can have the same
        'bit depth, but different color formats.
        'For example, the red, green, and blue
        'pixels can be encoded with different
        'numbers of bits, or those bits can be
        'located in different places in a pixel's
        'color value.
         .AddItem "   Same color format :" & vbTab & GetSystemMetrics(SM_SAMEDISPLAYFORMAT)
         .AddItem ""
      
        'determine the total size of the
        'virtual desktop created by all
        'display monitors on the desktop
         .AddItem "   Bounding rectangle of all display monitors :"
         .AddItem "     Virtual desktop - left :" & vbTab & GetSystemMetrics(SM_XVIRTUALSCREEN)
         .AddItem "     Virtual desktop - top :" & vbTab & GetSystemMetrics(SM_YVIRTUALSCREEN)
         .AddItem "     Virtual desktop - height :" & vbTab & GetSystemMetrics(SM_CYVIRTUALSCREEN)
         .AddItem "     Virtual desktop - width :" & vbTab & GetSystemMetrics(SM_CXVIRTUALSCREEN)
         .AddItem ""
         
      End With
      
   End If
   
   List1.AddItem "EnumDisplayMonitors results:"
   
  'The dwData member can be used to
  'specify any application-defined
  'value, so here we'll use it to
  'pass a listbox to the routine
  'for population
   Call EnumDisplayMonitors(0&, 0&, AddressOf MonitorEnumProc, List1)

End Sub
 Comments

 
 

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