【问题标题】:MSGbox in VBS that updates with value of variableVBS 中使用变量值更新的 MSGbox
【发布时间】:2014-03-19 17:17:53
【问题描述】:

只是想知道我怎么能有一个 MSgbox 来显示一个变量的值,因为它不断变化。基本上,每次循环时都会添加一个数字。我想在不需要打开一百万个窗口的 MSGbox 中显示它

【问题讨论】:

  • MsgBoxInputBox 都是模态的。如果要显示无模式窗口,则需要启动 ActiveX 服务器或 OLE 窗口(如 Internet Explorer)或创建 HTA。或者,您可以使用 CSCRIPT 将变量的值输出到 STDOUT。
  • 检查this WSH VBS GUI 解决方案。 This answer 也可能有帮助。

标签: vbscript


【解决方案1】:

一种解决方法是使用PopUp

Set objShell = WScript.CreateObject("WScript.Shell")
For i = 1 To 3
    objShell.Popup i, 1, "AutoClose MsgBox Simulation", vbInformation+vbOKOnly
Next

这将在 1 秒后“自动关闭”相似的 MsgBox

【讨论】:

  • @Pankaj 谢谢!这有帮助!
【解决方案2】:

您无法使用默认的 VBScript 对话框元素执行此操作,例如 MsgBoxWScript.EchoPopup。您需要使用 Internet Explorer COM 对象构建一个custom dialog

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "about:blank"

While ie.ReadyState <> 4 : WScript.Sleep 100 : Wend

ie.ToolBar   = False
ie.StatusBar = False
ie.Width     = 300
ie.Height    = 200

ie.document.body.innerHTML = "<p id='msg'>0</p>"

Set style = ie.document.CreateStyleSheet
style.AddRule "p", "text-align: center;"

ie.Visible = True

i = 1
Do
  ie.document.getElementById("msg").innerText = i
  i = i + 1
  WScript.Sleep 2000
Loop Until i > 10

或使用 HTA 代替普通的 VBScript:

<head>
<title>Test</title>
<HTA:APPLICATION ID="oHTA"
  APPLICATIONNAME="Test"
  SCROLL="no"
>
</head>

<style type="text/css">
  p {text-align: center;}
</style>

<script language="VBScript">
  window.resizeTo 300, 200

  Set sh = CreateObject("WScript.Shell")

  Sub Window_onLoad
    For i = 1 To 10
      msg.innerText = i
      Sleep 2
    Next
  End Sub

  Sub Sleep(t)
    sh.Run "ping -n " & (t+1) & " 127.0.0.1", 0, True
  End Sub
</script>

<body>
<p id="msg">0</p>
</body>

【讨论】:

    【解决方案3】:

    另一种解决方案,使用 HTA 窗口,没有临时文件:

    dim window, i
    
    set window = createwindow()
    window.document.write "<html><body bgcolor=buttonface>Current loop is: <span id='output'></span></body></html>"
    window.document.title = "Processing..."
    window.resizeto 300, 150
    window.moveto 200, 200
    
    for i = 0 to 32767
        show i
        ' your code here
    next
    
    window.close
    
    function show(value)
        on error resume next
        window.output.innerhtml = value
        if err then wscript.quit
    end function
    
    function createwindow()
        ' source http://forum.script-coding.com/viewtopic.php?pid=75356#p75356
        dim signature, shellwnd, proc
        on error resume next
        set createwindow = nothing
        signature = left(createobject("Scriptlet.TypeLib").guid, 38)
        set proc = createobject("WScript.Shell").exec("mshta about:""<script>moveTo(-32000,-32000);</script><hta:application id=app border=dialog minimizebutton=no maximizebutton=no scroll=no showintaskbar=yes contextmenu=no selection=no innerborder=no /><object id='shellwindow' classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>shellwindow.putproperty('" & signature & "',document.parentWindow);</script>""")
        do
            if proc.status > 0 then exit function
            for each shellwnd in createobject("Shell.Application").windows
                set createwindow = shellwnd.getproperty(signature)
                if err.number = 0 then exit function
                err.clear
            next
        loop
    end function
    

    【讨论】:

      【解决方案4】:

      此脚本将显示循环过程中它进行了多少次循环以及变量的值,并在循环完成后将结果显示在 1 个消息框中。

      Dim RateOfChange, roc, watchvariable : roc = 1 : watchvariable = 1
      
      for i=1 to 25
          watchvariable = (watchvariable * i) * 16
          RateOfChange = RateOfChange & "Iteration[" & roc & "]" & " - Value[" & watchvariable & "]" & vbcrlf
          roc = roc + 1
      next
      
      'NOTE uncomment this below to activate msgbox. 
      'msgbox rateofchange
      wscript.echo rateofchange
      

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多