【问题标题】:How in GUI to show the text from the file which changes?如何在 GUI 中显示更改的文件中的文本?
【发布时间】:2019-01-23 16:06:09
【问题描述】:

在文件1.txt 的第一行有一个铭文,它会随着时间而变化。在 GUI 中,它也应该更改。如何让它不闪烁?

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

【问题讨论】:

    标签: autoit


    【解决方案1】:
    #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,否则隐藏控件。

    此代码更新创建的控件,而不是删除并重新创建它们。

    【讨论】:

      猜你喜欢
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      相关资源
      最近更新 更多