Moves and/or resizes and arrange windows between multi monitors
_WinSideBySide
Moves and/or resizes and arrange windows between multi monitors.
_WinSideBySide ( $hWin1 [, $hWin2 = 0 [, $iView1 = 65 [, $sDevice = "Primary"]]])
Parameters
$hWin1 The title/hWnd/class of the window1.
$hWin2 [optional] The title/hWnd/class of the window2.
$iView1 [optional] Percentage of the current @DesktopWidth, for window1.
Negative value start from the right side
$sDevice [optional] The name of the device to move to, e.g. \\.\DISPLAY1 , \\.\DISPLAY2 , "Primary" is the primary monitor
Return Value
NoneRemarks
If $hWin2 = 0 then will be ignored, and act only to window1
_WinMoveOn
Moves and/or resizes a window between multi monitors._WinMoveOn ( $hWin, $iX, $iY, [$iWidth = Default [, $iHeigh = Default [, $iSpeed = -1 [, $sDevice = "Primary" [, $Scale = False]]]]] )
Parameters
$hWin The title/hWnd/class of the window to move/resize.
$iX X coordinate to move to.
$iY Y coordinate to move to.
$iWidth [optional] New width of the window.
$iHeigh [optional] New height of the window.
$iSpeed [optional] The speed to move the windows in the range 1 (fastest) to 100 (slowest). If not defined the move is instantaneous.
$sDevice [optional] The name of the device to move to, e.g. \\.\DISPLAY1 , \\.\DISPLAY2 , "Primary" is the primary monitor
$Scale [optional] True/False. If True the dimensions will be multiplied with the scale factor
Return Value
Success: a handle to the window.
Failure: 0 if the window is not found.
Remarks
In $iX, $iY, $iWidth, $iHeigh parameters can use string expression with the variable $DtW = current @DesktopWidth , $DtH = current @DesktopHeight
Example: _WinMoveOn( $hWin_1, "$DtW / 3", "$DtH / 5", "$DtW / 2", "$DtH / 2" , -1, "\\.\DISPLAY2")
Tested on Windows 10 Version 22H2
main setting
; https://www.autoitscript.com/forum/topic/210826-moves-andor-resizes-and-arrange-windows-between-multi-monitors/ ;---------------------------------------------------------------------------------------- ; Title...........: _WinSideBySide.au3 ; Description.....: Moves and arrange windows between multi monitors ; AutoIt Version..: 3.3.16.1 Author: ioa747 Fileversion=0.0.0.1 ;---------------------------------------------------------------------------------------- ;~ #include-once #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #AutoIt3Wrapper_UseX64=n DllCall("user32.dll", "bool", "SetProcessDpiAwarenessContext", "int", -4) ; -4=PerMonitorAwareV2 ;https://www.autoitscript.com/forum/topic/210687-_screencapture_capturewnd-function-taking-different-snap-on-window-11/#comment-1522810 ;-------------------------------- ___ ;~ #include <_WinAPI_DPI.au3> |__ \ ;~ #include <DPIAwareness.au3> / / ;~ _WinAPI_SetDPIAwareness() |_| ;-------------------------------- (_) #include <Array.au3> #include <WinAPIGdi.au3> #include <WindowsConstants.au3> _Example() ;---------------------------------------------------------------------------------------- Func _Example() Local $sFile_1 = @ScriptDir & '\Test_1.txt' Local $sFile_2 = @ScriptDir & '\Test_2.txt' If Not FileExists($sFile_1) Then FileWriteLine($sFile_1, "Win_1") If Not FileExists($sFile_2) Then FileWriteLine($sFile_2, "Win_2") Local $hWin_1 = WinWait("Test_1.txt - Notepad", "", 1) If Not WinExists($hWin_1) Then ShellExecute("notepad.exe", $sFile_1) $hWin_1 = WinWait("Test_1.txt - Notepad") ConsoleWrite("- $hWin_1=" & $hWin_1 & @CRLF) Local $hWin_2 = WinWait("Test_2.txt - Notepad", "", 1) If Not WinExists($hWin_2) Then ShellExecute("notepad.exe", $sFile_2) $hWin_2 = WinWait("Test_2.txt - Notepad") ConsoleWrite("- $hWin_2=" & $hWin_2 & @CRLF) Local $Tip, $Wait = 5000 ;******************************************************** ;Start with _WinSideBySide() Examples $Tip = "-------------------------------------------------" ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "Start with _WinSideBySide() Examples", 1) Sleep($Wait) $Tip = " Win_1 30% on the left side on Primary monitor" ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "_WinSideBySide()", 1) _WinSideBySide($hWin_1, $hWin_2, 30) Sleep($Wait) $Tip = " Win_1 70% on the left side on DISPLAY2" ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "_WinSideBySide()", 1) _WinSideBySide($hWin_1, $hWin_2, 70, "\\.\DISPLAY2") Sleep($Wait) $Tip = " Win_1 70% on the right side on Primary monitor" ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "_WinSideBySide()", 1) _WinSideBySide($hWin_1, $hWin_2, -70) Sleep($Wait) WinClose($hWin_2) $Tip = " Win_1 alone 70% on the left side on Primary monitor" ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "_WinSideBySide()", 1) _WinSideBySide($hWin_1, 0, 70) Sleep($Wait) $Tip = " Win_1 alone 70% on the right side on Primary monitor" ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "_WinSideBySide()", 1) _WinSideBySide($hWin_1, 0, -70) Sleep($Wait) ;******************************************************** ;Start with _WinMoveOn() Examples $Tip = "-------------------------------------------------" ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "Start with _WinMoveOn() Examples", 1) Sleep($Wait) $Tip = " ($hWin_1, 0, 0, 600, 300, -1, '\\.\DISPLAY1')" ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "_WinMoveOn()", 1) _WinMoveOn($hWin_1, 0, 0, 600, 300, -1, "\\.\DISPLAY1") Sleep($Wait) $Tip = " ($hWin_1, 0, 300, Default, Default, -1, '\\.\DISPLAY1')" ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "_WinMoveOn()", 1) _WinMoveOn($hWin_1, 0, 300, Default, Default, -1, "\\.\DISPLAY1") Sleep($Wait) $Tip = " ($hWin_1, 0, 0, 600, 300, -1, '\\.\DISPLAY2', False)" ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "_WinMoveOn()", 1) _WinMoveOn($hWin_1, 0, 0, 600, 300, -1, "\\.\DISPLAY2", False) Sleep($Wait) $Tip = " ($hWin_1, 0, 0, 600, 300, -1, '\\.\DISPLAY2', True)" ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "_WinMoveOn()", 1) _WinMoveOn($hWin_1, 0, 0, 600, 300, -1, "\\.\DISPLAY2", True) Sleep($Wait) ;******************************************************** ;Start with _WinMoveOn() Examples $Tip = "-------------------------------------------------" ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "Start with Special parameters Examples", 1) Sleep($Wait) $Tip = '($hWin_1, "($DtW / 2) - 300", "($DtH / 2) - 150", 600, 300)' ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "_WinMoveOn() Special parameters", 1) _WinMoveOn($hWin_1, "($DtW / 2) - 300", "($DtH / 2) - 150", 600, 300) Sleep($Wait) $Tip = '($hWin_1, "($DtW / 2) - ($DtW / 4)", "($DtH / 2) - ($DtH / 4)", "($DtW / 2)", "($DtH / 2)", -1, "\\.\DISPLAY2")' ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "_WinMoveOn() Special parameters", 1) _WinMoveOn($hWin_1, "($DtW / 2) - ($DtW / 4)", "($DtH / 2) - ($DtH / 4)", "($DtW / 2)", "($DtH / 2)", -1, "\\.\DISPLAY2") Sleep($Wait) $Tip = '($hWin_1, "$DtW / 3", "$DtH / 5", "$DtW / 2", "$DtH / 2")' ToolTip($Tip, @DesktopWidth / 2, @DesktopHeight / 2, "_WinMoveOn() Special parameters", 1) _WinMoveOn($hWin_1, "$DtW / 3", "$DtH / 5", "$DtW / 2", "$DtH / 2") Sleep($Wait) ;******************************************************** ToolTip("") WinClose($hWin_1) (expression) ? (expression1_if_expression_is_True) : (expression2_if_expression_is_False) EndFunc ;==>_Example ;---------------------------------------------------------------------------------------- ; #FUNCTION# ---------------------------------------------------------------------------- ; Name...........: _WinMoveOn ; Description ...: Moves and/or resizes a window between multi monitors. ; Syntax.........: _WinMoveOn ( $hWin, $iX, $iY, [$iWidth = Default [, $iHeigh = Default [, $iSpeed = -1 [, $sDevice = "Primary" [, $Scale = False]]]]] ) ; Parameters ....: $hWin - The title/hWnd/class of the window to move/resize. ; $iX - X coordinate to move to. ; $iY - Y coordinate to move to. ; $iWidth - [optional] New width of the window ; $iHeigh - [optional] New height of the window. ; $iSpeed - [optional] The speed to move the windows in the range 1 (fastest) to 100 (slowest). If not defined the move is instantaneous ; $sDevice - [optional] The name of the device to move to, e.g. \\.\DISPLAY1 , \\.\DISPLAY2 , "Primary" is the primary monitor ; $Scale - [optional] True/False. If True the dimensions will be multiplied with the scale factor ; Return value...: Success: a handle to the window. Failure: 0 if the window is not found. ; Author ........: ioa747 ; Notes .........: In $iX, $iY, $iWidth, $iHeigh parameters can use string expression with the variable $DtW = current @DesktopWidth , $DtH = current @DesktopHeight ;---------------------------------------------------------------------------------------- Func _WinMoveOn($hWin, $iX, $iY, $iWidth = Default, $iHeigh = Default, $iSpeed = -1, $sDevice = "Primary", $Scale = False) Local Enum $hM, $RX, $RY, $RW, $RH, $Dv, $Pr, $WX, $WY, $WW, $WH, $Sc Local $Cur Local $aMon = _GetMonitorsArray() If IsArray($aMon) Then For $i = 1 To $aMon[0][0] If $sDevice = "Primary" Then If $aMon[$i][$Pr] = 1 Then $Cur = $i Else If $aMon[$i][$Dv] = $sDevice Then $Cur = $i EndIf Next Else ConsoleWrite("! @error(1) in Func _WinMoveOn(), Parameter $sDevice" & @CRLF) Return SetError(1, @extended, 0) EndIf If Not $Cur Then ConsoleWrite("! @error(2) in Func _WinMoveOn(), can't find $sDevice" & @CRLF) Return SetError(2, @extended, 0) EndIf Local $iScale, $iwX, $iwY, $iwW, $iwH, $awPos, $DtW, $DtH $DtW = $aMon[$Cur][$RW] $DtH = $aMon[$Cur][$RH] ConsoleWrite("==================================" & @CRLF) ConsoleWrite("Device = " & $aMon[$Cur][$Dv] & @CRLF) ConsoleWrite("RectWidth = " & $DtW & @CRLF) ConsoleWrite("RectHeight = " & $DtH & @CRLF) ConsoleWrite("VarGetType($iX)=" & VarGetType($iX) & @CRLF) If $Scale = False Then $iScale = 1 Else $iScale = $aMon[$Cur][$Sc] EndIf ConsoleWrite("$iScale = " & $iScale & @CRLF) ; Win1 ------------------------------------------------------------------------ If WinExists($hWin) Then $awPos = WinGetPos($hWin) ConsoleWrite("- WPos:" & $awPos[0] & ", " & $awPos[1] & ", " & $awPos[2] & ", " & $awPos[3] & @CRLF) If VarGetType($iX) = "Int32" Then $iwX = $aMon[$Cur][$WX] + ($iX * $iScale) - (7 * $aMon[$Cur][$Sc]) Else $iwX = $aMon[$Cur][$WX] + (Execute($iX)) - (7 * $aMon[$Cur][$Sc]) EndIf If VarGetType($iY) = "Int32" Then $iwY = $aMon[$Cur][$WY] + ($iY * $iScale) Else $iwY = $aMon[$Cur][$WY] + (Execute($iY)) EndIf If $iWidth = Default Then $iwW = $awPos[2] * $iScale Else If VarGetType($iWidth) = "Int32" Then $iwW = ($iWidth * $iScale) + (14 * $aMon[$Cur][$Sc]) Else $iwW = (Execute($iWidth)) + (14 * $aMon[$Cur][$Sc]) EndIf EndIf If $iHeigh = Default Then $iwH = $awPos[3] * $iScale Else If VarGetType($iHeigh) = "Int32" Then $iwH = ($iHeigh * $iScale) + (7 * $aMon[$Cur][$Sc]) Else $iwH = (Execute($iHeigh)) + (7 * $aMon[$Cur][$Sc]) EndIf EndIf WinMove($hWin, "", $iwX, $iwY, $iwW, $iwH, $iSpeed) Sleep(200) ConsoleWrite("-> Win1:" & $iwX & ", " & $iwY & ", " & $iwW & ", " & $iwH & @CRLF) $awPos = WinGetPos($hWin) ConsoleWrite("<- WPos:" & $awPos[0] & ", " & $awPos[1] & ", " & $awPos[2] & ", " & $awPos[3] & @CRLF) If $iwW <> $awPos[2] Then WinMove($hWin, "", $iwX, $iwY, $iwW, $iwH) Sleep(200) $awPos = WinGetPos($hWin) ConsoleWrite("<<- WPos:" & $awPos[0] & ", " & $awPos[1] & ", " & $awPos[2] & ", " & $awPos[3] & @CRLF) EndIf ;give focus to $hWin WinActivate($hWin) Return $hWin Else ConsoleWrite("! @error(3) in Func _WinMoveOn(), can't find $hWin" & @CRLF) Return SetError(3, @extended, 0) EndIf ;~ If $iView1 > 0 Then ;~ $iwX = $aMon[$Cur][$WX] - (7 * $aMon[$Cur][$Sc]) ;~ $iwY = $aMon[$Cur][$WY] ;~ $iwW = ($iScale * $iView1) + (14 * $aMon[$Cur][$Sc]) ;~ $iwH = $aMon[$Cur][$WH] + (7 * $aMon[$Cur][$Sc]) ;~ Else ;~ $iwX = ($aMon[$Cur][$WW] - ($iScale * Abs($iView1))) - (7 * $aMon[$Cur][$Sc]) ;~ $iwY = $aMon[$Cur][$WY] ;~ $iwW = ($iScale * Abs($iView1)) + (14 * $aMon[$Cur][$Sc]) ;~ $iwH = $aMon[$Cur][$WH] + (7 * $aMon[$Cur][$Sc]) ;~ EndIf EndFunc ;==>_WinMoveOn ;---------------------------------------------------------------------------------------- ; #FUNCTION# ---------------------------------------------------------------------------- ; Name...........: _WinSideBySide ; Description ...: Moves and/or resizes and arrange windows between multi monitors. ; Syntax.........: _WinSideBySide ( $hWin1 [, $hWin2 = 0 [, $iView1 = 65 [, $sDevice = "Primary"]]]) ; Parameters ....: $hWin1 - The title/hWnd/class of the window1, ; $hWin2 - [optional] The title/hWnd/class of the window2. ; $iView1 - [optional] Percentage of the current @DesktopWidth, for window1. Negative value start from the right side ; $sDevice - [optional] The name of the device to move to, e.g. \\.\DISPLAY1 , \\.\DISPLAY2 , "Primary" is the primary monitor ; Return value...: None ; Author ........: ioa747 ; Notes .........: If $hWin2 = 0 then will be ignored, and act only to window1 ;---------------------------------------------------------------------------------------- Func _WinSideBySide($hWin1, $hWin2 = 0, $iView1 = 65, $sDevice = "Primary") Local Enum $hM, $RX, $RY, $RW, $RH, $Dv, $Pr, $WX, $WY, $WW, $WH, $Sc Local $Cur Local $aMon = _GetMonitorsArray() If IsArray($aMon) Then For $i = 1 To $aMon[0][0] If $sDevice = "Primary" Then If $aMon[$i][$Pr] = 1 Then $Cur = $i Else If $aMon[$i][$Dv] = $sDevice Then $Cur = $i EndIf Next Else ConsoleWrite("! @error(1) in Func _WinSideBySide(), Parameter $sDevice" & @CRLF) Return SetError(1, @extended, 0) EndIf If Not $Cur Then ConsoleWrite("! @error(2) in Func _WinSideBySide(), can't find $sDevice" & @CRLF) Return SetError(2, @extended, 0) EndIf ConsoleWrite("==================================" & @CRLF) ConsoleWrite("Device = " & $aMon[$Cur][$Dv] & @CRLF) ConsoleWrite("RectWidth = " & $aMon[$Cur][$RW] & @CRLF) ConsoleWrite("RectHeight = " & $aMon[$Cur][$RH] & @CRLF) Local $iScale, $iwX, $iwY, $iwW, $iwH, $awPos $iScale = ($aMon[$Cur][$RW] / 100) ; = 1% ConsoleWrite("$iScale = 1% = " & $iScale & @CRLF) ; Win1 ------------------------------------------------------------------------ If $iView1 > 0 Then $iwX = $aMon[$Cur][$WX] - (7 * $aMon[$Cur][$Sc]) $iwY = $aMon[$Cur][$WY] $iwW = ($iScale * $iView1) + (14 * $aMon[$Cur][$Sc]) $iwH = $aMon[$Cur][$WH] + (7 * $aMon[$Cur][$Sc]) Else $iwX = ($aMon[$Cur][$WW] - ($iScale * Abs($iView1))) - (7 * $aMon[$Cur][$Sc]) $iwY = $aMon[$Cur][$WY] $iwW = ($iScale * Abs($iView1)) + (14 * $aMon[$Cur][$Sc]) $iwH = $aMon[$Cur][$WH] + (7 * $aMon[$Cur][$Sc]) EndIf ConsoleWrite(@CRLF & "Win1 ~~~~~~~~~~~~~~~~~~" & @CRLF) WinMove($hWin1, "", $iwX, $iwY, $iwW, $iwH) Sleep(200) ConsoleWrite("-> Win1:" & $iwX & ", " & $iwY & ", " & $iwW & ", " & $iwH & @CRLF) $awPos = WinGetPos($hWin1) ConsoleWrite("<- WPos:" & $awPos[0] & ", " & $awPos[1] & ", " & $awPos[2] & ", " & $awPos[3] & @CRLF) If $iwW <> $awPos[2] Then WinMove($hWin1, "", $iwX, $iwY, $iwW, $iwH) Sleep(200) $awPos = WinGetPos($hWin1) ConsoleWrite("<<- WPos:" & $awPos[0] & ", " & $awPos[1] & ", " & $awPos[2] & ", " & $awPos[3] & @CRLF) EndIf ; Win2 ------------------------------------------------------------------------ If $hWin2 Then If $iView1 > 0 Then $iwX = $aMon[$Cur][$WX] + ($iScale * $iView1) - (7 * $aMon[$Cur][$Sc]) $iwY = $aMon[$Cur][$WY] $iwW = $aMon[$Cur][$RW] + (14 * $aMon[$Cur][$Sc]) - ($iScale * $iView1) $iwH = $aMon[$Cur][$WH] + (7 * $aMon[$Cur][$Sc]) Else $iwX = $aMon[$Cur][$WX] - (7 * $aMon[$Cur][$Sc]) $iwY = $aMon[$Cur][$WY] $iwW = $aMon[$Cur][$RW] + (14 * $aMon[$Cur][$Sc]) - ($iScale * Abs($iView1)) $iwH = $aMon[$Cur][$WH] + (7 * $aMon[$Cur][$Sc]) EndIf ConsoleWrite(@CRLF & "Win2 ~~~~~~~~~~~~~~~~~~" & @CRLF) WinMove($hWin2, "", $iwX, $iwY, $iwW, $iwH) Sleep(200) ConsoleWrite("-> Win2:" & $iwX & ", " & $iwY & ", " & $iwW & ", " & $iwH & @CRLF) $awPos = WinGetPos($hWin2) ConsoleWrite("<- WPos:" & $awPos[0] & ", " & $awPos[1] & ", " & $awPos[2] & ", " & $awPos[3] & @CRLF) If $iwW <> $awPos[2] Then WinMove($hWin2, "", $iwX, $iwY, $iwW, $iwH) Sleep(200) $awPos = WinGetPos($hWin2) ConsoleWrite("<<- WPos:" & $awPos[0] & ", " & $awPos[1] & ", " & $awPos[2] & ", " & $awPos[3] & @CRLF) EndIf ConsoleWrite("" & @CRLF) EndIf ;give focus to $hWin2 WinActivate($hWin2) ;give focus to $hWin1 WinActivate($hWin1) EndFunc ;==>_WinSideBySide ;---------------------------------------------------------------------------------------- Func _GetMonitorsArray() ;0=hM, 1=RX, 2=RY, 3=RW, 4=RH, 5=Dv, 6=Pr, 7=WX, 8=WY, 9=WW, 10=WH, 11=Sc ;Local Enum $hM, $RX, $RY, $RW, $RH, $Dv, $Pr, $WX, $WY, $WW, $WH, $Sc Local $aPos, $aMonitorInfo, $aData = _WinAPI_EnumDisplayMonitors() If IsArray($aData) Then ReDim $aData[$aData[0][0] + 1][12] For $i = 1 To $aData[0][0] $aPos = _WinAPI_GetPosFromRect($aData[$i][1]) For $j = 0 To 3 $aData[$i][$j + 1] = $aPos[$j] Next $aMonitorInfo = _WinAPI_GetMonitorInfo($aData[$i][0]) $aData[$i][5] = $aMonitorInfo[3] ;Device name $aData[$i][6] = $aMonitorInfo[2] ;Primary $aData[$i][7] = DllStructGetData($aMonitorInfo[1], 1) ;wx $aData[$i][8] = DllStructGetData($aMonitorInfo[1], 2) ;wy $aData[$i][9] = DllStructGetData($aMonitorInfo[1], 3) ;ww $aData[$i][10] = DllStructGetData($aMonitorInfo[1], 4) ;wh $aData[$i][11] = _WinAPI_EnumDisplaySettings($aMonitorInfo[3], $ENUM_CURRENT_SETTINGS)[0] / @DesktopWidth Next EndIf ;_ArrayDisplay($aData, "", "", 0, Default, "0=hM|1=RX|2=RY|3=RW|4=RH|5=Dv|6=Pr|7=WX|8=WY|9=WW|10=WH|11=Sc") Return $aData EndFunc ;==>_GetMonitorsArray ;----------------------------------------------------------------------------------------
Comments
Post a Comment