#include <Misc.au3>
#include <File.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
If _Singleton(@ScriptName, 1) = 0 Then Exit (1) ;互斥运行
Dim $rnd[3],$ver, $aRecords, $LabSentence
$hGUI = GUICreate("关于文字滚动的问题", 400, 60, -1, -1)
$LabSentence = GUICtrlCreateLabel("", 5, 10, 380, 20, $SS_CENTER, $GUI_WS_EX_PARENTDRAG)
$BtnOpenFile = GUICtrlCreateButton("打开文件", 5, 35, 60, 25)
OriFile() ;初始化程序
AdlibRegister("ChangeSentence", 10000) ;10秒后自动切换句子
GUISetState()
Do
$msg = GUIGetMsg()
Switch ($msg)
Case $BtnOpenFile
OpenFile()
EndSwitch
Until $msg = $GUI_EVENT_CLOSE
Func OriFile() ;程序运行后的初始化函数
Dim $szDrive, $szDir, $szFName, $szExt
$txtFile = IniRead(".\Setting.ini", "Setting", "File", "NotFound")
If _FileReadToArray($txtFile, $aRecords) Then ;把文件读入数组
If $txtFile <> "" Then
For $x = 1 To $aRecords[0]
$ver = $ver & $aRecords[$x] & @CRLF
Next
ChangeSentence()
Else
MsgBox(0, "Error", "读取文件失败!")
EndIf
Else
_PathSplit($txtFile, $szDrive, $szDir, $szFName, $szExt) ;拆分文件路径
MsgBox(0, "Error", "文件 “" & $szFName & $szExt & "” 不存在,请重新选取文件!")
EndIf
EndFunc ;==>OriFile
Func OpenFile() ;打开文件函数
$txtFile = FileOpenDialog("打开文件", @WorkingDir, "文本文件 (*.txt)", 1 + 2 + 8)
If $txtFile <> "" Then
If _FileReadToArray($txtFile, $aRecords) Then ;把文件读入数组
For $x = 1 To $aRecords[0] ;用换行符格式化数组
$ver = $ver & $aRecords[$x] & @CRLF
Next
ChangeSentence() ;变换句子
IniWrite(".\Setting.ini", "Setting", "File", $txtFile) ;将文件路径写入配置文件
Else
MsgBox(0, "Error", "读取文件失败!")
EndIf
EndIf
EndFunc ;==>OpenFile
Func ChangeSentence() ;变换句子函数
$rnd[0] = $rnd[2] ;读取正确的非空语句
$rnd[1] = Random(1, $aRecords[0], 1) ;随机生成一个新的下一句
If $aRecords[$rnd[1]]=="" Or $rnd[1]=$rnd[2] Then ;空行则或随机重复则重新生成
ChangeSentence()
Else
$rnd[2] = $rnd[1] ;存储非空的上一句下标
GUICtrlSetData($LabSentence, $aRecords[$rnd[1]])
EndIf
EndFunc ;==>ChangeSentence
测试用例(复制以下两行并保存为“测试.txt"):