Visual Basic Core Snippet Routines
   Returns a handle to the display monitor that contains a specified point.     
           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 POINTAPI
   x  As Long
   y  As Long
End Type

Private Declare Function MonitorFromPoint Lib "user32" _
  (ByVal x As Long, ByVal y As Long, _
   ByVal dwFlags As Long) As Long
Private Declare Function ClientToScreen Lib "user32" _
  (ByVal hwnd As Long, _
   lpPoint As POINTAPI) As Long
Private Function GetMonitorByPoint(x As Single, y As Single) As Long

  'Returns a handle to the display monitor
  'that contains a specified point.
  'If the point is contained by a display
  'monitor, the return value is a handle
  'to that display monitor.
  'If the point is not contained by 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_DEFAULTTOPRIMARY: Returns a handle to the
  '                          primary display monitor.
   Dim pt As POINTAPI
   pt.x = x \ Screen.TwipsPerPixelX
   pt.y = y \ Screen.TwipsPerPixelY
  'convert form coordinates to screen coordinates
   ClientToScreen Me.hwnd, pt
   GetMonitorByPoint = MonitorFromPoint(pt.x, pt.y, MONITOR_DEFAULTTONEAREST)
End Function

 Calling Syntax
   x = 1650
      y = 1243
      hMonitor = GetMonitorByPoint(x, y)

 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