Visual Basic Core Snippet Routines
GetMonitorByWindow
          
                 
   Returns handle to the display monitor that has the largest area of intersection with the bounding rectangle of a specified window.     
           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 MonitorFromWindow Lib "user32" _
  (ByVal hwnd As Long, _
   ByVal dwFlags As Long) As Long
         
Private Function GetMonitorByWindow(ByVal hwnd As Long) As Long

  'Returns a handle to the display monitor
  'that has the largest area of intersection
  'with the bounding rectangle of a specified
  'window.
  '
  'If the window specified by hwnd 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
  'window.
  '
  'If the window is currently minimized,
  'the window occupied before minimization
  'is returned.
  '
  '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.
  
   GetMonitorByWindow = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST)
   
End Function

 Calling Syntax
   hMonitor = GetMonitorByWindow(Me.hwnd)

 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