Jump to content

Dual-Monitor Resolution Detection


 Share

Recommended Posts

In my code, I need to find a way to replace "Unknown" with a variable that represents a numerical value.

I haven't yet added support for a vertical montior config, but if I can solidify this part of the code I'll expand it as needed, and I know where to come if I need help =)

$FW = DLLCall("user32.dll", "int", "GetSystemMetrics", "int", 78)
 $FH = DLLCall("user32.dll", "int", "GetSystemMetrics", "int", 79)
 $XA = @DesktopWidth
 $YA = @DesktopHeight
 $WA = $FW[0]
 $HA = $FH[0]
 
 If $WA > $XA Then
     $X1 = $XA
     $X2 = $WA-$XA
 ElseIf $WA = $XA Then
     $X1 = $XA
     $X2 = "Unknown"
 EndIf
 
 If $HA > $YA Then
     $Y1 = $YA
     $Y2 = $HA
 ElseIf $HA = $YA Then
     $Y1 = $YA
     $Y2 = "Unknown"
 EndIf
     
 MsgBox(0,"","Monitor A: " & $X1 & " by " & $Y1 & @LF & "Monitor B: " & $X2 & " by " & $Y2)

 

Edited by JLogan3o13
Removed reference to games, code is actually for monitor resolution
Link to comment
Share on other sites

Overkill said:

In my code, I need to find a way to replace "Unknown" with a variable that represents a numerical value.

I haven't yet added support for a vertical montior config, but if I can solidify this part of the code I'll expand it as needed, and I know where to come if I need help =)

$FW = DLLCall("user32.dll", "int", "GetSystemMetrics", "int", 78)
 $FH = DLLCall("user32.dll", "int", "GetSystemMetrics", "int", 79)
 $XA = @DesktopWidth
 $YA = @DesktopHeight
 $WA = $FW[0]
 $HA = $FH[0]
 
 If $WA > $XA Then
     $X1 = $XA
     $X2 = $WA-$XA
 ElseIf $WA = $XA Then
     $X1 = $XA
     $X2 = "Unknown"
 EndIf
 
 If $HA > $YA Then
     $Y1 = $YA
     $Y2 = $HA
 ElseIf $HA = $YA Then
     $Y1 = $YA
     $Y2 = "Unknown"
 EndIf
     
 MsgBox(0,"","Monitor A: " & $X1 & " by " & $Y1 & @LF & "Monitor B: " & $X2 & " by " & $Y2)

Here's a WMI query that can give you the monitor number and its dimensions:

CODE
$strComputer = "."
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")

$colItems = $objWMIService.ExecQuery("Select * from Win32_DesktopMonitor")

$sMsg = ""

For $objItem In $colItems

$sMsg &= "****************************" & @CRLF

$sMsg &= "Monitor: " & $objItem.DeviceID & @CRLF

$sMsg &= "****************************" & @CRLF

$sMsg &= "Description: " & $objItem.Description & @CRLF

$sMsg &= "Device ID: " & $objItem.DeviceID & @CRLF

$sMsg &= "Monitor Manufacturer: " & $objItem.MonitorManufacturer & @CRLF

$sMsg &= "Monitor Type: " & $objItem.MonitorType & @CRLF

$sMsg &= "Name: " & $objItem.Name & @CRLF

$sMsg &= "Screen Height: " & $objItem.ScreenHeight & @CRLF

$sMsg &= "Screen Width: " & $objItem.ScreenWidth & @CRLF

Next

ConsoleWrite($sMsg & @CRLF)

 

Edited by JLogan3o13
Fixed formatting issue

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Link to comment
Share on other sites

Interesting...I'll have to do some digging to learn exactly how this works but for now it gets the job done. Here's how I adapted the code to fit my needs:

$strComputer = "."
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_DesktopMonitor")

$i = 0
For $objItem In $colItems
$i = $i + 1
Next
Global $w[$i], $h[$i]
$j = $i-1
$i = 0
For $objItem In $colItems
$w[$i] = $objItem.ScreenWidth
$h[$i] = $objItem.ScreenHeight
$i = $i + 1
Next

For $n = 0 to $j
MsgBox(0,"",$w[$n] & "x" & $h[$n])
Next

I know there has to be a shorter way to make this work but I can't figure it out. Any ideas?

Link to comment
Share on other sites

Global Const $SM_VIRTUALWIDTH = 78

Global Const $SM_VIRTUALHEIGHT = 79

$VIRTUALDESKTOPWIDTH = DLLCall("user32.dll","int","GetSystemMetrics","int",$SM_VIRTUALWIDTH)

$VIRTUALDESKTOPWIDTH = $VIRTUALDESKTOPWIDTH[0]

$VIRTUALDESKTOPHEIGHT = DLLCall("user32.dll","int","GetSystemMetrics","int",$SM_VIRTUALHEIGHT)

$VIRTUALDESKTOPHEIGHT = $VIRTUALDESKTOPHEIGHT[0]

f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Link to comment
Share on other sites

Global Const $SM_VIRTUALWIDTH = 78

Global Const $SM_VIRTUALHEIGHT = 79

$VIRTUALDESKTOPWIDTH = DLLCall("user32.dll","int","GetSystemMetrics","int",$SM_VIRTUALWIDTH)

$VIRTUALDESKTOPWIDTH = $VIRTUALDESKTOPWIDTH[0]

$VIRTUALDESKTOPHEIGHT = DLLCall("user32.dll","int","GetSystemMetrics","int",$SM_VIRTUALHEIGHT)

$VIRTUALDESKTOPHEIGHT = $VIRTUALDESKTOPHEIGHT[0]

As posted above:

$FW = DLLCall("user32.dll", "int", "GetSystemMetrics", "int", 78)
 $FH = DLLCall("user32.dll", "int", "GetSystemMetrics", "int", 79)
 $WA = $FW[0]
 $HA = $FH[0]

This code retrieves the total size of the V-Desktop. I need the width/height of each monitor being used, so I used the concept from Monamo's code to write a working script (posted above), but the code seems a little bulkier than it needs to be. I was wondering if there was a cleaner way of writing it that uses a better grasp on the concept which I don't have at the moment. The end result should be close to or the same as it is right now, giving me an array of width/height values.

Link to comment
Share on other sites

I use enumdisplaymonitors for this purpose, (I have a number of scripts to start apps on specific monitors, or on the monitor the mouse is on (from toolbars or other apps tool menus etc.))

here is the MultiMon.au3 I wrote and am currently using (could have a whole bunch more capability, but does what i need it to, the one thing I keep meaning to add is a custom MsgBox that I can place on a specific monitor)

It all seems to still work, (I just cleaned up the code a bit, added some minimal info above each function and renamed a bunch of variables ($t1, $t2, $h etc just doesn't cut it if someone else is looking at the code :P )

#include-Once
Global $__MonitorList[1][5]
$__MonitorList[0][0] = 0

; Just for testing
_ShowMonitorInfo()

;==================================================================================================
; Function Name:   _ShowMonitorInfo()
; Description::    Show the info in $__MonitorList in a msgbox (line 0 is entire screen)
; Parameter(s):    n/a
; Return Value(s): n/a
; Author(s):       xrxca (autoit@forums.xrx.ca)
;==================================================================================================
Func _ShowMonitorInfo()
    If $__MonitorList[0][0] == 0 Then
        _GetMonitors()
    EndIf
    Local $Msg = ""
    Local $i = 0
    For $i = 0 To $__MonitorList[0][0]
        $Msg &= $i & " - L:" & $__MonitorList[$i][1] & ", T:" & $__MonitorList[$i][2] 
        $Msg &= ", R:" & $__MonitorList[$i][3] & ", B:" & $__MonitorList[$i][4]
        If $i < $__MonitorList[0][0] Then $Msg &= @CRLF
    Next
    MsgBox(0, $__MonitorList[0][0] & " Monitors: ", $Msg)
EndFunc   ;==>_ShowMonitorInfo

;==================================================================================================
; Function Name:   _MaxOnMonitor($Title[, $Text = ''[, $Monitor = -1]])
; Description::    Maximize a window on a specific monitor (or the monitor the mouse is on)
; Parameter(s):    $Title   The title of the window to Move/Maximize
;     optional:    $Text    The text of the window to Move/Maximize
;     optional:    $Monitor The monitor to move to (1..NumMonitors) defaults to monitor mouse is on
; Note:            Should probably have specified return/error codes but haven't put them in yet
; Author(s):       xrxca (autoit@forums.xrx.ca)
;==================================================================================================
Func _MaxOnMonitor($Title, $Text = '', $Monitor = -1)
    _CenterOnMonitor($Title, $Text, $Monitor)
    WinSetState($Title, $Text, @SW_MAXIMIZE)
EndFunc   ;==>_MaxOnMonitor

;==================================================================================================
; Function Name:   _CenterOnMonitor($Title[, $Text = ''[, $Monitor = -1]])
; Description::    Center a window on a specific monitor (or the monitor the mouse is on)
; Parameter(s):    $Title   The title of the window to Move/Maximize
;     optional:    $Text    The text of the window to Move/Maximize
;     optional:    $Monitor The monitor to move to (1..NumMonitors) defaults to monitor mouse is on
; Note:            Should probably have specified return/error codes but haven't put them in yet
; Author(s):       xrxca (autoit@forums.xrx.ca)
;==================================================================================================
Func _CenterOnMonitor($Title, $Text = '', $Monitor = -1)
    $hWindow = WinGetHandle($Title, $Text)
    If Not @error Then
        If $Monitor == -1 Then
            $Monitor = _GetMonitorFromPoint()
        ElseIf $__MonitorList[0][0] == 0 Then
            _GetMonitors()
        EndIf
        If ($Monitor > 0) And ($Monitor <= $__MonitorList[0][0]) Then
            ; Restore the window if necessary
            Local $WinState = WinGetState($hWindow)
            If BitAND($WinState, 16) Or BitAND($WinState, 32) Then
                WinSetState($hWindow, '', @SW_RESTORE)
            EndIf
            Local $WinSize = WinGetPos($hWindow)
            Local $x = Int(($__MonitorList[$Monitor][3] - $__MonitorList[$Monitor][1] - $WinSize[2]) / 2) + $__MonitorList[$Monitor][1]
            Local $y = Int(($__MonitorList[$Monitor][4] - $__MonitorList[$Monitor][2] - $WinSize[3]) / 2) + $__MonitorList[$Monitor][2]
            WinMove($hWindow, '', $x, $y)
        EndIf
    EndIf
EndFunc   ;==>_CenterOnMonitor

;==================================================================================================
; Function Name:   _GetMonitorFromPoint([$XorPoint = -654321[, $Y = 0]])
; Description::    Get a monitor number from an x/y pos or the current mouse position
; Parameter(s):
;     optional:    $XorPoint X Position or Array with X/Y as items 0,1 (ie from MouseGetPos())
;     optional:    $Y        Y Position
; Note:            Should probably have specified return/error codes but haven't put them in yet,
;                  and better checking should be done on passed variables.
;                  Used to use MonitorFromPoint DLL call, but it didn't seem to always work.
; Author(s):       xrxca (autoit@forums.xrx.ca)
;==================================================================================================
Func _GetMonitorFromPoint($XorPoint = 0, $y = 0)
    If @NumParams = 0 then
        local $MousePos = MouseGetPos()
        Local $myX = $MousePos[0]
        Local $myY = $MousePos[1]
    Elseif ( @NumParams = 1 ) and IsArray($XorPoint) Then
        Local $myX = $XorPoint[0]
        Local $myY = $XorPoint[1]
    Else
        Local $myX = $XorPoint
        Local $myY = $y
    EndIf
    If $__MonitorList[0][0] == 0 Then
        _GetMonitors()
    EndIf
    Local $i = 0
    Local $Monitor = 0
    For $i = 1 To $__MonitorList[0][0]
        If ($myX >= $__MonitorList[$i][1]) _
                And ($myX < $__MonitorList[$i][3]) _
                And ($myY >= $__MonitorList[$i][2]) _
                And ($myY < $__MonitorList[$i][4]) Then $Monitor = $i
    Next
    Return $Monitor
EndFunc   ;==>_GetMonitorFromPoint

;==================================================================================================
; Function Name:   _GetMonitors()
; Description::    Load monitor positions
; Parameter(s):    n/a
; Return Value(s): 2D Array of Monitors
;                       [0][0] = Number of Monitors 
;                       [i][0] = HMONITOR handle of this monitor.
;                       [i][1] = Left Position of Monitor 
;                       [i][2] = Top Position of Monitor 
;                       [i][3] = Right Position of Monitor 
;                       [i][4] = Bottom Position of Monitor 
; Note:            [0][1..4] are set to Left,Top,Right,Bottom of entire screen
;                  hMonitor is returned in [i][0], but no longer used by these routines.
;                  Also sets $__MonitorList global variable (for other subs to use)
; Author(s):       xrxca (autoit@forums.xrx.ca)
;==================================================================================================
Func _GetMonitors()
    $__MonitorList[0][0] = 0  ;  Added so that the global array is reset if this is called multiple times
    Local $handle = DllCallbackRegister("_MonitorEnumProc", "int", "hwnd;hwnd;ptr;lparam")
    DllCall("user32.dll", "int", "EnumDisplayMonitors", "hwnd", 0, "ptr", 0, "ptr", DllCallbackGetPtr($handle), "lparam", 0)
    DllCallbackFree($handle)
    Local $i = 0
    For $i = 1 To $__MonitorList[0][0]
        If $__MonitorList[$i][1] < $__MonitorList[0][1] Then $__MonitorList[0][1] = $__MonitorList[$i][1]
        If $__MonitorList[$i][2] < $__MonitorList[0][2] Then $__MonitorList[0][2] = $__MonitorList[$i][2]
        If $__MonitorList[$i][3] > $__MonitorList[0][3] Then $__MonitorList[0][3] = $__MonitorList[$i][3]
        If $__MonitorList[$i][4] > $__MonitorList[0][4] Then $__MonitorList[0][4] = $__MonitorList[$i][4]
    Next
    Return $__MonitorList
EndFunc   ;==>_GetMonitors

;==================================================================================================
; Function Name:   _MonitorEnumProc($hMonitor, $hDC, $lRect, $lParam)
; Description::    Enum Callback Function for EnumDisplayMonitors in _GetMonitors
; Author(s):       xrxca (autoit@forums.xrx.ca)
;==================================================================================================
Func _MonitorEnumProc($hMonitor, $hDC, $lRect, $lParam)
    Local $Rect = DllStructCreate("int left;int top;int right;int bottom", $lRect)
    $__MonitorList[0][0] += 1
    ReDim $__MonitorList[$__MonitorList[0][0] + 1][5]
    $__MonitorList[$__MonitorList[0][0]][0] = $hMonitor
    $__MonitorList[$__MonitorList[0][0]][1] = DllStructGetData($Rect, "left")
    $__MonitorList[$__MonitorList[0][0]][2] = DllStructGetData($Rect, "top")
    $__MonitorList[$__MonitorList[0][0]][3] = DllStructGetData($Rect, "right")
    $__MonitorList[$__MonitorList[0][0]][4] = DllStructGetData($Rect, "bottom")
    Return 1 ; Return 1 to continue enumeration
EndFunc   ;==>_MonitorEnumProc

Edited Jan 8, 2009 to correct minor bug in _GetMonitors routine (Pointed out by Cusem).

Edited by xrxca
By far, the worst four letter word (swear word) out there has to be USER
Link to comment
Share on other sites

  • 9 years later...
  • Developers

@charles_su, What is the point of quoting an  9 years old post and not adding anything?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Moderators

@charles_su did you happen to notice this thread is over 9 years old? Please do not resurrect old threads pointlessly. Also, when commenting please just hit reply rather than quote, it pads the thread unnecessarily. 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • 11 months later...

A small refactoring....

#include-Once
; https://www.autoitscript.com/forum/topic/82353-dual-monitor-resolution-detection/?do=findComment&comment=1405494
#AutoIt3Wrapper_Run_AU3Check=Y
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

Global $__g_aMonitorList[1][5]
$__g_aMonitorList[0][0] = 0

; For testing - uncomment following line 'CTRL+Q in SciTE4AutoIt'
;~ If Not @Compiled Then _Example_ShowMonitorInfo()

; #FUNCTION# ====================================================================================================================
; Name ..........: _Example_ShowMonitorInfo
; Description ...: Show the info in $__g_aMonitorList in a msgbox (line 0 is entire screen)
; Syntax ........: _Example_ShowMonitorInfo()
; Parameters ....: None
; Return values .: None
; Author ........: xrxca (autoit@forums.xrx.ca)
; Modified ......: mLipok
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Example_ShowMonitorInfo()
    If $__g_aMonitorList[0][0] == 0 Then _GetMonitors()

    Local $sMessage = ""
    For $i = 0 To $__g_aMonitorList[0][0]
        $sMessage &= $i & " - L:" & $__g_aMonitorList[$i][1] & ", T:" & $__g_aMonitorList[$i][2]
        $sMessage &= ", R:" & $__g_aMonitorList[$i][3] & ", B:" & $__g_aMonitorList[$i][4]
        If $i < $__g_aMonitorList[0][0] Then $sMessage &= @CRLF
    Next
    MsgBox(0, $__g_aMonitorList[0][0] & " Monitors: ", $sMessage)
EndFunc   ;==>_Example_ShowMonitorInfo

; #FUNCTION# ====================================================================================================================
; Name ..........: _MaxOnMonitor
; Description ...: Maximize a window on a specific monitor (or the monitor the mouse is on)
; Syntax ........: _MaxOnMonitor($sTitle[, $sText = ''[, $iMonitor = -1]])
; Parameters ....: $sTitle              - a string value. The title/hWnd/class of the window to Move/Maximize
;                  $sText               - [optional] a string value. Default is ''. The text of the window to Move/Maximize
;                  $iMonitor            - [optional] an integer value. Default is -1. The monitor, to which window should be moved (1..NumMonitors). Use default -1 to select monitor on which mouse is on
; Return values .: None, or sets the @error flag to non-zero if the window is not found.
; Author ........: xrxca (autoit@forums.xrx.ca)
; Modified ......: mLipok
; Remarks .......:
; Related .......: WinGetHandle
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _MaxOnMonitor($sTitle, $sText = '', $iMonitor = -1)
    _CenterOnMonitor($sTitle, $sText, $iMonitor)
    If @error Then Return SetError(@error, @extended)
    WinSetState($sTitle, $sText, @SW_MAXIMIZE)
EndFunc   ;==>_MaxOnMonitor

; #FUNCTION# ====================================================================================================================
; Name ..........: _CenterOnMonitor
; Description ...: Center a window on a specific monitor (or the monitor the mouse is on)
; Syntax ........: _CenterOnMonitor($Title[, $sText = ''[, $iMonitor = -1]])
; Parameters ....: $Title               - an unknown value. The title/hWnd/class of the window to center on monitor
;                  $sText               - [optional] a string value. Default is ''. The text of the window to center on monitor
;                  $iMonitor            - [optional] an integer value. Default is -1. The monitor, to which window should be moved (1..NumMonitors). Use default -1 to select monitor on which mouse is on
; Return values .: None, or sets the @error flag to non-zero if the window is not found.
; Author ........: xrxca (autoit@forums.xrx.ca)
; Modified ......: mLipok
; Remarks .......: Should probably have specified return/error codes but haven't put them in yet
; Remarks .......:
; Related .......: WinGetHandle
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _CenterOnMonitor($Title, $sText = '', $iMonitor = -1)
    Local $hWindow = WinGetHandle($Title, $sText)
    If @error Then Return SetError(1)

    If $iMonitor == -1 Then $iMonitor = _GetMonitorFromPoint()
    If $__g_aMonitorList[0][0] == 0 Then _GetMonitors()

    If ($iMonitor > 0) And ($iMonitor <= $__g_aMonitorList[0][0]) Then
        ; Restore the window if necessary
        Local $WinState = WinGetState($hWindow)
        If BitAND($WinState, 16) Or BitAND($WinState, 32) Then
            WinSetState($hWindow, '', @SW_RESTORE)
        EndIf
        Local $WinSize = WinGetPos($hWindow)
        Local $x = Int(($__g_aMonitorList[$iMonitor][3] - $__g_aMonitorList[$iMonitor][1] - $WinSize[2]) / 2) + $__g_aMonitorList[$iMonitor][1]
        Local $y = Int(($__g_aMonitorList[$iMonitor][4] - $__g_aMonitorList[$iMonitor][2] - $WinSize[3]) / 2) + $__g_aMonitorList[$iMonitor][2]
        WinMove($hWindow, '', $x, $y)
    EndIf
EndFunc   ;==>_CenterOnMonitor

; #FUNCTION# ====================================================================================================================
; Name ..........: _GetMonitorFromPoint
; Description ...: Get a monitor number from an x/y pos or the current mouse position
; Syntax ........: _GetMonitorFromPoint([$XorPoint = 0[, $y = 0]])
; Parameters ....: $XorPoint            - [optional] an unknown value. Default is 0. X Position or Array with X/Y as items 0,1 (ie from MouseGetPos())
;                  $y                   - [optional] an unknown value. Default is 0. Y Position
; Return values .: $iMonitor, or set @error to 1
; Author ........: xrxca (autoit@forums.xrx.ca)
; Modified ......: mLipok
; Remarks .......: Used to use MonitorFromPoint DLL call, but it didn't seem to always work.
; Related .......: MouseGetPos
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _GetMonitorFromPoint($XorPoint = 0, $y = 0)
    Local $i_MouseX, $i_MouseY
    If @NumParams = 0 Then
        Local $aMousePos = MouseGetPos()
        $i_MouseX = $aMousePos[0]
        $i_MouseY = $aMousePos[1]
    ElseIf (@NumParams = 1) And IsArray($XorPoint) Then
        If UBound($XorPoint) <> 2 Then Return SetError(1, 1)
        $i_MouseX = $XorPoint[0]
        If Not IsInt($i_MouseX) Then Return SetError(2, 2)
        $i_MouseY = $XorPoint[1]
        If Not IsInt($i_MouseY) Then Return SetError(2, 3)
    Else
        If Not IsInt($XorPoint) Then Return SetError(2, 1)
        If Not IsInt($y) Then Return SetError(2, 2)
        $i_MouseX = $XorPoint
        $i_MouseY = $y
    EndIf
    If $__g_aMonitorList[0][0] == 0 Then _GetMonitors()

    Local $iMonitor = 0
    For $i = 1 To $__g_aMonitorList[0][0]
        If ($i_MouseX >= $__g_aMonitorList[$i][1]) _
                And ($i_MouseX < $__g_aMonitorList[$i][3]) _
                And ($i_MouseY >= $__g_aMonitorList[$i][2]) _
                And ($i_MouseY < $__g_aMonitorList[$i][4]) Then $iMonitor = $i
    Next
    Return $iMonitor
EndFunc   ;==>_GetMonitorFromPoint

; #FUNCTION# ====================================================================================================================
; Name ..........: _GetMonitors
; Description ...: Load monitor positions
; Syntax ........: _GetMonitors()
; Parameters ....: None
; Return values .: $__g_aMonitorList and 2D Array of Monitors
;                       [0][0] = Number of Monitors
;                       [i][0] = HMONITOR handle of this monitor.
;                       [i][1] = Left Position of Monitor
;                       [i][2] = Top Position of Monitor
;                       [i][3] = Right Position of Monitor
;                       [i][4] = Bottom Position of Monitor
; Author ........: xrxca (autoit@forums.xrx.ca)
; Modified ......: mLipok
; Remarks .......: [0][1..4] are set to Left,Top,Right,Bottom of entire screen
;                  hMonitor is returned in [i][0], but no longer used by these routines.
;                  Also sets $__g_aMonitorList Global variable (for other subs to use)
; Related .......:
; Link ..........:
; Example .......: _Example_ShowMonitorInfo()
; ===============================================================================================================================
Func _GetMonitors()
    $__g_aMonitorList[0][0] = 0 ;  Added so that the global array is reset if this is called multiple times
    Local $handle = DllCallbackRegister(__MonitorEnumProc, "int", "hwnd;hwnd;ptr;lparam")
    DllCall("user32.dll", "int", "EnumDisplayMonitors", "hwnd", 0, "ptr", 0, "ptr", DllCallbackGetPtr($handle), "lparam", 0)
    DllCallbackFree($handle)
    For $i = 1 To $__g_aMonitorList[0][0]
        If $__g_aMonitorList[$i][1] < $__g_aMonitorList[0][1] Then $__g_aMonitorList[0][1] = $__g_aMonitorList[$i][1]
        If $__g_aMonitorList[$i][2] < $__g_aMonitorList[0][2] Then $__g_aMonitorList[0][2] = $__g_aMonitorList[$i][2]
        If $__g_aMonitorList[$i][3] > $__g_aMonitorList[0][3] Then $__g_aMonitorList[0][3] = $__g_aMonitorList[$i][3]
        If $__g_aMonitorList[$i][4] > $__g_aMonitorList[0][4] Then $__g_aMonitorList[0][4] = $__g_aMonitorList[$i][4]
    Next
    Return $__g_aMonitorList
EndFunc   ;==>_GetMonitors

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __MonitorEnumProc
; Description ...: Enum Callback Function for EnumDisplayMonitors in _GetMonitors
; Syntax ........: __MonitorEnumProc($hMonitor, $hDC, $lRect, $lParam)
; Parameters ....: $hMonitor            - a handle value.
;                  $hDC                 - a handle value.
;                  $lRect               - an unknown value.
;                  $lParam              - an unknown value.
; Return values .: 1 and set $__g_aMonitorList
; Author ........: xrxca (autoit@forums.xrx.ca)
; Modified ......: mLipok
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __MonitorEnumProc($hMonitor, $hDC, $lRect, $lParam)
    #forceref $hDC, $lParam
    Local $tRect = DllStructCreate("int left;int top;int right;int bottom", $lRect)
    $__g_aMonitorList[0][0] += 1
    ReDim $__g_aMonitorList[$__g_aMonitorList[0][0] + 1][5]
    $__g_aMonitorList[$__g_aMonitorList[0][0]][0] = $hMonitor
    $__g_aMonitorList[$__g_aMonitorList[0][0]][1] = DllStructGetData($tRect, "left")
    $__g_aMonitorList[$__g_aMonitorList[0][0]][2] = DllStructGetData($tRect, "top")
    $__g_aMonitorList[$__g_aMonitorList[0][0]][3] = DllStructGetData($tRect, "right")
    $__g_aMonitorList[$__g_aMonitorList[0][0]][4] = DllStructGetData($tRect, "bottom")
    Return 1 ; Return 1 to continue enumeration
EndFunc   ;==>__MonitorEnumProc

 

Edited by mLipok
Added comments about - #INTERNAL_USE_ONLY#

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • 1 year later...
  • Developers

@dwaynek, Any good reason why you are cross posting this question while you have your own thread running already?

Please don't and simply stick to your original Thread!

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 6 months later...

I had a problem with @xrxca's fantastic MultiMon.au3, commented above, with my Windows 10 64-bit configuration. I have four displays, of which only two are the same size. My PC's BIOS displays boot messages on the left monitor, but Windows thinks that it's monitor #2, so I have my Display Settings like the attachment shows ( [2] [1] [3] [4] ).

I had to add a few lines to MultiMon.au3 to show the monitors in the correct physical representation, because it was showing Display 2 (primary display) as the 4th hMonitor instead of the 1st. I put an _ArraySort in _GetMonitors() to fix this (at least on my system). I also added a line to the example _ShowMonitorInfo() to provide the hMonitor value(s). See attached "before after" screenshot for the result.

#include-Once
#include<Array.au3> ;added to fix the display order

Global $__MonitorList[1][5]
$__MonitorList[0][0] = 0

; Just for testing
;~ _ArrayDisplay(_GetMonitors()) ;added as another way to see the info
_ShowMonitorInfo()

;==================================================================================================
; Function Name:   _ShowMonitorInfo()
; Description::    Show the info in $__MonitorList in a msgbox (line 0 is count of monitors and entire Desktop size)
; Parameter(s):    n/a
; Return Value(s): n/a
; Author(s):       xrxca (autoit@forums.xrx.ca)
;==================================================================================================
Func _ShowMonitorInfo()
    If $__MonitorList[0][0] == 0 Then
        _GetMonitors()
    EndIf
    Local $Msg = ""
    Local $i = 0

    For $i = 0 To $__MonitorList[0][0]
        $Msg &= $i & " - hnd:" & $__MonitorList[$i][0] ;added for information
        $Msg &= ", L:" & $__MonitorList[$i][1] & ", T:" & $__MonitorList[$i][2]
        $Msg &= ", R:" & $__MonitorList[$i][3] & ", B:" & $__MonitorList[$i][4]
        If $i < $__MonitorList[0][0] Then $Msg &= @CRLF
    Next
    MsgBox(0, $__MonitorList[0][0] & " Monitors: ", $Msg)
EndFunc   ;==>_ShowMonitorInfo

;==================================================================================================
; Function Name:   _MaxOnMonitor($Title[, $Text = ''[, $Monitor = -1]])
; Description::    Maximize a window on a specific monitor (or the monitor the mouse is on)
; Parameter(s):    $Title   The title of the window to Move/Maximize
;     optional:    $Text    The text of the window to Move/Maximize
;     optional:    $Monitor The monitor to move to (1..NumMonitors) defaults to monitor mouse is on
; Note:            Should probably have specified return/error codes but haven't put them in yet
; Author(s):       xrxca (autoit@forums.xrx.ca)
;==================================================================================================
Func _MaxOnMonitor($Title, $Text = '', $Monitor = -1)
    _CenterOnMonitor($Title, $Text, $Monitor)
    WinSetState($Title, $Text, @SW_MAXIMIZE)
EndFunc   ;==>_MaxOnMonitor

;==================================================================================================
; Function Name:   _CenterOnMonitor($Title[, $Text = ''[, $Monitor = -1]])
; Description::    Center a window on a specific monitor (or the monitor the mouse is on)
; Parameter(s):    $Title   The title of the window to Move/Maximize
;     optional:    $Text    The text of the window to Move/Maximize
;     optional:    $Monitor The monitor to move to (1..NumMonitors) defaults to monitor mouse is on
; Note:            Should probably have specified return/error codes but haven't put them in yet
; Author(s):       xrxca (autoit@forums.xrx.ca)
;==================================================================================================
Func _CenterOnMonitor($Title, $Text = '', $Monitor = -1)
    $hWindow = WinGetHandle($Title, $Text)
    If Not @error Then
        If $Monitor == -1 Then
            $Monitor = _GetMonitorFromPoint()
        ElseIf $__MonitorList[0][0] == 0 Then
            _GetMonitors()
        EndIf
        If ($Monitor > 0) And ($Monitor <= $__MonitorList[0][0]) Then
            ; Restore the window if necessary
            Local $WinState = WinGetState($hWindow)
            If BitAND($WinState, 16) Or BitAND($WinState, 32) Then
                WinSetState($hWindow, '', @SW_RESTORE)
            EndIf
            Local $WinSize = WinGetPos($hWindow)
            Local $x = Int(($__MonitorList[$Monitor][3] - $__MonitorList[$Monitor][1] - $WinSize[2]) / 2) + $__MonitorList[$Monitor][1]
            Local $y = Int(($__MonitorList[$Monitor][4] - $__MonitorList[$Monitor][2] - $WinSize[3]) / 2) + $__MonitorList[$Monitor][2]
            WinMove($hWindow, '', $x, $y)
        EndIf
    EndIf
EndFunc   ;==>_CenterOnMonitor

;==================================================================================================
; Function Name:   _GetMonitorFromPoint([$XorPoint = -654321[, $Y = 0]])
; Description::    Get a monitor number from an x/y pos or the current mouse position
; Parameter(s):
;     optional:    $XorPoint X Position or Array with X/Y as items 0,1 (ie from MouseGetPos())
;     optional:    $Y        Y Position
; Note:            Should probably have specified return/error codes but haven't put them in yet,
;                  and better checking should be done on passed variables.
;                  Used to use MonitorFromPoint DLL call, but it didn't seem to always work.
; Author(s):       xrxca (autoit@forums.xrx.ca)
;==================================================================================================
Func _GetMonitorFromPoint($XorPoint = 0, $y = 0)
    If @NumParams = 0 then
        local $MousePos = MouseGetPos()
        Local $myX = $MousePos[0]
        Local $myY = $MousePos[1]
    Elseif ( @NumParams = 1 ) and IsArray($XorPoint) Then
        Local $myX = $XorPoint[0]
        Local $myY = $XorPoint[1]
    Else
        Local $myX = $XorPoint
        Local $myY = $y
    EndIf
    If $__MonitorList[0][0] == 0 Then
        _GetMonitors()
    EndIf
    Local $i = 0
    Local $Monitor = 0
    For $i = 1 To $__MonitorList[0][0]
        If ($myX >= $__MonitorList[$i][1]) _
                And ($myX < $__MonitorList[$i][3]) _
                And ($myY >= $__MonitorList[$i][2]) _
                And ($myY < $__MonitorList[$i][4]) Then $Monitor = $i
    Next
    Return $Monitor
EndFunc   ;==>_GetMonitorFromPoint

;==================================================================================================
; Function Name:   _GetMonitors()
; Description::    Load monitor positions
; Parameter(s):    n/a
; Return Value(s): 2D Array of Monitors
;                       [0][0] = Number of Monitors
;                       [i][0] = HMONITOR handle of this monitor.
;                       [i][1] = Left Position of Monitor
;                       [i][2] = Top Position of Monitor
;                       [i][3] = Right Position of Monitor
;                       [i][4] = Bottom Position of Monitor
; Note:            [0][1..4] are set to Left,Top,Right,Bottom of entire screen
;                  hMonitor is returned in [i][0], but no longer used by these routines.
;                  Also sets $__MonitorList global variable (for other subs to use)
; Author(s):       xrxca (autoit@forums.xrx.ca)
;==================================================================================================
Func _GetMonitors()
    $__MonitorList[0][0] = 0  ;  Added so that the global array is reset if this is called multiple times
    Local $handle = DllCallbackRegister("_MonitorEnumProc", "int", "hwnd;hwnd;ptr;lparam")
    DllCall("user32.dll", "int", "EnumDisplayMonitors", "hwnd", 0, "ptr", 0, "ptr", DllCallbackGetPtr($handle), "lparam", 0)
    DllCallbackFree($handle)
    Local $i = 0
    For $i = 1 To $__MonitorList[0][0]
        If $__MonitorList[$i][1] < $__MonitorList[0][1] Then $__MonitorList[0][1] = $__MonitorList[$i][1]
        If $__MonitorList[$i][2] < $__MonitorList[0][2] Then $__MonitorList[0][2] = $__MonitorList[$i][2]
        If $__MonitorList[$i][3] > $__MonitorList[0][3] Then $__MonitorList[0][3] = $__MonitorList[$i][3]
        If $__MonitorList[$i][4] > $__MonitorList[0][4] Then $__MonitorList[0][4] = $__MonitorList[$i][4]
    Next

    _ArraySort($__MonitorList, 0, 1, 0, 0, 0) ;added to sort it by the first column (hMonitor) so that they line up in physical order like Display Settings-->Rearrange Displays
    Return $__MonitorList
EndFunc   ;==>_GetMonitors

;==================================================================================================
; Function Name:   _MonitorEnumProc($hMonitor, $hDC, $lRect, $lParam)
; Description::    Enum Callback Function for EnumDisplayMonitors in _GetMonitors
; Author(s):       xrxca (autoit@forums.xrx.ca)
;==================================================================================================
Func _MonitorEnumProc($hMonitor, $hDC, $lRect, $lParam)
    Local $Rect = DllStructCreate("int left;int top;int right;int bottom", $lRect)
    $__MonitorList[0][0] += 1
    ReDim $__MonitorList[$__MonitorList[0][0] + 1][5]
    $__MonitorList[$__MonitorList[0][0]][0] = $hMonitor
    $__MonitorList[$__MonitorList[0][0]][1] = DllStructGetData($Rect, "left")
    $__MonitorList[$__MonitorList[0][0]][2] = DllStructGetData($Rect, "top")
    $__MonitorList[$__MonitorList[0][0]][3] = DllStructGetData($Rect, "right")
    $__MonitorList[$__MonitorList[0][0]][4] = DllStructGetData($Rect, "bottom")
    Return 1 ; Return 1 to continue enumeration
EndFunc   ;==>_MonitorEnumProc

Now when I use _CenterOnMonitor, it correctly moves the specified WindowTitle to the specified Monitor number.

_CenterOnMonitor("Notepad", "", 1) ;leftmost
Sleep(2000)
_CenterOnMonitor("Notepad", "", 2) ;middle left
Sleep(2000)
_CenterOnMonitor("Notepad", "", 3) ;middle right
Sleep(2000)
_CenterOnMonitor("Notepad", "", 4) ;rightmost

Edit: I didn't find MultiMon.au3 anywhere in the AutoIt Examples pages to follow up with my 2 cents, so I stuck it here.

displays.png

before after.png

Edited by Decibel
Link to comment
Share on other sites

  • 10 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...