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.

When you need to identify monitors, call CreateDC with "DISPLAY" as the driver name and pass monitor name as the device name. Then you can draw a number on the result HDC.
I can not draw that number and I can not set an identifier. As I said in my last post:
I share this tool with my colleagues at work.
Okay... CreateDC is an idea... as I've read at MSDN I can call EnumDisplayMonitors with a DC to get informations about the monitor, so I tried the code below, but it doesn't work.
EnumDisplayMonitors does not call my MonitorEnumProc when I call it with a DC.
Do I something wrong? Do I misunderstood the MSDN article? Is that not possible? Can you confirm that?
Andreas.
Option Explicit
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Type DISPLAY_DEVICE
cb As Long
DeviceName As String * 32
DeviceString As String * 128
StateFlags As Long
DeviceID As String * 128
DeviceKey As String * 128
End Type
Private Const DISPLAY_DEVICE_MIRRORING_DRIVER = &H8
Private Declare Function EnumDisplayDevicesS Lib "user32" Alias "EnumDisplayDevicesA" ( _
ByVal DeviceName As String, ByVal iDevNum As Long, lpDisplayDevice As _
DISPLAY_DEVICE, ByVal dwFlags As Long) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" ( _
ByVal lpszDriver As String, ByVal lpszDevice As String, ByVal lpszOutput As Long, _
lpInitData As Any) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" ( _
ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function EnumDisplayMonitors Lib "user32.dll" ( _
ByVal hDC As Long, ByRef lprcClip As Any, ByVal lpfnEnum As Long, _
ByVal dwData As Long) As Long
Private Type MONITORINFO
cbSize As Long
rcMonitor As RECT
rcWork As RECT
dwFlags As Long
End Type
Private Declare Function GetMonitorInfo Lib "user32.dll" Alias "GetMonitorInfoA" ( _
ByVal hMonitor As Long, ByRef lpmi As MONITORINFO) As Long
Private Function MonitorEnumProc(ByVal hMonitor As Long, ByVal hdcMonitor As Long, _
lprcMonitor As RECT, ByVal dwData As Long) As Long
Dim MI As MONITORINFO
Debug.Print "Monitor " & hMonitor
GetMonitorInfo hMonitor, MI
With MI.rcMonitor
Debug.Print "Left", "Top", "Right", "Bottom"
Debug.Print .Left, .Top, .Right, .Bottom
End With
'Continue enumeration
MonitorEnumProc = 1
End Function
Function JustNumbers(ByVal What As String) As String
'Return only numbers from What (by Rick Rothstein)
Dim i As Long, j As Long, Digit As String
For i = 1 To Len(What)
Digit = Mid$(What, i, 1)
If Digit Like "#" Then
j = j + 1
Mid$(What, j, 1) = Digit
End If
Next
JustNumbers = Left$(What, j)
End Function
Sub Main()
Dim DD As DISPLAY_DEVICE
Dim DDevice
Dim DDevices As New Collection
Dim DMonitor
Dim DMonitors As New Collection
Dim i As Long, k As Long
Dim hDC As Long
Dim DrvName As String, DevName As String
Dim hWnd As Long, hRECT As RECT
'Get our window handle
hWnd = GetForegroundWindow
'Now I want to know if this window is located on monitor 1 or 2 (or both)
'Get the coordinates of the window
GetWindowRect hWnd, hRECT
With hRECT
Debug.Print "Window:"
Debug.Print "Left", "Top", "Right", "Bottom"
Debug.Print .Left, .Top, .Right, .Bottom
End With
'Get the device drivers
DD.cb = Len(DD)
i = 0
Do While EnumDisplayDevicesS(vbNullString, i, DD, 0&) <> 0
If (DD.StateFlags And DISPLAY_DEVICE_MIRRORING_DRIVER) = 0 Then
k = InStr(DD.DeviceName, vbNullChar)
DDevices.Add Left(DD.DeviceName, k - 1)
End If
i = i + 1
Loop
'2. Get the monitors on each driver
For Each DDevice In DDevices
i = 0
Do While EnumDisplayDevicesS(DDevice & vbNullChar, i, DD, 0&) <> 0
k = InStr(DD.DeviceName, vbNullChar)
DMonitors.Add Left(DD.DeviceName, k - 1)
i = i + 1
Loop
Next
'Create a DC for each monitor and get the coordinates
For Each DMonitor In DMonitors
i = InStrRev(DMonitor, "\")
'"\\.\DISPLAY1"
DrvName = Left(DMonitor, i - 1) & vbNullChar
'"Monitor0"
DevName = Mid(DMonitor, i + 1) & vbNullChar
hDC = CreateDC(DrvName, DevName, 0, ByVal 0&)
Debug.Print DrvName, DevName, hDC
'The driver name is the monitor number!?
EnumDisplayMonitors hDC, ByVal 0&, AddressOf MonitorEnumProc, Val(JustNumbers(DrvName))
DeleteDC hDC
Next
End Sub

Similar Messages

  • How to get the total number of pages printed in a report?

    Hi All,
    I have a requirement where I need to print a frame of fields only in the last page. Unfortunately I cannot use the 'Print Object On' property as it doesnt work in my case. So, I am planning to write a format trigger on the frame to return TRUE if the page is the last physical page. Now, I need to know how to get the total number of physical pages that will get printed in the report so that I can use this to manipulate the frame. I was planning to use the 'Total Physical Pages' built-in, but it seems like I can just use it to print in a field and I can't use this field's value anywhere in the plsql code (formula column function/format trigger) in the report. Is there anyway to get the total number of pages printed in the report which can be used in the report plsql code?
    Thanks,
    Srini.

    i found the solution, thanks

  • How to get the page number in the break section?

    Hi expert,
    The user wants the page number came right after the Break group name if the break section contains more than 1 page. E.g.
    ABC Compnay
    XXXXXXXXXXX XXXXXXXX
    XXXXXXXXXX XXXXXXXXXXXX
    BCD Company, p 1
    XXXXXXXXXXX XXXXXXXX
    XXXXXXXXXX XXXXXXXXXXXX
    BCD Company, p 2
    XXXXXXXXXXX XXXXXXXX
    XXXXXXXXXX XXXXXXXXXXXX
    CDE Company
    XXXXXXXXXXX XXXXXXXX
    XXXXXXXXXX XXXXXXXXXXXX
    How to get the page number in section break level?

    Hi Wes
    * Create a Field on the margin.
    * Set its "Source" to "Page Number"/"Physical Page Number".
    * Click "Page Numbering" button.
    * In "Reset At" list box, choose the Repeating frame that surrounds your detail group in the layout.
    This should cause Reports to increment the page number until the "Company" repeating frame resets.
    Regards
    Sripathy

  • How to get the Page Number in CS3 with VB?

    Hi, Everybody
    I use the Visual Basic to read the words and their page number where the Words are at. I want to do it according to Story. Some TextFrame of the Story waw placed in other pages: How can I get the Page number according to the Word that is selected?
    For myStoryCounter = 1 to Document.Stories.Count
    Set myStory = Document.Stories.Item(myStoryCounter)
    For myWordCounter = 1 to myStory.Words.Count
    Set myWord = myStory.Words.Item(myWordCounter)
    myWordContents = myWord.Contents
    Rem ** How to Get the Page Number according to the Word ***
    Next
    Next

    > But I don't understand why Item(1) are set.
    because Word can have more than one parent TextFrame - when split across TextFrames
    only Character have one ParentTextFrame accessed also by ParentTextFrames.Item(1)
    > why Story.TextFrames.Count alway is 0, Is it bug?
    no - it's not bug
    in older IDs - Story.TextFrames collection refer to all TextFrames as containers for Story text
    but in CS3 - collection Story.TextFrames contain all TextFrames inserted as InLine/Anchored objects - same like Story.Rectangles or Story.Ovals
    in CS3 - when you want to refer to TextFrames as containers for Story text - you need to use Story.TextContainers - because now you can link TextFrame and Text-On-Path as Story - Text-On-Path isn't TextFrame
    robin
    www.adobescripts.com

  • How to get the document number for a ware house order.

    Hello gurus,
    how to get the document number for a ware house order. [if GI is posted refering that WH order] .. is there any report?
    Thanks in advance

    There are several options.  When you post a Goods Movement, you can use LB12 -display Transfer Requirement for material document or LB11 Display TR for material.
    Depending on how your system is set up you may have gotten a Transfer Order automcatically.  In this case you can look at LT24 - Transfer Order for material.

  • I'm trying to load a student version of Acrobat XI Pro but not sure how to get the serial number to validate I'm a student

    I'm trying to load a student version of Acrobat XI Pro but not sure how to get the serial number to validate I'm a student

    Hi ,
    Could you please tell me form where did you purchase the Student version of Acrobat ?Is it through Adobe or some outside vendor ?
    Is it the first time you are trying to access it ?
    If yes ,then you must have received your serial number along with your product .
    If not ,then how how did you manage to get your serial number and access Acrobat before ?
    Regards
    Sukrit Dhingra

  • How to get the delivery number for the sales order

    hi
    how to get the delivery number for the sales order

    Hi,
    1. IN VA03, enter the sales order and click on the document flow button. From there you can check the delivery document.
    2. In SE11, enter VBFA(Document flow) table and enter the sales order in VBELV field and in VBTYP_N field enter 'J' to specify that you want to check if there is already a delivery document for that sales order.
    Hope it helps...
    P.S. Please award points if it helps...

  • HT1529 If I do not have the macbook with me and in the bill is not writing? How to get the Serial Number true Itunes?

    If I do not have the macbook with me and in the bill is not writing? How to get the Serial Number true Itunes?

    https://support.apple.com/kb/HT2526

  • HT4061 last night I stole my ipad and not how to get the serial number I have the itunes account that I do

    last night I stole my ipad and not how to get the serial number I have the itunes account that I do

    What To Do If Your iDevice or Computer Is Lost Or Stolen
    If your Mac, iPhone, iPod, iPod Touch, or iPad is lost or stolen what do you do? There are things you should do in advance - before you lose it or it's stolen - and some things to do after the fact. Here are some suggestions:
    Reporting a lost or stolen Apple product
    What-To-Do-When-Iphone-Is-Stolen
    Lost or Stolen iPhone? Here’s What to do.
    6 Ways to Track and Recover Your Lost/Stolen iPhone
    Find My iPhone
    It pays to be proactive by following the advice on using Find My Phone before you lose your device:
    Find My iPhone
    Setup your iDevice on MobileMe
    OS X Lion- About Find My Mac
    How To Set Up Free Find Your iPhone (Even on Unsupported Devices)
    Third-party solutions for computers:
    VUWER 1.5.4
    Sneaky ******* 0.2.0
    Undercover 4.7
    LoJack for Laptops Premium Mac
    STEM 2.1

  • How to get the page number according to MarkerID?

    I want to get the page number according to the marker element in fdk. I can get the marktext and markid,How to get the page number according to MarkerID in fdk?

    zhaopeng,
    You need to first get the ID of the paragraph containing the marker (with FP_TextLoc), then get the top-level frame containing the FO_Pgf object (FP_InTextFrame), then get the page containing the FO_TextFrame object (FP_PageFramePage). Here is a function that you can send various objects to in order to get the page ID, including paragraphs (this courtesy of Rick Quatro, originally posted to the Yahoo Framedev list):
    F_ObjHandleT GetPage(F_ObjHandleT oDoc, F_ObjHandleT oObj)
      F_ObjHandleT oFrame = 0;
      IntT iObjType;
      F_ObjHandleT oRow, oCell;
      while(oObj)
        oFrame = oObj;
        iObjType = F_ApiGetObjectType(oDoc, oObj);
        switch(iObjType)
          case FO_SubCol:
          oObj = F_ApiGetId(oDoc, oObj, FP_ParentTextFrame);
          break;
          case FO_Tbl:
          oRow = F_ApiGetId(oDoc, oObj, FP_FirstRowInTbl);
          oCell = F_ApiGetId(oDoc, oRow, FP_FirstCellInRow);
          oObj = oCell;
          break;
          case FO_Row:
          oCell = F_ApiGetId(oDoc, oObj, FP_FirstCellInRow);
          oObj = oCell;
          break;
          case FO_Cell:
          case FO_Pgf:
          case FO_AFrame:
          oObj = F_ApiGetId(oDoc, oObj, FP_InTextFrame);
          break;
          case FO_TextLine:
          case FO_TextFrame:
          case FO_UnanchoredFrame:
          case FO_Arc:
          case FO_Ellipse:
          case FO_Group:
          case FO_Inset:
          case FO_Line:
          case FO_Math:
          case FO_Polygon:
          case FO_Polyline:
          case FO_Rectangle:
          case FO_RoundRect:
          oObj = F_ApiGetId(oDoc, oObj, FP_FrameParent);
          break;
          //endless loop prevention, stops the process when the top-level frame is reached
          default:
          oObj = 0;
          break;
      //if we found the top-level frame, return its page ID, otherwise null
      if(oFrame)
        return (F_ApiGetId(oDoc, oFrame, FP_PageFramePage));
      else return(0);
    ...so, you would do something like this if you have the marker ID:
    F_TextLocT textLoc;
    F_ObjHandleT pageId;
    textLoc = F_ApiGetId(docId, markerId, FP_TextLoc);
    pageId = GetPage(docId, pageId.objId);
    Note: I'm not sure how this handles the case where a paragraph extends across two pages and the marker is located on the second page. My thought is that it might erroneously return the page where the paragraph begins. I don't know how to handle that.
    Russ

  • How to get the Equipements number and desciption for the contract item ?

    Hi Gurus,
            How to get the Equipements number and desciption for the contract item items? It would gr8 helpful to me...
    VBAP,,EQUI,,,,,,?
    Thanks
    Krisna

    Check with below tables :
    EQUI    Equipment master data
    EQKT   Equipment short text
    EQUZ   Equipment time segment
    Thanks
    Seshu

  • How to get the vendor number that was created using xk01..?

    Suppose i am creating a vendor with all the information by using a bdc program.
    In the same program if i want to go to the XK02 of that particular vendor that was created recently....how it possible..?
    Means how to get that particular vendor number that was created recently...?

    Hi...
    You Can Get the Vendor number after Calling the Transaction 'XK01' as below.
    <b>Declare a table for Collecting messages :</b>
    DATA: T_MSG TYPE TABLE OF BDCMSGCOLL,
              WA_MSG TYPE  BDCMSGCOLL.
    <b>Then Call Transaction Statment:</b>
    CALL TRANSACTION 'XK01'
               USING T_BDCDATA
               MODE 'N'
               MESSAGES INTO t_msg.
    if sy-subrc = 0.
      READ TABLE T_MSG INTO WA_MSG WITH MSGTYP = 'S'.
                                                                    MSGID = 'F2'
                                                                    MSGNR =  '175'.
    <b>Note: Bcoz the XK01 will issue this message</b>
      if sy-subrc = 0.
      write: / WA_MSG-MSGV1.  "This will contrain Vendor Number
    endif.
    endif.
    And you can also Try the Other method i.e. after the Call transaction statement
    <b> GET PARAMETER ID 'LIF' field V_LIFNR.
      WRITE:/ V_LIFNR.</b>
    Reward if Helpful.

  • How to get the row number

    Hi list,
    does any one know how I can get the row number the same as what I have in column rowno?
    thanks
    Arvin
    REATE   TABLE dbo.temptable
    ( y int  NOT NULL,
      e int not null,
      c int not null,
      rowno int not null)
    /* insert values  */
    INSERT INTO dbo.temptable(y,e,c,rowno ) VALUES
    (1,1,1,1),
    (1,1,2,1),
    (1,1,3,1),
    (1,20,1,2),
    (1,20,2,2),
    (1,20,3,2),
    (1,3,1,3),
    (1,3,1,3),
    (2,1,1,1),
    (2,1,1,1),
    (2,2,1,2),
    (2,2,1,2);

    You may update your rownumber column with Column "e".
    But why do you duplicate your data? May be there is no particular reason, you may be wasting space for it
    Try the below:
    CREATE TABLE dbo.temptable
    ( y int NOT NULL,
    e int not null,
    c int not null,
    ronum int null)
    INSERT INTO dbo.temptable(y,e,c ) VALUES
    (1,1,1),
    (1,1,2),
    (1,1,3),
    (1,20,1),
    (1,20,2)
    select * from temptable
    update dbo.temptable Set ronum=e
    Select * From dbo.temptable
    DRop table dbo.temptable

  • How to get the Personal Number from Address Number

    Hi,
    I have one requirement which is as follows.
    I have one Customer. I am fetching Address Number(ADRNR) of this customer number from table KNA1.
    I want to find the Personal number linked to this Address number and Customer.
    Is there is any table or FM from where i will get the Personal number of the Customer using Customer number and Address number?
    Kindly Help

    Hi Sachin,
    Check this FM BUP_PARTNER_PERSNUMBER_GET
    " Check KNB1 and the Field name is PERNR, Most of the times this is not maintianed.
    " This refers to the Employees who are assigned as Customers
    " In XD03 go to the Company Code Data there you can find this Value
    " For more info contact your Functional Consultant
    " This is to the Best of my knowledge
    Also Check KNB1 but000 Tables
    Cheerz
    Ram

  • How to get the BP number from OBJECT ID or ID rel.Object taken from HRP1001

    Hi Gurus
    I am trying to find the BP number from the OBJECT ID or ID rel.Object which i got from the table HRP1001 in CRM  but i am not able to find the link straightaway to get that details
    Kindly help me to find out the table where i can simply put the OBJECT ID or ID rel.Object to get the BP number for ROLE EMPLOYEE
    Thanks a lot
    Regards
    Soni

    Hi Soni,
    I found this in a different thread:
    first you get the all connected partners to a position by selecting SOBID from hrp1001 (with OTYPE / OBJID of position, RELAT = 008, SCLAS = CP).
    With this SOBID go again to HRP1001 and fill the SOBID into OBJID (with OTYPE = CP, SCLAS = BP) and the SOBID of the result is the connected partner number.
    You can also use FM 'CRM_CENTRALPERSON_GET' instead of the last SELECT-Statement. Here IV_PERSON_ID = SOBID (of first select). You get the partner guid and can get data of the partner from but000.
    In this thread:
    Help needed...Infotype HRP1001--Getting details OF Position Users...
    Hope it helps.
    regards,
    Wim

Maybe you are looking for

  • Software RAID Failure - my experience and solution

    I just wanted to share this information with the iCloud community. I searched a bit and did not find much information that was useful with regard to my software RAID issue. I have 27 inch Mid 2011 iMac with SSD and Hard drive which has been great. I

  • Switching from Windows to Mac

    Here's my situation: I own CS 5.0 Design Premium. I run it on a PC. I want to switch to my new Mac. Adobe Support told me that I cannot move CS5.0 from PC to Mac. They said I have to buy the upgrade to CS5.5 and specify the Mac as my target OS. The u

  • VA02 Transaction VBAP & VBKD data sync

    Dear Friends VA02 Transaction, VBAP & VBKD data synchronization issue. For printing invoice I am retrieving data from VBAP & VBKD For Example I have 18 Records in VBAP table the same time I have only 16 entries in VBKD table the field POSNR not match

  • Settings for sharpening 1ds MK3 files

    Can anyone recommend sharpening settings in Aperture for 1ds mk3 files. Canon has recommended sharpening settings for PS but does not provide them for Aperture or other software. Thanks

  • Unable to download music or video directly to mobo...

    in Nokia 710, unable to download music or video directly from internet explorer. Not having option to connect internet through pc suite. Unable to download apps from market place from debit card or net banking. Not having adobe flash player. Unable t