Using a Windows 7 Pro virtual machine on Oracle VirtualBox 5.0.20.
AutoIt 3.3.14.5
Accessing a program minimized to the TaskBar when no program has focus can cause unusual behavior. If the window is in the restored state or maximized state there is no problem.
Example:
$sWindow_Name = "AutoCAD"
WinActivate($sWindow_Name)
WinSetState($sWindow_Name,"", @SW_RESTORE )
Both commands work as expected if the main AutoCAD 2000 window is running and not minimized to the TaskBar.
If the main AutoCAD 2000 window is minimized to the TaskBar, either of the above commands can produce undesired results when used individually or in combination (regardless of order). In this case the result was to open an AutoCAD 2000 Text Window when another window did not have focus, This is basically a WinActivate() / WinSetState() failure.
This problem is repeatable when using AutoIt to close a window, then attempt to access a minimized window (see Solution 2 for an example).
Solution 1:
The easiest solution in this instance was to change the portion of the window title AutoIt looks for:
$sWindow_Name = "AutoCAD 2002"
This solved the problem, much like ebbie's situation, although the script is now less generic (it works for AutoCAD 2002 but not AutoCAD 2010).
Solution 2:
If using AutoIt to close a window (such as AutoCAD 2000 Help) before accessing the AutoCAD 2000 main program window (minimized to the TaskBar), focus is consistently lost. Display a MsgBox before closing the window to give the virtual machine focus:
MsgBox($MB_ICONINFORMATION,@scriptfullpath, 'Closing the help window.')
$iVirtualMachineNeedsFocus = 1
WinClose($sHelp_Window_Name)
Use the mouse to click the OK button (giving VM focus, using the Enter will close the window but not give focus).
Then display a MsgBox before using WinActivate() or WinSetState() to give the virtual machine focus:
; Retrieve the state of focus:
$iWinState = WinGetState($sWindow_Name)
; If the window is not active:
if Not BitAND($iWinState, $WIN_STATE_ACTIVE) Then
; Check if the mouse needs to bring focus to the virtual machine:
If $iVirtualMachineNeedsFocus = 1 Then
; if so, display a message box and reset the flag.
MsgBox($MB_ICONINFORMATION,@scriptfullpath, 'Regaining mouse focus for Virtual Machines.')
$iVirtualMachineNeedsFocus = 0
EndIf
; Activate the window:
WinActivate($sWindow_Name)
EndIf
; If the window is not in a windowed state ("restored") (then it must be either minimized or maximized):
if BitAND($iWinState, $WIN_STATE_MINIMIZED) Or BitAND($iWinState, $WIN_STATE_MAXIMIZED ) Then
; Check if the mouse needs to bring focus to the virtual machine:
If $iVirtualMachineNeedsFocus = 1 Then
; if so, display a message box and reset the flag.
MsgBox($MB_ICONINFORMATION,@scriptfullpath, 'Regaining mouse focus for Virtual Machines.')
$iVirtualMachineNeedsFocus = 0
EndIf
; Retore the window:
WinSetState($sWindow_Name,"", @SW_RESTORE )
EndIf
As a note, using Sleep() in various places did not work as it is not a timing issue.