LoginSignup
4
4

More than 5 years have passed since last update.

【自分用】バッチファイルで選択肢をGUI表示するときのテンプレ

Last updated at Posted at 2015-12-19

複数の選択肢。どれを選ぶかWASDで操作させたいときにこれをコピペする
Vista以降

見た目
無題 - コピー.png

準備

どこにどの選択肢を配置するかをあらかじめ設定しておく
・可読性を上げるために配列風の記述をする
・[0][0]始まりではなく[1][1]にする。計算時に負の数を発生させないようにするため
・文字列の幅が違うとがたがたになる。事前に直しておくこと
 展望:選択肢ごとの文字列の幅を調べて最大のものに合わせて自動で空白埋めさせたい
    選択肢をそれぞれテキストファイルに突っ込んで、順にバイト数を取っていくのが確実か。
    欠点は要素数に比例して時間がかかることと一時的なファイルの量産

command[1][1] command[1][2] command[1][3]
command[2][1] command[2][2] command[2][3]
command[3][1] command[3][2] command[3][3]

以下のコマンドを実行しておく。

call :cursor 0 (縦のサイズ) (横のサイズ)
rem 例:call :cursor 0 3 2

選択肢の上に表示したい文章があれば、それを適当な名前のテキストファイルに突っ込む
予め突っ込んでおいたファイルを用意してもいい
・半角パーセントの個数に気を付ける
・バッチ内で用意する場合はecho 文章>>hoge.txt
 予め用意しておく場合、%%変数名%%と書いておくことでバッチ内で設定した変数を読み込める。

実行

以下のコマンドを実行する。

call :select (WASD以外の選択肢) (さっき作ったテキストファイルの.txt以前)

・当然、txtを使わない場合は引数2は要らない。
・txtのディレクトリが異なる場合、フルパスで記入。フルパスの場合、スペースがあるとバグる。無くせ

結果

WASD以外の選択肢が選ばれると、何番目が選ばれたかをerrorlevelに入れて出してくる。
call :select jkとしたなら、JかKが押されたときに5,6が帰ってくる
その時選ばれていたコマンドは[cursorm] [cursorn]に出力。

使っている変数(重複防止用)

arraym...縦のサイズ
arrayn...横のサイズ
cursorl[m][n]...全角スペースか【
cursorr[m][n]...全角スペースか】
cursorm...今指してる縦
cursorn...今指してる横
command[m][n]...各コマンド
commandline[m]...選択肢を横に一列ずつならべたやつ

以下コピペ

@echo off
setlocal enabledelayedexpansion
cd /d %~dp0




:select
    for /l %%i in (1,1,%arraym%) do (
        set commandlist[%%i]= 
        for /l %%j in (1,1,%arrayn%) do (
            set commandlist[%%i]=!commandlist[%%i]!!cursorl[%%i][%%j]!!command[%%i][%%j]!!cursorr[%%i][%%j]!
        )
    )
    cls
    if exist "%2.txt" type "%2.txt"
    for /l %%i in (1,1,%arraym%) do (
        echo !commandlist[%%i]!
    )
    choice /c wasd%1 >nul
    if %errorlevel% leq 4 (
        call :cursor %errorlevel%
        goto select
    )
    exit /b

:cursor
    if not "%3" == "" set /a arraym=%2,arrayn=%3
    set cursorl[%cursorm%][%cursorn%]= 
    set cursorr[%cursorm%][%cursorn%]= 
    call :cursor_%1
    set cursorl[%cursorm%][%cursorn%]=set cursorr[%cursorm%][%cursorn%]=exit /b
:cursor_0
    for /l %%i in (1,1,!arraym!) do (
        for /l %%j in (1,1,!arrayn!) do (
            set cursorl[%%i][%%j]= 
            set cursorr[%%i][%%j]= 
        )
    )
    set /a cursorm=1,cursorn=1
    exit /b
:cursor_1
    set /a cursorm=(cursorm+arraym-2)%%arraym+1
    exit /b
:cursor_2
    set /a cursorn=(cursorn+arrayn-2)%%arrayn+1
    exit /b
:cursor_3
    set /a cursorm=cursorm%%arraym+1
    exit /b
:cursor_4
    set /a cursorn=cursorn%%arrayn+1
    exit /b
4
4
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
4