[LIB] MDMF - Multiple Display Monitor Functions

Post your working scripts, libraries and tools for AHK v1.1 and older
just me
Posts: 9467
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [LIB] MDMF - Multiple Display Monitor Functions

24 Jan 2020, 15:56

@guest3456: Thanks! You should move the v2 version to an appropriate v2 forum.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: [LIB] MDMF - Multiple Display Monitor Functions

24 Jan 2020, 16:43

just me wrote:
24 Jan 2020, 15:56
@guest3456: Thanks! You should move the v2 version to an appropriate v2 forum.
its compatible with both v1 and v2

just me
Posts: 9467
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [LIB] MDMF - Multiple Display Monitor Functions

24 Jan 2020, 17:00

So it's a v2 version. ;)
Rohwedder
Posts: 7659
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: [LIB] MDMF - Multiple Display Monitor Functions

08 Apr 2024, 10:53

Hallo,
the v2-a108 example does not run with v2.0.12
Is there already an update?
iPhilip
Posts: 822
Joined: 02 Oct 2013, 12:21

Re: [LIB] MDMF - Multiple Display Monitor Functions

08 Apr 2024, 19:25

Rohwedder wrote:
08 Apr 2024, 10:53
Hallo,
the v2-a108 example does not run with v2.0.12
Is there already an update?
@Rohwedder Here's a version that works:

Code: Select all

; ======================================================================================================================
; Multiple Display Monitors Functions -> msdn.microsoft.com/en-us/library/dd145072(v=vs.85).aspx =======================
; ======================================================================================================================
; Enumerates display monitors and returns an object containing the properties of all monitors or the specified monitor.
; ======================================================================================================================
MDMF_Enum() {
   Monitors := Map(), Address := CallbackCreate(MDMF_EnumProc)
   Success := DllCall("User32.dll\EnumDisplayMonitors", "Ptr", 0, "Ptr", 0, "Ptr", Address, "Ptr", ObjPtr(Monitors), "Int")
   return (CallbackFree(Address), Success ? Monitors : false)
}
; ======================================================================================================================
;  Callback function that is called by the MDMF_Enum function.
; ======================================================================================================================
MDMF_EnumProc(HMON, HDC, PRECT, ObjectAddr) {
   Monitors := ObjFromPtrAddRef(ObjectAddr)
   Monitors[HMON] := MDMF_GetInfo(HMON)
   if Monitors[HMON].Primary
      Monitors.Primary := HMON
   return true
}
; ======================================================================================================================
; Retrieves the display monitor that has the largest area of intersection with a specified window.
; The following flag values determine the function's return value if the window does not intersect any display monitor:
;    MONITOR_DEFAULTTONULL    = 0 - Returns NULL.
;    MONITOR_DEFAULTTOPRIMARY = 1 - Returns a handle to the primary display monitor. 
;    MONITOR_DEFAULTTONEAREST = 2 - Returns a handle to the display monitor that is nearest to the window.
; ======================================================================================================================
MDMF_FromHWND(HWND, Flag?) {
   return DllCall("User32.dll\MonitorFromWindow", "Ptr", HWND, "UInt", Flag ?? 0, "Ptr")
}
; ======================================================================================================================
; Retrieves the display monitor that contains a specified point.
; If either X or Y is empty, the function will use the current cursor position for this value and return it ByRef.
; The following flag values determine the function's return value if the point is not contained within any
; display monitor:
;    MONITOR_DEFAULTTONULL    = 0 - Returns NULL.
;    MONITOR_DEFAULTTOPRIMARY = 1 - Returns a handle to the primary display monitor. 
;    MONITOR_DEFAULTTONEAREST = 2 - Returns a handle to the display monitor that is nearest to the point.
; ======================================================================================================================
MDMF_FromPoint(X?, Y?, Flag?) {
   if !IsSet(X) || !IsSet(Y) {
      PT := Buffer(8)
      DllCall("User32.dll\GetCursorPos", "Ptr", PT, "Int")
      if !IsSet(X)
         X := NumGet(PT, 0, "Int")
      if !IsSet(Y)
         Y := NumGet(PT, 4, "Int")
   }
   return DllCall("User32.dll\MonitorFromPoint", "Int64", (X & 0xFFFFFFFF) | (Y << 32), "UInt", Flag ?? 0, "Ptr")
}
; ======================================================================================================================
; Retrieves the display monitor that has the largest area of intersection with a specified rectangle.
; Parameters are consistent with the common AHK definition of a rectangle, which is X, Y, W, H instead of
; Left, Top, Right, Bottom.
; The following flag values determine the function's return value if the rectangle does not intersect any
; display monitor:
;    MONITOR_DEFAULTTONULL    = 0 - Returns NULL.
;    MONITOR_DEFAULTTOPRIMARY = 1 - Returns a handle to the primary display monitor. 
;    MONITOR_DEFAULTTONEAREST = 2 - Returns a handle to the display monitor that is nearest to the rectangle.
; ======================================================================================================================
MDMF_FromRect(X, Y, W, H, Flag?) {
   RC := Buffer(16)
   DllCall("SetRect", "Ptr", RC, "Int", X, "Int", Y, "Int", X + W, "Int", Y + H, "Int")
   return DllCall("User32.dll\MonitorFromRect", "Ptr", RC, "UInt", Flag ?? 0, "Ptr")
}
; ======================================================================================================================
; Retrieves information about a display monitor.
; ======================================================================================================================
MDMF_GetInfo(HMON) {
   MIEX := Buffer(104)
   NumPut "UInt", 104, MIEX, 0
   if DllCall("User32.dll\GetMonitorInfo", "Ptr", HMON, "Ptr", MIEX, "Int")
      return {Name:       (Name := StrGet(MIEX.Ptr + 40, 32))  ; CCHDEVICENAME = 32
            , Num:        RegExReplace(Name, ".*(\d+)$", "$1")
            , Left:       NumGet(MIEX,  4, "Int")    ; display rectangle
            , Top:        NumGet(MIEX,  8, "Int")    ; "
            , Right:      NumGet(MIEX, 12, "Int")    ; "
            , Bottom:     NumGet(MIEX, 16, "Int")    ; "
            , WALeft:     NumGet(MIEX, 20, "Int")    ; work area
            , WATop:      NumGet(MIEX, 24, "Int")    ; "
            , WARight:    NumGet(MIEX, 28, "Int")    ; "
            , WABottom:   NumGet(MIEX, 32, "Int")    ; "
            , Primary:    NumGet(MIEX, 36, "UInt")}  ; contains a non-zero value for the primary monitor.
   return false
}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
Rohwedder
Posts: 7659
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: [LIB] MDMF - Multiple Display Monitor Functions

09 Apr 2024, 00:27

Thank you very much!
iPhilip
Posts: 822
Joined: 02 Oct 2013, 12:21

Re: [LIB] MDMF - Multiple Display Monitor Functions

09 Apr 2024, 00:30

You are welcome. :)
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: xavierarmand and 106 guests