Jump to content

Windows 10 - WinMove only fails at the StartUp


Yodavish
 Share

Recommended Posts

I have this AutoIt script executable in the Windows 10 startup folder, along with 3 other things in the startup folder: Internet Explorer (IE ) to a specific webpage, 3rd party application for the client to use, and another AutoIt script. 

Once a user logs into Windows, one of the many tasks the AutoIt script will do is a loop to check if the specific IE title exists and move it to the secondary monitor, however, it always fails. Either, it doesn't move it at all or I see a gray blank IE page on the secondary monitor. This only occurs on the first login, if I close the AutoIt.exe down and re-run the AutoIt.exe again it will then move the IE over correctly.

Other things I've tried so far but have failed:

  • Tested different times to delay, sleep(10000), before the script goes into the while
  • Adding a RunWait() to open up the specific page in question first before the while

Here is specific code in question, I can add the whole thing if needed:

Global Const $proTrackerWindowTitle = IniRead("gross.ini", "default", "proTrackerWindowTitle", "NONE")              ; The title of ProTracker window
Global $proTrackerMovedFlag = ''

While 1
    ;----------------------------------------------------------
    ; Moves IE Window to secondary monitor only once
    ; $proTrackerMovedFlag = '' is the one time trigger
    ;----------------------------------------------------------
    If WinExists($proTrackerWindowTitle) and $proTrackerMovedFlag = '' Then
        moveProTrackerWindow()
    EndIF
    Sleep(100) ; Sleep to reduce CPU usage
WEnd

;---------------------------------------------------------------
; Functions
;---------------------------------------------------------------
Func moveProTrackerWindow()
    Local $aPos, $aData = _WinAPI_EnumDisplayMonitors()
    If IsArray($aData) Then
        ReDim $aData[$aData[0][0] + 1][5]
        For $i = 1 To $aData[0][0]
            $aPos = _WinAPI_GetPosFromRect($aData[$i][1])
            If($aPos[0] <> 0) Then
                For $j = 0 To 3
                    Switch $j
                        Case 0 ; index for X-position of secondary monitor
                            $secondMonitorPosition = $aPos[$j]
                        Case 2 ; index for width of secondary monitor
                            $secondMonitorWidth = $aPos[$j]
                        Case 3 ; index for height of secondary monitor
                            $secondMonitorHeight = $aPos[$j]
                    EndSwitch
                Next
            EndIf
        Next
    EndIf
    $hWnd=WinActivate($proTrackerWindowTitle)
    ; Checks to see if proTracker is maximized
    If BitAND(WinGetState($hWnd), 32) Then
        ;consoleWrite ("Maximized" & @CRLF)
        ; Gets proTracker position
        Local $aPos = WinGetPos ($proTrackerWindowTitle)
        ;consoleWrite ("Pos: " & $aPos[0] & @CRLF)
        ; Checks if the position is at the second monitor
        If $aPos[0] <> $secondMonitorPosition Then
            ;consoleWrite ("Maximized but position is wrong" & @CRLF)
            WinMove($hWnd, "", $secondMonitorPosition, 0,$secondMonitorWidth, $secondMonitorHeight)
            WinWait($hWnd)
            WinSetState($hWnd, "", @SW_MAXIMIZE)
        EndIf
    Else
        ;consoleWrite ("Not Maximized" & @CRLF)
        WinMove($hWnd, "", $secondMonitorPosition, 0, $secondMonitorWidth, $secondMonitorHeight)
        WinWait($hWnd)
        WinSetState($hWnd, "", @SW_MAXIMIZE)
    EndIf
    $proTrackerMovedFlag = True
    ;_ArrayDisplay($aData, '_WinAPI_EnumDisplayMonitors') ; displays monitor numbers
    consoleWrite("Width: " & $secondMonitorWidth & " Height: " & $secondMonitorHeight & " Second Monitor Position: " & $secondMonitorPosition & @CRLF)
EndFunc

 

Edited by Yodavish
wording
Link to comment
Share on other sites

Tested on single monitor (win 10), compiled x86, made a shortcut in startup folder.  Working :

#include <Constants.au3>
#include <IE.au3>

Local $oIE = _IECreate ("google.ca")
Local $hWnd = _IEPropertyGet ($oIE, "hwnd")
WinMove ($hWnd, "", 100, 100, 800, 600)
Sleep (3000)
WinSetState ($hWnd, "", @SW_MAXIMIZE)
Sleep (1000)

 

Edited by Nine
not menu but folder, I meant
Link to comment
Share on other sites

Thanks @Nine

I tried your suggestion and that lead me to see that the issue was due to the fact that _IECreate has issues with creating intranet addresses. So I modified the code and I think it works now, but I'll continue to test it.

Adjust code below:

Func moveProTrackerWindow()
    ; Code below was copied from AutoIt Help by searching "_WinAPI_EnumDisplayMonitors" and altered slightly
    Local $aPos, $aData = _WinAPI_EnumDisplayMonitors()
    If IsArray($aData) Then
        ReDim $aData[$aData[0][0] + 1][5]
        For $i = 1 To $aData[0][0]
            $aPos = _WinAPI_GetPosFromRect($aData[$i][1])
            If($aPos[0] <> 0) Then
                For $j = 0 To 3
                    Switch $j
                        Case 0 ; index for X-position of secondary monitor
                            $secondMonitorPosition = $aPos[$j]
                        Case 2 ; index for width of secondary monitor
                            $secondMonitorWidth = $aPos[$j]
                        Case 3 ; index for height of secondary monitor
                            $secondMonitorHeight = $aPos[$j]
                    EndSwitch
                Next
            EndIf
        Next
    EndIf
    ;WinActivate($proTrackerWindowTitle)
    ShellExecute("iexplore.exe", "about:blank")
    sleep(3000)
    $oIE = _IEAttach("about:blank", "url")
    _IENavigate($oIE, "intranet URL address")
    Local $hWnd = _IEPropertyGet($oIE, "hwnd")
    ; Checks to see if proTracker is maximized
    If BitAND(WinGetState($hWnd), 32) Then
        ;consoleWrite ("Maximized" & @CRLF)
        ; Gets proTracker position
        Local $aPos = WinGetPos($hWnd)
        ;consoleWrite ("Pos: " & $aPos[0] & @CRLF)
        ; Checks if the position is at the second monitor
        If $aPos[0] <> $secondMonitorPosition Then
            ;consoleWrite ("Maximized but position is wrong" & @CRLF)
            WinMove($hWnd, "", $secondMonitorPosition, 0,$secondMonitorWidth, $secondMonitorHeight)
            WinWait($hWnd)
            WinSetState($hWnd, "", @SW_MAXIMIZE)
        EndIf
    Else
        ;consoleWrite ("Not Maximized" & @CRLF)
        WinMove($proTrackerWindowTitle, "", $secondMonitorPosition, 0, $secondMonitorWidth, $secondMonitorHeight)
        WinWait($hWnd)
        WinSetState($hWnd, "", @SW_MAXIMIZE)
    EndIf
    $proTrackerMovedFlag = True
    ;_ArrayDisplay($aData, '_WinAPI_EnumDisplayMonitors') ; displays monitor numbers
    consoleWrite("Width: " & $secondMonitorWidth & " Height: " & $secondMonitorHeight & " Second Monitor Position: " & $secondMonitorPosition & @CRLF)
EndFunc

 

Link to comment
Share on other sites

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...