- ' VBS在CMD单行输出显示(宽度80) By Yu2n
- Option Explicit
-
- CommandMode "在CMD单行输出显示(宽度80)"
-
- Dim a(100), s, i, o
- Execute(Join(a,"s=s&i&Chr(32)&Chr(32):i=i+1:a(i)=i:"))
-
- '单行显示进度效果
- WScript.Echo ""
- WScript.Echo "1. 单行显示进度效果"
- For Each o In a
- EchoLine o
- WScript.Sleep 50
- Next
-
- '单行跑马灯效果
- WScript.Echo ""
- WScript.Echo "2. 单行跑马灯效果"
- WScript.Sleep 1500
- Roll s
-
- '单行显示时间效果
- WScript.Echo ""
- WScript.Echo "3. 单行显示时间效果"
- Do
- EchoLine Now()
- WScript.Sleep 500
- Loop
-
- '单行跑马灯效果
- Sub Roll(ByVal str)
- Dim n, sLine, nLeft
- nLeft=0
- If Len(str) >= 79 Then nLeft=79
- For n=0 To Len(str) - nLeft
- sLine=Right(str,Len(str)-n) & Left(str,n)
- EchoLine sLine
- WScript.Sleep 50
- Next
- WScript.Sleep 1500
- For n=nLeft To Len(str)
- sLine=Right(str,n) & Left(str,Len(str)-n)
- EchoLine sLine
- WScript.Sleep 50
- Next
- End Sub
-
- '在CMD单行输出显示(宽度80)
- Sub EchoLine(ByVal sMsg)
- If InStr(1,WScript.FullName,"\wscript.exe",vbTextCompare)>0 Then Exit Sub
- If strLength(sMsg) > 79 Then sMsg = strLeft(sMsg,79) & Chr(13)
- WScript.StdOut.Write Chr(13) & String(79,Chr(32)) & Chr(13)
- WScript.StdOut.Write sMsg
- End Sub
-
- '左取字符,按中文长度为2、英文长度为1
- 'strLeft("1二3四5",1)="1"
- 'strLeft("1二3四5",2)="1 "
- 'strLeft("1二3四5",3)="1二"
- 'strLeft("1二3四5",4)="1二3"
- 'strLeft("1二3四5",5)="1二3 "
- Function strLeft(ByVal str, ByVal nLength)
- On Error Resume Next
- Dim n, s, l
- If (nLength Mod 2 = 0) Then
- l=nLength/2
- Else
- l=Int(nLength/2)+1
- End If
- For n=l To strLength(str)
- If strLength(Left(str,n)) <= nLength Then
- s=Left(str,n)
- End If
- Next
- s=s & String(nLength-strLength(s),Chr(32))
- strLeft=s
- End Function
-
- '计算字符串长度,中文字符长为2、英文中文字符长为1
- Function strLength(ByVal str)
- On Error Resume Next
- If (Len(ChrW(20013) & ChrW(25991)) = 2) Then
- Dim l, t, c, i: l = Len(str): t = l
- For i = 1 To l
- c = Asc(Mid(str, i, 1))
- If c < 0 Then c = c + 65536
- If c > 255 Then t = t + 1
- Next
- strLength = t
- Else
- strLength = Len(str)
- End If
- If Err.Number <> 0 Then Err.Clear
- End Function
-
- ' 以命令提示符环境运行(保留参数)
- Sub CommandMode(ByVal sTitle)
- If InStr(1, WScript.FullName, "\cscript.exe", vbTextCompare) > 0 Then Exit Sub
- Dim sCommand, oArg, sArgs
- sCommand = "%Comspec% /c title " & sTitle & " & cscript.exe //NoLogo """ & WScript.ScriptFullName & """"
- For Each oArg In WScript.Arguments
- sArgs = sArgs & " " & """" & oArg & """"
- Next
- CreateObject("WScript.Shell").Run sCommand & sArgs & " & pause", 1, False
- WScript.Quit
- End Sub
复制代码 结果如下:(动态显示、以下是部分效果):- 1. 单行显示进度效果
- 100
- 2. 单行跑马灯效果
- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
- 3. 单行显示时间效果
- 2015/7/28 23:04:58
复制代码
|