Example:


VB Code:
  1. 'In a form (Form1)
  2. Private Sub Form_Load()
  3.     'KPD-Team 2000
  4.     'URL: [url]http://www.allapi.net/[/url]
  5.     'E-Mail: [email][email protected][/email]
  6.     'set the graphics mode of this form to persistent
  7.     Me.AutoRedraw = True
  8.     'start the enumeration
  9.     EnumDisplayMonitors ByVal 0&, ByVal 0&, AddressOf MonitorEnumProc, ByVal 0&
  10. End Sub
  11.  
  12. 'In a module
  13. Public Const MONITORINFOF_PRIMARY = &H1
  14. Public Const MONITOR_DEFAULTTONEAREST = &H2
  15. Public Const MONITOR_DEFAULTTONULL = &H0
  16. Public Const MONITOR_DEFAULTTOPRIMARY = &H1
  17. Public Type RECT
  18.     Left As Long
  19.     Top As Long
  20.     Right As Long
  21.     Bottom As Long
  22. End Type
  23. Public Type MONITORINFO
  24.     cbSize As Long
  25.     rcMonitor As RECT
  26.     rcWork As RECT
  27.     dwFlags As Long
  28. End Type
  29. Public Type POINT
  30.     x As Long
  31.     y As Long
  32. End Type
  33. Public Declare Function GetMonitorInfo Lib "user32.dll" Alias "GetMonitorInfoA" (ByVal hMonitor As Long, ByRef lpmi As MONITORINFO) As Long
  34. Public Declare Function MonitorFromPoint Lib "user32.dll" (ByVal x As Long, ByVal y As Long, ByVal dwFlags As Long) As Long
  35. Public Declare Function MonitorFromRect Lib "user32.dll" (ByRef lprc As RECT, ByVal dwFlags As Long) As Long
  36. Public Declare Function MonitorFromWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal dwFlags As Long) As Long
  37. Public Declare Function EnumDisplayMonitors Lib "user32.dll" (ByVal hdc As Long, ByRef lprcClip As Any, ByVal lpfnEnum As Long, ByVal dwData As Long) As Long
  38. Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
  39. Public Function MonitorEnumProc(ByVal hMonitor As Long, ByVal hdcMonitor As Long, lprcMonitor As RECT, ByVal dwData As Long) As Long
  40.     Dim MI As MONITORINFO, R As RECT
  41.     Debug.Print "Moitor handle: " + CStr(hMonitor)
  42.     'initialize the MONITORINFO structure
  43.     MI.cbSize = Len(MI)
  44.     'Get the monitor information of the specified monitor
  45.     GetMonitorInfo hMonitor, MI
  46.     'write some information on teh debug window
  47.     Debug.Print "Monitor Width/Height: " + CStr(MI.rcMonitor.Right - MI.rcMonitor.Left) + "x" + CStr(MI.rcMonitor.Bottom - MI.rcMonitor.Top)
  48.     Debug.Print "Primary monitor: " + CStr(CBool(MI.dwFlags = MONITORINFOF_PRIMARY))
  49.     'check whether Form1 is located on this monitor
  50.     If MonitorFromWindow(Form1.hwnd, MONITOR_DEFAULTTONEAREST) = hMonitor Then
  51.         Debug.Print "Form1 is located on this monitor"
  52.     End If
  53.     'heck whether the point (0, 0) lies within the bounds of this monitor
  54.     If MonitorFromPoint(0, 0, MONITOR_DEFAULTTONEAREST) = hMonitor Then
  55.         Debug.Print "The point (0, 0) lies wihthin the range of this monitor..."
  56.     End If
  57.     'check whether Form1 is located on this monitor
  58.     GetWindowRect Form1.hwnd, R
  59.     If MonitorFromRect(R, MONITOR_DEFAULTTONEAREST) = hMonitor Then
  60.         Debug.Print "The rectangle of Form1 lies within this monitor"
  61.     End If
  62.     Debug.Print ""
  63.     'Continue enumeration
  64.     MonitorEnumProc = 1
  65. End Function