Jump to content

Unable to find window


Recommended Posts

using this code.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=shutdown.ico
#AutoIt3Wrapper_outfile=..\ShutDowner.exe
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <Date.au3>
#Include <ScreenCapture.au3>
#include <ListviewConstants.au3>
#Include <Array.au3>
#include <Process.au3>

Opt ("Wintitlematchmode",2)
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1+2)
TraySetToolTip ("ShutDowner")

Global $option

$ReadLastDay = IniRead("ShutDowner.ini", "Time", "LastDays", "")
$ReadLastHour = IniRead("ShutDowner.ini", "Time", "LastHours", "")
$ReadLastMinute = IniRead("ShutDowner.ini", "Time", "LastMinute", "")

TrayCreateItem("Terminate and EXIT")
TrayItemSetOnEvent(-1,"ExitFunction") ;function name

$Form1 = GUICreate("ShutDowner", 460, 400)
GUICtrlCreateGroup("How long to wait ?", 5, 3, 150, 180)

GUICtrlCreateLabel("Day's", 30, 23)
$Days = GUICtrlCreateInput($ReadLastDay, 10, 20, 20, 20)
If GUICtrlRead ($Days) = "" Then GUICtrlSetData ($Days ,"0")

GUICtrlCreateLabel("Hr's", 80, 23)
$Hours = GUICtrlCreateInput($ReadLastHour, 60, 20, 20, 20)
If GUICtrlRead ($Hours) = "" Then GUICtrlSetData ($Hours ,"0")
GUICtrlSetLimit($Hours, 2)

GUICtrlCreateLabel("Min's", 125, 23, 25)
$Minutes = GUICtrlCreateInput($ReadLastMinute, 105, 20, 20, 20)
If GUICtrlRead ($Minutes) = "" Then GUICtrlSetData ($Minutes ,"10")
GUICtrlSetLimit($Minutes, 2)

$optionInput = GUICtrlCreateInput("Please select desired action", 10, 40, 140)
GUICtrlSetState (-1,$GUI_DISABLE)
$reasonInput = GUICtrlCreateEdit("No reason specified", 10, 60, 140, 115)

GUICtrlCreateGroup("Action's:", 160, 3, 90, 120)
$optionLogoff = GUICtrlCreateRadio("LogOff", 170, 20)
$optionShutDown = GUICtrlCreateRadio("ShutDown", 170, 40)
$optionReboot = GUICtrlCreateRadio("Reboot", 170, 60)
$optionStandby = GUICtrlCreateRadio("Standby", 170, 80)
$optionHibernate = GUICtrlCreateRadio("Hibernate", 170, 100)

GUICtrlCreateGroup("Optional:", 160, 123, 90, 60)
$optionForceCloseApps = GUICtrlCreateCheckbox("Force", 165, 140)
GUICtrlSetState (-1,$GUI_CHECKED)
$optionForceAll = GUICtrlCreateCheckbox("Force if hung", 165, 160)
GUICtrlSetState (-1,$GUI_CHECKED)
$screenshot = GUICtrlCreateCheckbox ("Take Screenshot's before selected action.",10,190,220,15)
GUICtrlSetTip (-1,"This option will save screenshots to:" & @CRLF & @DesktopDir & "\ShutDowner Date and Time.jpg")
GUICtrlSetState (-1,$GUI_CHECKED)

GUICtrlCreateGroup ("Process",5,210,245,40)
$ProcessToClose = GUICtrlCreateInput ("",10,225,100,20)
GUICtrlSetTip (-1,"Process name")
$CloseProcess = GUICtrlCreateRadio ("Close",115,225)
$WaitProcess = GUICtrlCreateRadio ("Wait till closed",160,225)

$RefreshProcess = GUICtrlCreateButton ("Refresh list",255,335,100,15)
$SelectProcess = GUICtrlCreateButton ("Select",255,350,100,15)
$ListView = GUICtrlCreateListView ("Process                                ",255,10,100,320,$LVS_SORTASCENDING)
$list1st = ProcessList()
    for $i = 1 to $list1st[0][0]
        If ($list1st[$i][1]) = "" Then
            ;empty
        Else
            GUICtrlCreateListViewItem($list1st[$i][0],$ListView)
        EndIf
    next
GUICtrlCreateGroup ("Windows",5,250,245,40)
Global $WindowName = GUICtrlCreateInput ("",10,265,100,20)
GUICtrlSetTip (-1,"Window name")
$CloseWindow = GUICtrlCreateRadio ("Close",115,265)
$WaitWindow = GUICtrlCreateRadio ("Wait till closed",160,265)
$WinList = GUICtrlCreateListView ("Window                                                       ",355,10,100,320,$LVS_SORTASCENDING)
$Win1st = WinList()
    For $W1st = 1 to $Win1st[0][0]
        If $Win1st[$W1st][0] <> "" AND IsVisible($Win1st[$W1st][1]) Then
            GUICtrlCreateListViewItem ($Win1st[$W1st][0] & @LF,$WinList)
        EndIf
    Next
$RefreshWindows = GUICtrlCreateButton ("Refresh Windows",355,335,100,15)
$SelectWindow = GUICtrlCreateButton ("Select",355,350,100,15)

GUICtrlCreateGroup ("Commands",5,290,245,105)
$CommandLine1 = GUICtrlCreateEdit ("",10,305,190,80)
GUICtrlSetTip (-1,"This can be one line command or a whole script." & @CRLF & "Generated Command.bat will be executed" & @CRLF & "Dont forget to add EXIT at the end of your script")
$CommandTest = GUICtrlCreateButton ("Test it" ,205,305,40,80)
GUICtrlSetTip (-1,"Test this command to see if it will work.")

$button = GUICtrlCreateButton("Start Count Down", 255, 365, 200, 30)
GUISetState(@SW_SHOW)

GuiFunc()
Func GuiFunc()
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $button
                If GUICtrlRead ($optionInput) = "Please select desired action" Then
                    MsgBox(64,"Information","Please select desired action frst.")
                Else
                    GUISetState(@SW_HIDE)
                    _StartCountDown()
                EndIf
            Case $optionLogoff
                GUICtrlSetData($optionInput, "Logoff")
                $option = 0
            Case $optionShutDown
                GUICtrlSetData($optionInput, "ShutDown")
                $option = 1
            Case $optionReboot
                GUICtrlSetData($optionInput, "Reboot")
                $option = 2
            Case $optionStandby
                GUICtrlSetData($optionInput, "Standby")
                $option = 32
            Case $optionHibernate
                GUICtrlSetData($optionInput, "Hibernate")
                $option = 64
            Case $optionForceCloseApps
                If GUICtrlRead($optionForceCloseApps) = $GUI_CHECKED Then
                ElseIf GUICtrlRead($optionForceCloseApps) = $GUI_UNCHECKED Then
                EndIf
            Case $optionForceAll
                If GUICtrlRead($optionForceAll) = $GUI_CHECKED Then
                ElseIf GUICtrlRead($optionForceAll) = $GUI_UNCHECKED Then
                EndIf
            Case $RefreshProcess
                GUICtrlDelete ($ListView)
                $ListView = GUICtrlCreateListView ("Process                                ",255,10,100,320,$LVS_SORTASCENDING)
                $list = ProcessList()
                for $i = 1 to $list[0][0]
                    If ($list[$i][1]) = "" Then
                        ;empty
                    Else
                        GUICtrlCreateListViewItem($list[$i][0],$ListView)
                    EndIf
                next
            Case $SelectProcess
                $ReadSelected = GUICtrlRead ($ListView,"ListView")
                $FixedName = StringTrimRight(GUICtrlRead ($ReadSelected),1)
                GUICtrlSetData ($ProcessToClose,$FixedName)
            Case $RefreshWindows
                GUICtrlDelete ($WinList)
                $WinList = GUICtrlCreateListView ("Window                                                       ",355,10,100,320,$LVS_SORTASCENDING)
                $Win = WinList()
                For $W = 1 to $Win[0][0]
                    ; Only display visble windows that have a title
                    If $Win[$W][0] <> "" AND IsVisible($Win[$W][1]) Then
                        GUICtrlCreateListViewItem ($Win[$W][0] & @LF,$WinList)
                    EndIf
                Next
            Case $SelectWindow
                $ReadSelectedWindow = GUICtrlRead ($WinList,"ListView")
                $FixedName = StringTrimRight(GUICtrlRead ($ReadSelectedWindow),1)
                GUICtrlSetData ($WindowName,$FixedName)
            Case $CommandTest
                $ReadCommand = GUICtrlRead ($CommandLine1)
                FileDelete (@ScriptDir & "\Command.bat")
                FileWrite (@ScriptDir & "\Command.bat",$ReadCommand)
                RunWait (@ScriptDir & "\Command.bat",@ScriptDir)
                FileDelete (@ScriptDir & "\Command.bat")
        EndSwitch
    WEnd
EndFunc ;==>GuiFunc

Func _StartCountDown()
    Local $ActionRead = GUICtrlRead($optionInput)
    Local $ReadDays = GUICtrlRead($Days)
    Local $ReadHours = GUICtrlRead($Hours)
    Local $ReadMinutes = GUICtrlRead($Minutes)
    Local $ForceCloseRead = GUICtrlRead($optionForceCloseApps)
    Local $ForceFrozeRead = GUICtrlRead($optionForceAll)
    Local $reason = GUICtrlRead($reasonInput)

    If $ReadDays >= 365 Then
        MsgBox(64, "Days Error", "Please set from 0-364 days only")
        GUISetState(@SW_SHOW)
        Return
    EndIf
    If $ReadHours >= 24 Then
        MsgBox(64, "Hours Error", "Please set from 0-23 hours only")
        GUISetState(@SW_SHOW)
        Return
    EndIf
    If $ReadMinutes >= 60 Then
        MsgBox(64, "Minutes Error", "Please set from 0-59 minutes only")
        GUISetState(@SW_SHOW)
        Return
    EndIf
    If $ActionRead = "What to do" Then
        MsgBox(16, 'Error', 'Please select action first')
        GUISetState(@SW_SHOW)
        Return
    EndIf
    IniWrite("ShutDowner.ini", "Time", "LastDays", $ReadDays)
    IniWrite("ShutDowner.ini", "Time", "LastHours", $ReadHours)
    IniWrite("ShutDowner.ini", "Time", "LastMinute", $ReadMinutes)
    If $ForceCloseRead = 1 Then $option += 4
    If $ForceFrozeRead = 1 Then $option += 16

    $timer = TimerInit()
    $ShutDowntime = (((($ReadDays*24)+$ReadHours)*60)+$ReadMinutes)*60*1000
    While 1
        Local $ms = $ShutDowntime - TimerDiff($timer)
        If $ms < 0 Then
            If GUICtrlRead ($screenshot) = $GUI_CHECKED Then _ScreenCapture_Capture (@DesktopDir & "\ShutDowner D" & @MDAY & "M" & @MON & "Y"& @YEAR & " " & @HOUR & @MIN & @SEC & ".jpg" )
            If ProcessExists (GUICtrlRead ($ProcessToClose)) = 1 Then
                If GUICtrlRead ($CloseProcess) = $GUI_CHECKED Then ProcessClose (GUICtrlRead ($ProcessToClose))
                If GUICtrlRead ($WaitProcess) = $GUI_CHECKED Then ProcessWaitClose (GUICtrlRead ($ProcessToClose))
                If GUICtrlRead ($screenshot) = $GUI_CHECKED Then _ScreenCapture_Capture (@DesktopDir & "\ShutDowner D" & @MDAY & "M" & @MON & "Y"& @YEAR & " " & @HOUR & @MIN & @SEC & ".jpg" )
            EndIf
            If WinExists (GUICtrlRead ($WindowName))= 1 Then
                MsgBox(0,'',GUICtrlRead ($WindowName))
                If GUICtrlRead ($CloseWindow) = $GUI_CHECKED Then WinClose (GUICtrlRead ($WindowName))
                If GUICtrlRead ($WaitWindow) = $GUI_CHECKED Then WinWaitClose (GUICtrlRead ($WindowName))
                If GUICtrlRead ($screenshot) = $GUI_CHECKED Then _ScreenCapture_Capture (@DesktopDir & "\ShutDowner D" & @MDAY & "M" & @MON & "Y"& @YEAR & " " & @HOUR & @MIN & @SEC & ".jpg" )
            EndIf
            If GUICtrlRead ($screenshot) = $GUI_CHECKED Then _ScreenCapture_Capture (@DesktopDir & "\ShutDowner D" & @MDAY & "M" & @MON & "Y"& @YEAR & " " & @HOUR & @MIN & @SEC & ".jpg" )
            $ReadCommandWait = GUICtrlRead ($CommandLine1) ;read whole command script
            FileDelete (@ScriptDir & "\Command.bat") ;delete command file
            FileWrite (@ScriptDir & "\Command.bat",$ReadCommandWait) ;write read command to file
            RunWait (@ScriptDir & "\Command.bat",@ScriptDir) ;execute command file
            FileDelete (@ScriptDir & "\Command.bat") ;delete command file
            If GUICtrlRead ($screenshot) = $GUI_CHECKED Then _ScreenCapture_Capture (@DesktopDir & "\ShutDowner D" & @MDAY & "M" & @MON & "Y"& @YEAR & " " & @HOUR & @MIN & @SEC & ".jpg" )
            ;Shutdown($option,$reason)
            Sleep (1000)
            Exit
        Else

            ToolTip(GUICtrlRead ($optionInput) & " in " & StringFormat("%d days and \n%02d:%02d:%02d", Int($ms / 86400000), Mod(Int($ms / 3600000),24), Mod(Int($ms / 60000),60), Mod(Int($ms / 1000),60)),@DesktopWidth/2-70,@DesktopHeight/2-60,"ShutDowner CountDown")
        EndIf
        Sleep(100)
    WEnd
EndFunc

Func ExitFunction()
    Exit
EndFunc

Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 2 ) Then
    Return 1
  Else
    Return 0
  EndIf
EndFunc

I would select any visible window from the list, click Select button to assign window name to windows name edit control.

Then i would ask

If WinExists (GUICtrlRead ($WindowName))= 1 Then
    MsgBox(0,'',GUICtrlRead ($WindowName))
    If GUICtrlRead ($CloseWindow) = $GUI_CHECKED Then WinClose (GUICtrlRead ($WindowName))
    If GUICtrlRead ($WaitWindow) = $GUI_CHECKED Then WinWaitClose (GUICtrlRead ($WindowName))
    If GUICtrlRead ($screenshot) = $GUI_CHECKED Then _ScreenCapture_Capture (@DesktopDir & "\ShutDowner D" & @MDAY & "M" & @MON & "Y"& @YEAR & " " & @HOUR & @MIN & @SEC & ".jpg" )
EndIf

but its not finding any window unless i say If WinExists ("Start")= 1 Then where Start is the name of the windows from the list.

Yet the MsgBox(0,'',GUICtrlRead ($WindowName)) returns the name correct.

Any ideas why its not working ? It seems like its not reading the window name correctly.

Please help. i am lost :)

Edited by madasraka
Link to comment
Share on other sites

i still have no clue for why its not finding the window when name is a variable. Only when i use Static name instead of it being a $windowname = guictrlread ($var)

Can anyone look at my code and see what might be the problem ? Because i dont see anything wrong with it (in that case)

Link to comment
Share on other sites

When I have problems finding a window I always use Opt("WinTitleMatchMode", 3) for exact match. This always works for me. Of course you must define the hole title.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

whole title is defined.

Window list lists all windows (visible) and SELECT will assign window name to guictrlcreateedit

Then its read by guictrlread

MSGBOX says windows name is exactly as its displayed, yet WinExist (guictrlread ($windowname)) = 0 somehow.

Can you try this code and see for your self ? Maybe i have a virus ? But even if i do, i know its not in the system but stored else where (and its only reported as a virus but what is a "virus" really ? ""illegal functions application"" or those normal users should not have access to but lets not talk about that please.)

Link to comment
Share on other sites

Turns out

guictrlread returns not only the correct window name, but 1 space after it so its "WINDOWNAME " instead of "WINDOWNAME"

THats why winexist could not find it no matter what option i chose.

I ended up trimming the result with

Case $SelectWindow
                $ReadSelectedWindow = GUICtrlRead ($WinList,"ListView")
                $FixedName = StringTrimRight(GUICtrlRead ($ReadSelectedWindow),2)
                GUICtrlSetData ($WindowName,$FixedName)
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...