LoginSignup
4
6

More than 3 years have passed since last update.

VBScriptでプログレスバーを表示する方法

Last updated at Posted at 2020-01-20

VBScriptからIEを起動します

  • IEを起動し、DIVを使ってプログレスバーのようなものを表示します
  • 途中実績から平均処理速度を算出し、全体処理量から残時間を計算して表示させます

使い方

必要となるスクリプトを読み込みます

  Set objFS = CreateObject("Scripting.FileSystemObject")
  Execute objFS.OpenTextFile("FProgressBar.vbs", 1).ReadAll()

この2行をスクリプトの冒頭に記述して、クラス定義本体を取り込みます
メインクラス本体FProgressBar.vbsは後述します

コンストラクタを呼び出して表示タイトルを設定

Set objPB = new ProgressBar
objPB.SetTitle "サンプルです"

IEが起動し画面の左上に配置されます

途中経過の表示

全体の作業量をm、現在完了している作業量をnとしたとき、n/mが全体量と現在完了量との割合になります
作業量の割合は0.0が未着手、1.0が終了を表します
作業量を指定してプログレスバーを再表示するには以下を記述します

objPB.SetProgress n/m

完了

全体の作業が終わったらデストラクタを呼び出します

Set objPB = Nothing

IEが終了しプログレスバー画面が消えます

サンプルコード

以下のサンプルでは乱数を使って約10秒の処理を行います
途中50回プログレスバーの更新をしています

sample.vbs
'-------------------------------------------------------------------------------
' sample.vbs : 進捗バー表示サンプル
'-------------------------------------------------------------------------------
Option Explicit
'-------------------------------------------------------------------------------
' 変数定義
'-------------------------------------------------------------------------------
Dim objPB, objFS
Dim i
'-------------------------------------------------------------------------------
' 外部定義ファイル読み込み
'-------------------------------------------------------------------------------
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Execute objFS.OpenTextFile("FProgressBar.vbs", 1).ReadAll()

'-------------------------------------------------------------------------------
' メイン処理
'-------------------------------------------------------------------------------
    WScript.Echo "sample.vbs 開始"

    Set objPB = new ProgressBar
    objPB.SetTitle "サンプルです"

    Randomize                   ' 乱数の初期化
    For i = 1 To 50
        objPB.SetProgress i / 50        ' 実行数/全体数 (1.00で全部終了)
        WScript.Sleep(Rnd * 400)        ' 適当な時間SLEEPさせるため
    Next

    Set objPB = Nothing

    WScript.Echo "sample.vbs 終了"
    WScript.Quit(0)

プログレスバーメインクラス

実行VBSと同一フォルダに配置します

FProgressBar.vbs
'-------------------------------------------------------------------------------
' ProgressBar : 進捗バークラス
'-------------------------------------------------------------------------------
Const WIDTH = 400
Const HEIGHT = 150
Const BAR_WIDTH = 350
Const BAR_HEIGHT = 16
Const BAR_BG = "#C0C0C0"
Const BAR_FG = "#0066FF"

Class ProgressBar

    '----------------------------------------------------
    ' クラス変数定義
    '----------------------------------------------------
    Private strTitle1
    Private nCurrent1
    Private nStartTime, nCurrentTime
    Private objIE
    Private div1, div2, div3

    '----------------------------------------------------
    ' コンストラクタ
    '----------------------------------------------------
    Private Sub Class_Initialize
        Set objIE = CreateObject("InternetExplorer.Application")
        objIE.Visible = False
        objIE.Navigate2 "about:blank"
        objIE.Document.Title = "進捗状況"
        objIE.AddressBar = False
        objIE.MenuBar = False
        objIE.ToolBar = False
        objIE.Resizable = False
        objIE.Width = WIDTH
        objIE.Height = HEIGHT
        objIE.Top = 0
        objIE.Left = 0
        Set div1 = objIE.Document.CreateElement("div")
        div1.Id = "div1"
        div1.style.position = "absolute"
        div1.style.top = "10px"
        div1.style.left = "10px"
        div1.style.backgroundColor = BAR_BG
        div1.style.width = BAR_WIDTH & "px"
        div1.style.height = BAR_HEIGHT & "px"
        div1.style.border = "1px solid"
        div1.style.overflow = "hidden"
        Set div2 = objIE.Document.CreateElement("div")
        div2.Id = "div2"
        div2.style.position = "relative"
        div2.style.top = "1px"
        div2.style.left = "1px"
        div2.style.backgroundColor = BAR_FG
        div2.style.width = "0px"
        div2.style.height = (BAR_HEIGHT - 2) & "px"
        div2.style.overflow = "hidden"
        Set div3 = objIE.Document.CreateElement("div")
        div3.Id = "div3"
        div3.style.position = "absolute"
        div3.style.top = "45px"
        div3.style.left = "10px"

        objIE.Document.Body.AppendChild(div1)
        div1.AppendChild(div2)
        objIE.Document.Body.AppendChild(div3)

        nStartTime = Timer()
    End Sub

    '----------------------------------------------------
    ' デストラクタ
    '----------------------------------------------------
    Private Sub Class_Terminate
        on error resume next
        objIE.Quit
        Set objIE = Nothing
        on error goto 0
    End Sub

    '----------------------------------------------------
    ' 表示タイトルの設定
    '----------------------------------------------------
    Public Sub SetTitle (t)
        strTitle1 = t
        objIE.Document.Title = t & String(40, " ")
        objIE.Visible = True
    End Sub

    '----------------------------------------------------
    ' 進捗パーセントの設定
    '----------------------------------------------------
    Public Sub SetProgress(n1)
        nCurrent1 = n1
        Repaint
    End Sub

    '----------------------------------------------------
    ' 進捗バー再描画
    '----------------------------------------------------
    Private Sub Repaint

    Dim nAverage
    Dim nElapsedTime
    Dim nRemain
    Dim strRemain
    Dim strPercent

    Dim w1
    Dim style1, style2

        nCurrentTime = Timer()
        nElapsedTime = nCurrentTime - nStartTime

        w1 = BAR_WIDTH * (nCurrent1)
        strRemain = "不明"
        If nElapsedTime <> 0 Then
            nAverage = nCurrent1 / nElapsedTime
            If nAverage <> 0 Then
                nRemain = Round((1 - nCurrent1) / nAverage, 1)
            End If
            If nRemain > 60 Then
                strRemain = "約" & CStr(Round(nRemain / 60, 0)) & "分"
            Else
                strRemain = FormatNumber(nRemain, 1) & "秒"
            End If
        End If

        strPercent = FormatNumber(nCurrent1 * 100, 1)

        on error resume next
        div2.style.width = (w1 -1) & "px"
        div3.innerText = strPercent & "%終了 -- 残り推定:" & strRemain
        objIE.Visible = True
        objIE.Document.all(0).Click
        on error goto 0
    End Sub
End Class

サンプルの実行方法

sample.vbsをダブルクリックするか、コマンドプロンプトから、cscript sample.vbsを実行します

4
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6