MsgBox

指定されたテキストを、1つまたは複数のボタン(YesやNoなど)を含む小さなウィンドウに表示します。

MsgBox Text, Title, Options
Result := MsgBox(Text, Title, Options)

パラメータ

Text

型:文字列

If omitted and "OK" is the only button present, it defaults to the string "Press OK to continue.". If omitted in any other case, it defaults to an empty string. Otherwise, specify the text to display inside the message box.

エスケープシーケンスは、特殊文字を表すために使用することができます。例えば、`nは改行文字を示し、現在の行を終了して新しい行を開始します。Thus, using text1`n`ntext2 would create a blank line between text1 and text2.

Textが長い場合、継続セクションによっていくつかの短い行に分割することができ、読みやすさと保守性を向上させることができるかもしれません。

タイトル

型:文字列

省略された場合、A_ScriptNameの現在の値がデフォルトとなります。Otherwise, specify the title of the message box.

Options

型:文字列

If blank or omitted, it defaults to 0 (only an OK button is displayed). Otherwise, specify a combination (sum) of values or a string of one or more options from the tables below to indicate the type of message box and the possible button combinations.

さらに、以下のオプションを0個以上指定することができます:

Owner: To specify an owner window for the message box, use the word Owner followed immediately by a HWND (window ID).

T: Timeout. 指定した時間内にユーザーがメッセージボックスを閉じなかった場合、自動的に閉じるようにするには、文字Tの後にタイムアウトを秒単位で指定します(小数点以下も可)。この値が2147483(24.8日)を超えた場合、2147483に設定されます。メッセージボックスがタイムアウトした場合、戻り値はTimeoutという文字になります。

Optionsパラメータに設定する値

Optionsパラメータには、以下のグループからの数値の組み合わせ(合計)をオペレーティングシステムのMessageBox関数に直接渡すか、0個以上の大文字小文字を区別しないオプションを少なくとも1つのスペースまたはタブで区切った文字列を指定することができます。また、1つ以上の数値オプションを文字列に含めることができます。

Group #1: Buttons

To indicate the buttons displayed in the message box, add one of the following values:

機能 Dec Hex String
OK(つまり、OKボタンのみが表示されます。) 0 0x0 OK or O
OK, Cancel 1 0x1 OKCancel, O/C or OC
Abort, Retry, Ignore 2 0x2 AbortRetryIgnore, A/R/I or ARI
Yes, No, Cancel 3 0x3 YesNoCancel, Y/N/C or YNC
Yes, No 4 0x4 YesNo, Y/N or YN
Retry, Cancel 5 0x5 RetryCancel, R/C or RC
Cancel, Try Again, Continue 6 0x6 CancelTryAgainContinue, C/T/C or CTC

Group #2: Icon

To display an icon in the message box, add one of the following values:

機能 Dec Hex String
Icon Hand (stop/error) 16 0x10 Iconx
Icon Question 32 0x20 Icon?
Icon Exclamation 48 0x30 Icon!
Icon Asterisk (info) 64 0x40 Iconi

Group #3: Default Button

To indicate the default button, add one of the following values:

機能 Dec Hex String
第2ボタンをデフォルトにする 256 0x100 Default2
第3ボタンをデフォルトにする 512 0x200 Default3
第4ボタンをデフォルトにする
(ヘルプボタンがあることが必要です)
768 0x300 Default4

Group #4: Modality

To indicate the modality of the dialog box, add one of the following values:

機能 Dec Hex String
システムモーダル(常に上部に表示される) 4096 0x1000 N/A
Task Modal 8192 0x2000 N/A
常時トップ(スタイル WS_EX_TOPMOST)
(システムモーダル同様、タイトルバーアイコンが省略されています)
262144 0x40000 N/A

Group #5:その他のオプション

To specify other options, add one or more of the following values:

機能 Dec Hex String
ヘルプボタンを追加します(下記備考参照) 16384 0x4000 N/A
テキストを右寄せにします 524288 0x80000 N/A
ヘブライ語/アラビア語の右から左への読み方 1048576 0x100000 N/A

戻り値

型:文字列

この関数は、ユーザーがどのボタンを押したかを表すために、以下の文字列のいずれかを返します:

ダイアログを表示できなかった場合は、空文字列が返されます。これは通常、MsgBoxの制限に達した場合にのみ発生しますが、その他の異常なケースでも発生する可能性があります。

エラー処理

オプションが無効な場合、MsgBoxの制限に達した場合、またはその他の理由でメッセージボックスを表示できなかった場合など、失敗するとErrorがスローされます。

備考

メッセージボックスは通常このように表示されます:

MsgBox

ユーザーがどのボタンを押したかを判断するには、関数の戻り値を使用します。事例:

Result := MsgBox("Would you like to continue?(press Yes or No)",, "YesNo")
if Result = "Yes"
    MsgBox "You pressed Yes."
else
    MsgBox "You pressed No."

if MsgBox("Retry or cancel?",, "R/C") = "Retry"
    MsgBox("You pressed Retry.")

To customize the names of the buttons, see Changing MsgBox's Button Names.

Note: Pressing Ctrl+C while a message box is active will copy its text to the clipboard. これは、AutoHotkeyで作成されたメッセージボックスだけでなく、すべてのメッセージボックスに適用されます。

Using MsgBox with GUI windows: A GUI window may display a modal message box by means of the OwnDialogs option. モーダルメッセージボックスは、メッセージボックスが解除されるまで、ユーザーがGUIウィンドウと対話することを妨げます。この場合、上表からシステムモード、タスクモードのオプションを指定する必要はありません。

OwnDialogsオプションが有効でない場合、Task Modalオプション(8192)を使用すると、ユーザーがメッセージボックスを解除するまで、スクリプトのすべてのウィンドウを無効にすることができます。

OwnerHWNDオプションが指定されている場合は、他の設定より優先されます。HWNDには、スクリプトが所有していないウィンドウのHWNDも指定することができます。

The Help button: When the Help button option (16384) is present in Options, pressing the Help button will have no effect unless both of the following are true:

  1. メッセージボックスは、OwnDialogsオプションにより、GUIウィンドウに所有されます。
  2. スクリプトはWM_HELPメッセージ(0x0053)を監視しています。事例:OnMessage(0x0053, WM_HELP). WM_HELP関数が呼び出されると、別のウィンドウやメッセージボックスを表示するなどの手段でユーザーを誘導することができる。

The Close button (in the message box's title bar): Since the message box is a built-in feature of the operating system, its X button is enabled only when certain buttons are present. If there is only an OK button, clicking the X button is the same as pressing OK. Otherwise, the X button is disabled unless there is a Cancel button, in which case clicking the X is the same as pressing Cancel.

Maximum 7 ongoing calls: The thread displaying a message box can typically be interrupted, allowing the new thread to display its own message box before the previous call returns. MsgBoxの呼び出しは最大7回まで可能ですが、7回目を超えるとErrorが投げられます。中断されたスレッドでMsgBoxを呼び出すと、スレッドが再開されるまで戻ることができないことに注意してください。

InputBox, FileSelect, DirSelect, ToolTip, Gui object

特定のテキストを含むメッセージボックスを表示します。情報を素早く簡単に表示する方法です。ユーザーはOKボタンを押すことでメッセージボックスを閉じ、実行を継続することができます。

MsgBox "This is a string."

特定のテキストとタイトルを含むメッセージボックスを表示します。

MsgBox "This MsgBox has a custom title.", "A Custom Title"

デフォルトのテキストでメッセージボックスを表示します。主にデバッグ用途で、スクリプトにブレークポイントを素早く設定する場合などに便利です。

MsgBox ; "Press OK to continue."

特定のテキスト、タイトル、情報アイコンを含むメッセージボックスが表示されます。そのほか、複数行のテキストをよりわかりやすく表示するために、コンティニュアスセクションを使用しています。

MsgBox "
  (
    The first parameter is displayed as the message.
The second parameter becomes the window title.
The third parameter determines the type of message box.
)", "Window Title", "iconi"

戻り値から、ユーザーがメッセージボックスのどのボタンを押したかを判断します。この場合、MsgBox関数呼び出しは括弧で指定する必要があることに注意してください。

result := MsgBox("Do you want to continue?(Press YES or NO)",, "YesNo")
if (result = "No")
    return

T(タイムアウト)オプションを使用すると、一定の秒数後にメッセージボックスを自動的に閉じます。

result := MsgBox("This MsgBox will time out in 5 seconds.  Continue?",, "Y/N T5")
if (result = "Timeout")
    MsgBox "You didn't press YES or NO within the 5-second period."
else if (result = "No")
    return

メッセージに変数や部分式を含める。こちらもご覧ください:Concatenation

var := 10
MsgBox "The initial value is: " var
MsgBox "The result is: " var * 2
MsgBox Format("The result is: {1}", var * 2)