Visual Basic Core Snippet Routines
GetMonitorByRect
          
                 
   Returns a handle to the display monitor that has the largest area of intersection with a specified rectangle. This rectangle can be a form, or any rectangle inside or outside the desktop.     
           Updated:   Monday December 26, 2011   
click to copy code:  



   Applies to:   VB4-32, VB5, VB6   
OS restrictions:   Win 98/Me, 2000/XP  
 Code Snippet
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 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 Const MONITOR_DEFAULTTONULL As Long = &H0    'If the monitor is not found, return 0
Private Const MONITOR_DEFAULTTOPRIMARY As Long = &H1 'If the monitor is not found, return the primary monitor
Private Const MONITOR_DEFAULTTONEAREST As Long = &H2 'If the monitor is not found, return the nearest monitor

Private Type RECT
   Left    As Long
   Top     As Long
   Right   As Long
   Bottom  As Long
End Type
         
Private Declare Function MonitorFromRect Lib "user32" _
  (rc As RECT, _
   ByVal dwFlags As Long) As Long
         
Private Function GetMonitorByRect(rc As RECT) As Long

  'Returns a handle to the display monitor that
  'has the largest area of intersection with
  'a specified rectangle. This rectangle can be
  'a form, or any rectangle inside or outside
  'the desktops.
  '
  'If the rectangle intersects one or more display
  'monitor rectangles, the return value is an
  'HMONITOR handle to the display monitor that
  'has the largest area of intersection with the
  'rectangle.
  '
  'If the window does not intersect a display
  'monitor, the return value depends on the
  'value of dwFlags. Available values are:
  'MONITOR_DEFAULTTONEAREST: Returns a handle to the
  '                          display monitor that is
  '                          nearest to the window.
  'MONITOR_DEFAULTTONULL:    Returns NULL
  'MONITOR_DEFAULTTOPRIMARY: Returns a handle to the
  '                          primary display monitor.
  
   GetMonitorByRect = MonitorFromRect(rc, MONITOR_DEFAULTTONEAREST)
   
End Function

 Calling Syntax
  Dim rc As Rect
                 
                  rc.Left = 650
                  rc.Right = 1243
                  rc.Top = 455
                  rc.Bottom = 950
                  hMonitor = GetMonitorByRect(rc)

 Comments / Related
demo in use:    
Related:   EnumDisplayMonitors: Enumerating System Monitor Info
Core: DesktopsSameColourFormat
Core: GetDesktopMaximumHeight
Core: GetDesktopMaximumWidth
Core: GetMonitorByPoint
Core: GetMontitorByRect
Core: GetMonitorByWindow
Core: GetMonitorCount

 
 

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