VBA API: How to get the monitor number?

Hello,

I have written an Excel application which controls an external application (making inputs, reads out the window, etc.).

I use a lot of API functions, e.g. as in the code from thisarticle:
https://support.microsoft.com/en-us/kb/194578?wa=wsignin1.0

A basic function of my application is to distinguish between windows that have the same title but are on different monitors.

In my windows (and NVIDIA) settings the left monitor is 1 and the right monitor is 2.

In the past I have this routine to "identify" the monitor number from a window handle:

Function GetWindowMonitor(ByVal hWnd As Long) As Integer
  Dim hMonitor As Long, MI As MONITORINFO, hM
  hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST)
  MI.cbSize = Len(MI)
  GetMonitorInfo hMonitor, MI
  Set mMonitor = New Collection
  EnumDisplayMonitors ByVal 0&, ByVal 0&, AddressOf MonitorEnumProc, ByVal 0&
  For Each hM In mMonitor
    GetWindowMonitor = GetWindowMonitor + 1
    If hM = hMonitor Then Exit Function
  Next
  GetWindowMonitor = 0
End Function

But last week I got a new graphic card and now this obvious stupid function fails, because EnumDisplayMonitors delivers the 2nd monitor first and then the 1st monitor.

Long talk, short question: Is there any way to get the real monitor numbers as they are shown inside the windows settings?

Andreas.

April 7th, 2015 1:24am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics