How in GUI to show the text from the file which changes? - autoit

In the file 1.txt in the first line there is an inscription, and it changes over time. In the GUI, it should also be changed. How to make it not flicker?
Local $Form1 = GUICreate('Form1', 261, 200, 192, 124)
$10 = FileReadLine ( "1.txt", 1);
GUISetState()
Local $spic = $10, $Pic1
While 1
$Pic1 = GUICtrlCreateLabel($10, 10, 70, 235, 50)
Switch FileExists($spic)
Case 0
If $Pic1 Then
GUICtrlDelete($Pic1)
$Pic1 = 0
EndIf
Case 1
If Not $Pic1 Then $Pic1 = GUICtrlCreatePic($spic, 16, 24, 212, 124)
EndSwitch
Sleep(1)
WEnd

#include <GUIConstantsEx.au3>
; Create the Gui.
$Form1 = GUICreate('Form1', 261, 200, 192, 124)
$iLabel = GUICtrlCreateLabel('', 10, 10, 235, 50)
$iPic = GUICtrlCreatePic('', 16, 34, 212, 124)
GUISetState()
; Hide picture control if no file [True|False].
$bHideImage = FileExists('default.jpg') ? False : True
; Updates in the loop to recognize change.
$sSavedFilename = ''
; Set time to reset image etc.
$iTimeReset = 1000
$hTimeStamp = TimerInit()
While 1
; Get Gui messages.
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
GUIDelete()
Exit
EndSwitch
; Check if time needs reset.
If TimerDiff($hTimeStamp) > $iTimeReset Then
$hTimeStamp = TimerInit()
Else
ContinueLoop
EndIf
; Read 1st line of a file.
$sReadFilename = FileReadLine ('1.txt', 1)
; If the saved line is different to read line.
If $sSavedFilename <> $sReadFilename Then
GUICtrlSetData($iLabel, $sReadFilename)
Switch FileExists($sReadFilename)
Case 0
If $bHideImage Then
GUICtrlSetState($iPic, $GUI_HIDE)
Else
; Display a default (blank?) image.
GUICtrlSetImage($iPic, 'default.jpg')
EndIf
Case 1
If $bHideImage Then
GUICtrlSetState($iPic, $GUI_SHOW)
EndIf
; Display the new image.
GUICtrlSetImage($iPic, $sReadFilename)
EndSwitch
; Save the current filename.
$sSavedFilename = $sReadFilename
EndIf
WEnd
Sleep is accurate to approximately
10 milliseconds, which is of so little time to update a control, thus
you get the flicker.
Update of labels which do not occur on an event like a button click,
can be handled using a timer.
If you use a message loop Gui, then you get the Gui messages with using
GuiGetMsg. After the messages, you can check a time stamp to know if
the time difference is larger than the time reset value which is currently
set as 1000 milliseconds. If larger, timer is reset and the code below
is executed, else will continue the loop from the top.
The filename read from the text file is saved to $sSavedFilename.
Updating of controls is only done when the read filename is different. If
the filename read does not exist, then display a default (blank?) image.
I sometimes choose a default image else an empty filename instead can
cause control sizing issues with the next image change. The control could
be instead be hidden, if no image to show. $bHideImage current decides
to use the file default.jpg if exist, else to hides the control.
This code updates the created controls instead of deleting and recreating them.
Update GUICtrlCreateLabel text with GUICtrlSetData.
Update GUICtrlCreatePic image with GUICtrlSetImage.

Related

If statement not working AutoIT

I am having trouble with the code I am writing. I have a table which lists staff member details. I am trying to have an email address copy to the clipboard when you select a record and right click. I have the following code:
#include <GUIConstantsEx.au3>
#include <mssql.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
global $title = "E-Mail address lookup"
global $name = InputBox($title,"Please type the name of the person you wish to find")
global $sqlCon = _MSSQL_Con("server", "username", "password", "directory-plus")
global $result = _MSSQL_GetRecord($sqlCon, "autoit_view","*", "WHERE cn LIKE '%" & StringStripWS($name,3) & "%'")
if StringLen(StringStripWS($name,3)) < 1 then
MsgBox(0, $title, "Name cannot be empty")
Else
Global $rset = UBound($result) - 1
Global $ControlID = GUICreate($title, 500, 150)
Global $idListview = GUICtrlCreateListView("Deparment|E-Mail Address|Name|Telephone Number", 10, 10, 480, 150)
for $count = 1 to $rset step 1
GUICtrlCreateListViewItem($result[$count][0] & "|" & $result[$count][2] & "|" & $result[$count][1] & "|" & $result[$count][2], $idListview)
if MouseClick($MOUSE_CLICK_RIGHT)==1 Then
ClipPut($result[$count][2])
EndIf
Next
GUISetState(#SW_SHOW)
GUISetState()
While 1
Global $Msg = GUIGetMsg()
Switch $Msg
Case -3, $ControlID
Exit
EndSwitch
WEnd
EndIf
I would have thought my IF statement within the for loop would do the trick but, when I run my code, it just copies whatever is in email address column in the last row when in fact I want it to copy the email address when you right click on a particular row but I'm not sure how to do it.
Your code has two major problems. The first is that MouseClick() doesn't check for the mouse button being pressed, but instead sends a mouse button click. Since sending a mouse button click is going to succeed, MouseClick($MOUSE_CLICK_RIGHT)==1 will evaluate to true. For each list view item you create, you are putting the email address on the clipboard.
The second problem is that the if statement is in the wrong place. It's executed just after you create each list view item.
Instead, change your While statement as follows
While 1
Global $Msg = GUIGetMsg()
Switch $Msg
Case -3, $ControlID
Exit
case $GUI_EVENT_SECONDARYDOWN
$selecteditem=StringSplit(GUICtrlRead(GUICtrlRead($idListview)),"|")
if #error=0 and $selecteditem[0]>1 Then
ClipPut($selecteditem[2])
EndIf
EndSwitch
WEnd

Close windows in reverse order of creation

I have a program with a main window and multiple other windows and want to use autoit to close these windows. The main window however refuses to close if other windows of the program are open and creates a warning message.
To avoid this, I want to close the other windows first.
Because of the way the program is written, the windows are NOT in a parent-child relationship, so I cannot use "EnumChildWindows" in "user32.dll".
So my other option would be to obtain the order or time the windows were created at, and close them in reversed order. Is there any way to do this?
You can try to get it by the process. The function below will return the z-order of the windows, as far as I know, without you tracking the "creation time" of the windows themselves from within your own code, you won't get a solution that can do that.
#include <WinAPIProc.au3>
#include <WinAPISys.au3>
#region - Example
Global $gahWnd[3]
For $i = 0 To 2
$gahWnd[$i] = GUICreate("Example " & $i)
GUISetState(#SW_SHOW, $gahWnd[$i])
Next
Global $gaExample = _WinGetByProc(#AutoItPID)
If #error Then Exit 2
Sleep(3000) ; so you can at least see the windows were created
; windows are returned in the "last z-order"
; so they'll be returned "Example 2, Example 1, Example 0"
; This makes it easy to close all but the first one
; use -1 to keep the first window created
For $i = 1 To $gaExample[0] - 1
ConsoleWrite("Closing: " & $gaExample[$i] & ":" & WinGetTitle($gaExample[$i]) & #CRLF)
WinClose($gaExample[$i])
Next
Global $gaMsg
While 1
$gaMsg = GUIGetMsg(1)
Switch $gaMsg[1]
Case $gahWnd[0]
Switch $gaMsg[0]
Case -3
Exit
EndSwitch
Case $gahWnd[1]
Switch $gaMsg[0]
Case -3
GUIDelete($gahWnd[1])
EndSwitch
Case $gahWnd[2]
Switch $gaMsg[0]
Case -3
GUIDelete($gahWnd[2])
EndSwitch
EndSwitch
WEnd
#EndRegion - Example
; pass the exe or the process id
Func _WinGetByProc($vExe)
; will return pid if exe name sent or validate if pid is sent
Local $iPID = ProcessExists($vExe)
If Not ProcessExists($iPID) Then
Return SetError(1, 0, 0)
EndIf
; enum desktop window only (top-level-windows)
Local $ahWnds = _WinAPI_EnumDesktopWindows(_WinAPI_GetThreadDesktop( _
_WinAPI_GetCurrentThreadId()))
If #error Or Not IsArray($ahWnds) Then
Return SetError(2, 0, 0)
EndIf
Local $aExeContainer[11], $iDim
For $iwnd = 1 To $ahWnds[0][0]
; compare windows pid with our pid
If (WinGetProcess(HWnd($ahWnds[$iwnd][0])) = $iPID) Then
$iDim += 1
; sanity check, be sure the array has the right number of indexes
If (Mod($iDim, 10) = 0) Then
ReDim $aExeContainer[$iDim + 10]
EndIf
$aExeContainer[$iDim] = HWnd($ahWnds[$iwnd][0])
EndIf
Next
; if there were no matches return
If Not $iDim Then
Return SetError(3, 0, 0)
EndIf
; trim array and set number found in the zero index
ReDim $aExeContainer[$iDim + 1]
$aExeContainer[0] = $iDim
Return $aExeContainer
EndFunc

Autoit make combo show when another combo has a certain value

In my script: http://pastebin.com/Az6vabnK I am trying to make a combo show up when another combo has a certain value as seen here:
Case $Combo2
If Read() = "Vanilla" Then
GUISwitch($GUI, $TabSheet2)
$Combo3 = GUICtrlCreateCombo("Choose Version", 10, 85, 161, 25, $CBS_DROPDOWNLIST)
GUICtrlSetData($Combo3, "Latest|1.6.4|1.6.2|1.5.2|1.4.7|1.4.5")
Else
GUISwitch($GUI, $TabSheet2)
GUICtrlDelete($Combo3)
EndIf
Func Read();The function for reading the state of the server type combo.
Return GUICtrlRead($Combo2)
EndFunc ;==>Read
In the while it's a:
AdlibRegister("Read")
To read the value. For some reason, nothing happens when the combo changes value. Any ideas?
Delete that AdlibRegister("Read") because it does nothing.
You are already getting value of $combo2 here
If Read() = "Vanilla" Then
$Combo2 should be a global variable or you can remove that Read() function and use GuiCtrlRead directly
If GUICtrlRead($Combo2) = "Vanilla" Then
Try this
If GUICtrlRead($Combo2) = "Vanilla" Then
GUISwitch($GUI, $TabSheet2)
$Combo3 = GUICtrlCreateCombo("Choose Version", 10, 85, 161, 25, $CBS_DROPDOWNLIST)
GUICtrlSetData($Combo3, "Latest|1.6.4|1.6.2|1.5.2|1.4.7|1.4.5")
GUICtrlCreateTabItem("")
Else
GUISwitch($GUI, $TabSheet2)
GUICtrlDelete($Combo3)
EndIf

Autoit: Creating a Function?

I have a GUI that records the list of all the chm in a folder. When 'RUN' button is clicked, it will open the first chm in the list until so on. Then I have created a function that should expand the tree.
My problem is my function, it works until MsgBox() and stops there. When I ran my program there is no error indicated.
#include <GuiConstantsEx.au3>
#include <GuiListBox.au3>
#include <GuiTreeView.au3>
#include <File.au3>
#include <Array.au3>
;GUI
$guiTitle = "Automation"
GUICreate($guiTitle, 250, 430)
Global $hWnd = ControlGetHandle("[CLASS:HH Parent;TITLE:AutoIt Help]", "", "[CLASS:SysTreeView32; INSTANCE:1]")
Global $hChild = _GUICtrlTreeView_GetFirstChild($hWnd, 0)
Local $source = InputBox("Source Folder","Please enter the source folder","")
;InputBox
If #error = 1 Then
Exit
EndIf
If #error = 4 Then
Exit
;GUI_List
Else
$add = GUICtrlCreateButton("Show", 10, 53, 230, 20)
$picList = GUICtrlCreateList("", 10, 78, 230, 300)
$run = GUICtrlCreateButton("Run", 170, 385, 70, 30)
GUISetState(#SW_SHOW)
While 1
$msg = GUIGetMsg()
Switch $msg
;add
Case $add
Global $FileList = _FileListToArray($source, "*.chm")
If #error = 1 Then
MsgBox(0, "", "No Files Found.")
Exit
EndIf
If #error = 4 Then
MsgBox(0, "", "No Files Found.")
Exit
EndIf
For $i = 1 To $FileList[0] ;List_IFIX Pictures
GUICtrlSetData($picList, $FileList[$i])
Next
;run
Case $run
If _GUICtrlListBox_GetCount($picList) = 0 Then
MsgBox(0, "", "No Files Found.")
Else
For $i = 1 To $FileList[0]
If Not WinExists("AutoIT Help]") Then
ShellExecute($source & "\" & $FileList[1])
_Expand($hWnd, $hChild)
EndIf
Next
EndIf
;exit
Case $GUI_EVENT_CLOSE
ExitLoop
EndSwitch
WEnd
EndIf
And here is my function:
Func _Expand($hWnd, $hChild)
WinWaitActive("AutoIT Help")
MsgBox(0,"","Expand")
While 1
$hChild = _GUICtrlTreeView_GetNextChild($hWnd, $hChild)
If _GUICtrlTreeView_GetText($hWnd, $hChild) = "Tutorials" Then ExitLoop
WEnd
_GUICtrlTreeView_Expand(ControlGetHandle("[CLASS:HH Parent;TITLE:AutoIt Help]","", "[CLASS:SysTreeView32; INSTANCE:1]"),$hchild, True)
EndFunc
Lots of problems with the code.
Check your titles! In two cases you have got the window title spelt incorrectly in your example. AutoIt is spelt with a lowercase t, and window title matching is case sensitive unless you set an option otherwise.
If "Tutorials" is not found then you will loop forever. You should add a check after _GUICtrlTreeView_GetNextChild to see if you've reached the end of the treeview.
But the real problem with your code is that you are setting $hWnd and $hChild at the beginning of the code, before you run the process that creates the window. As a result, the window is not found, and so $hWnd will always be NULL when you call _Expand.
This sort of question is not encouraged on stackoverflow. We like questions which will be useful to other people in the future rather than help with specific code. Before asking questions like this in the future, please try and debug the problem yourself. You could add ConsoleWrite statements through the code showing variable values, which would have shown you that $hWnd didn't have a handle value when you enter expand, from there it is obvious.

Can't figure out why this AutoIt code isn't working

the problem is that after you hit ok on this GUI, it doesn't start the function I have.
Func BeginningGUI()
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Stronghold Kingdoms", 248, 95, -1, -1)
$Password = GUICtrlCreateInput("Password", 8, 32, 233, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
$ButtonOk = GUICtrlCreateButton("OK", 86, 64, 75, 25, $BS_NOTIFY)
$ButtonCancel = GUICtrlCreateButton("Cancel", 167, 64, 75, 25, $BS_NOTIFY)
$EnterPassLabel = GUICtrlCreateLabel("Please Enter Your Stronghold Kingdoms Password", 0, 12, 241, 17, 0)
GUISetState(#SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $ButtonCancel
Exit
Case $ButtonOk
Exit
OpenSHK()
EndSwitch
WEnd
EndFunc
I want it to obtain the input word, then save that as a variable and enter it into the later function OpenSHK()
Also when I run OpenSHK() Alone it works fine so it's not that.
You're exiting before the call to OpenSHK() is made, causing it to never be called.
Case $ButtonOk
Exit
OpenSHK()
EndSwitch
Exit after that call is made instead:
Case $ButtonOk
OpenSHK()
Exit
EndSwitch
The second problem is that $Password is not the password the user entered; it's a control ID. You need to use GuiCtrlRead to retrieve the value the user entered after the Ok button has been clicked.

Resources