Search code examples
autohotkeyautohotkey-2

I am trying to move my windows but WinMove doesn't work


I am trying to swap windows on my screen. The MsgBoxes work but WinMove doesn't:

{
...
; Move from Monitor 1 to Monitor 2
    if (WinX >= Monitor1X && WinX < Monitor1EndX) {
        SwapToScreen(thisID, Monitor2X, WinX, WinY, Monitor1X)
        MsgBox , 1 to 2, %Title%
    }
    ; Move from Monitor 2 to Monitor 1
    else if (WinX >= Monitor2X && WinX < Monitor2EndX) {
        SwapToScreen(thisID, Monitor1X, WinX, WinY, Monitor2X)
        MsgBox, 2 to 1, %Title%
    }
    ; Handle fullscreen cases (X = -9)
    else if (WinX < 0 || WinY < 0) {
        WinGet, IsMaximized, MinMax, ahk_id %thisID%
        if (IsMaximized) {
            SwapToScreen(thisID, Monitor2X, Monitor1X, WinY, Monitor1X)
            WinRestore, ahk_id %thisID%
        }
        if (IsMaximized) {
            WinMaximize, ahk_id %thisID%
        }
    }
    else {
        MsgBox, Active window is not within defined screen boundaries.
    }
}
}

SwapToScreen(ActiveWinID, TargetMonitorX, CurrentX, CurrentY,
SourceMonitorX) {
NewX := TargetMonitorX + (CurrentX - SourceMonitorX)
WinGetPos, OldX, OldY, OldWidth, OldHeight, ahk_id %ActiveWinID%

; Adjust size based on scaling factor
if (TargetMonitorX = 0) {
    ScaleFactor := 1  ; Moving to Monitor 1
} else {
    ScaleFactor := 1 ; Moving to Monitor 2
}

NewWidth := OldWidth * ScaleFactor
NewHeight := OldHeight * ScaleFactor

; Move the window and report success
MsgBox, Moving Window ID: %ActiveWinID% to X: %NewX%, Y: %CurrentY%, Width: %NewWidth%, Height: %NewHeight%

WinMove, ahk_id %ActiveWinID% , %NewX% , %CurrentY% , %NewWidth%, %NewHeight%
MsgBox, Window moved successfully to X: %OldX%,%NewX%, Y: %OldY%,%CurrentY%, Width: %OldWidth%,%NewWidth%, Height: %OldHeight%,%NewHeight%.
}

I also need to resize it because of difference between resolutions.


Solution

  • WinMove, WinTitle, WinText, X, Y [, Width, Height, ExcludeTitle, ExcludeText]
    

    If I don't want to specify WinText it still needs the extra comma.