#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 精确到大约
10 毫秒,更新控件的时间太短了,因此
你会得到闪烁。
更新不会发生在按钮单击等事件上的标签,
可以使用计时器来处理。
如果您使用消息循环 Gui,那么您会使用以下方式获取 Gui 消息
GuiGetMsg。在消息之后,您可以检查时间戳以了解是否
时间差大于当前时间复位值
设置为1000 毫秒。如果更大,定时器将被重置,下面的代码
被执行,否则将从顶部继续循环。
从文本文件中读取的文件名保存到$sSavedFilename。
仅当读取的文件名不同时才更新控件。如果
读取的文件名不存在,然后显示默认(空白?)图像。
我有时会选择默认图像,否则可以选择空文件名
导致下一次图像更改时出现控件大小问题。控制可以
如果没有要显示的图像,则改为隐藏。 $bHideImage电流决定
如果存在则使用文件default.jpg,否则隐藏控件。
此代码更新创建的控件,而不是删除并重新创建它们。