<style type="text/css"> .sized1 { width: 30em; } .sized2 { width: 4em; } </style> </head> <body> <h1>MsgBox</h1> <p>在含有一个或多个按钮(例如 "是" 和 "否") 的小窗口中显示指定的文本.</p> <pre class="Syntax"><span class="func">MsgBox</span> <span class="optional">Text, Title, Options</span> Result := <span class="func">MsgBox</span>(<span class="optional">Text, Title, Options</span>)</pre> <h2 id="Parameters">参数</h2> <dl> <dt>Text</dt> <dd> <p>类型: <a href="../Concepts.htm#strings">字符串</a></p> <p>如果省略并且 "OK" 是唯一存在的按钮, 则默认为字符串 "Press OK to continue.". 如果在任何其他情况下省略, 则默认为空字符串. 否则, 指定要在消息框内显示的文本.</p> <p>可以使用<a href="../misc/EscapeChar.htm">转义序列</a>来表示特殊字符. 例如, `n 表示换行符, 它结束当前行并开始一个新行. 因此, 使用 <code>text1`n`ntext2</code> 会在 text1 和 text2 之间添加一个空行.</p> <p>如果 <em>Text</em> 较长, 可以通过<a href="../Scripts.htm#continuation">延续片段</a>的方法将其分解成较短的几行, 这样可以增加可读性和可维护性.</p> </dd> <dt>Title</dt> <dd> <p>类型: <a href="../Concepts.htm#strings">字符串</a></p> <p>如果省略, 则默认为 <a href="../Variables.htm#ScriptName">A_ScriptName</a> 的当前值. 否则, 请指定消息框的标题.</p> </dd> <dt>Options</dt> <dd> <p>类型: <a href="../Concepts.htm#strings">字符串</a></p> <p>如果为空或省略, 则默认为 0(只显示 OK 按钮)). 否则, 从下表中指定值的组合(总和) 或一个或多个选项的字符串, 以指示消息框的类型和可能的按钮组合.</p> <p>此外, 还可以指定以下零个或多个选项:</p> <p id="OwnerOption"><strong>Owner:</strong> 要为消息框指定<a href="#Owner">所有者窗口</a>, 请使用单词 Owner 后接 HWND(窗口 ID).</p> <p id="Timeout"><strong>T:</strong> 超时. 如果用户在指定的时间内没有关闭消息框, 要让消息框自动关闭, 请使用字母 T 后接超时秒数, 可以包含小数点. 如果这个值超过 2147483(24.8 天), 将被设置为 2147483. 如果消息框超时, <a href="#Result">返回值</a>为单词 Timeout.</p> </dd> </dl> <h2 id="vftop"><em>Options</em> 参数的值</h2> <p><em>Options</em> 参数可以是以下组的数字值的组合(和), 这些值直接传递给操作系统的 MessageBox 函数, 也可以是由至少一个空格或制表符分隔的不区分大小写的选项组成的字符串. 字符串中还可以包含一个或多个数字选项.</p> <h3 id="Group_1_Buttons">组 #1: 按钮</h3> <p>若要指定在消息框中显示的按钮, 请添加以下值 <u>之一</u>:</p> <table class="info"> <tr> <th class="sized1">功能</th> <th class="sized2 right">十进制</th> <th class="sized2 right">十六进制</th> <th>字符串</th> </tr> <tr> <td>OK(即, 仅显示一个 OK(确认) 按钮)</td> <td class="right">0</td> <td class="right">0x0</td> <td><code>OK</code> 或 <code>O</code></td> </tr> <tr> <td>确认和 Cancel(取消)</td> <td class="right">1</td> <td class="right">0x1</td> <td><code>OKCancel</code>, <code>O/C</code> 或 <code>OC</code></td> </tr> <tr> <td>Abort(中止(A)), Retry(重试(R)) 和 Ignore(忽略(I))</td> <td class="right">2</td> <td class="right">0x2</td> <td><code>AbortRetryIgnore</code>, <code>A/R/I</code> 或 <code>ARI</code></td> </tr> <tr> <td>Yes(是(Y)), No(否(N)) 和取消</td> <td class="right">3</td> <td class="right">0x3</td> <td><code>YesNoCancel</code>, <code>Y/N/C</code> 或 <code>YNC</code></td> </tr> <tr> <td>是(Y) 和否(N)</td> <td class="right">4</td> <td class="right">0x4</td> <td><code>YesNoCancel</code>, <code>Y/N/C</code> 或 <code>YNC</code></td> </tr> <tr> <td>重试(R) 和取消</td> <td class="right">5</td> <td class="right">0x5</td> <td><code>RetryCancel</code>, <code>R/C</code> 或 <code>RC</code></td> </tr> <tr> <td>取消, Try Again(重试(T)) 和 Continue(继续(N))</td> <td class="right">6</td> <td class="right">0x6</td> <td><code>CancelTryAgainContinue</code>, <code>C/T/C</code> 或 <code>CTC</code></td> </tr> </table> <h3 id="Group_2_Icon">组 #2: 图标</h3> <p>要在消息框中显示一个图标, 请添加以下值 <u>之一</u>:</p> <table class="info"> <tr> <th class="sized1">功能</th> <th class="sized2 right">十进制</th> <th class="sized2 right">十六进制</th> <th>字符串</th> </tr> <tr> <td>停止/错误图标.</td> <td class="right">16</td> <td class="right">0x10</td> <td><code>Iconx</code></td> </tr> <tr> <td>问号图标.</td> <td class="right">32</td> <td class="right">0x20</td> <td><code>Icon?</code></td> </tr> <tr> <td>惊叹号图标.</td> <td class="right">48</td> <td class="right">0x30</td> <td><code>Icon!</code></td> </tr> <tr> <td>星号图标(信息).</td> <td class="right">64</td> <td class="right">0x40</td> <td><code>Iconi</code></td> </tr> </table> <h3 id="Group_3_Default_Button">组 #3: 默认按钮</h3> <p>若要指定默认按钮, 请添加以下值 <u>之一</u>:</p> <table class="info"> <tr> <th class="sized1">功能</th> <th class="sized2 right">十进制</th> <th class="sized2 right">十六进制</th> <th>字符串</th> </tr> <tr> <td>使第二个按钮成为默认按钮.</td> <td class="right">256</td> <td class="right">0x100</td> <td><code>Default2</code></td> </tr> <tr> <td>使第三个按钮成为默认按钮.</td> <td class="right">512</td> <td class="right">0x200</td> <td><code>Default3</code></td> </tr> <tr> <td>使第四个按钮为默认的. 需要存在 <a href="#Help">Help(帮助) 按钮</a></td> <td class="right">768</td> <td class="right">0x300</td> <td><code>Default4</code></td> </tr> </table> <h3 id="Group_4_Modality">组 #4: 模式</h3> <p>若要指定对话框的模式, 请添加以下值 <u>之一</u>:</p> <table class="info"> <tr> <th class="sized1">功能</th> <th class="sized2 right">十进制</th> <th class="sized2 right">十六进制</th> <th>字符串</th> </tr> <tr> <td>系统模式(始终置顶)</td> <td class="right">4096</td> <td class="right">0x1000</td> <td>N/A</td> </tr> <tr> <td>任务模式</td> <td class="right">8192</td> <td class="right">0x2000</td> <td>N/A</td> </tr> <tr> <td>置顶(WS_EX_TOPMOST 样式)<br>(和系统模式类似, 但省略了标题栏图标)</td> <td class="right">262144</td> <td class="right">0x40000</td> <td>N/A</td> </tr> </table> <h3 id="Group_5_Other_Options">组 #5: 其他选项</h3> <p>若要指定其他选项, 请添加以下值中的 <u>一个或多个</u>:</p> <table class="info"> <tr> <th class="sized1">功能</th> <th class="sized2 right">十进制</th> <th class="sized2 right">十六进制</th> <th>字符串</th> </tr> <tr id="Help"> <td>添加帮助按钮(请参阅下面的备注)</td> <td class="right">16384</td> <td class="right">0x4000</td> <td>N/A</td> </tr> <tr> <td>让文本右对齐显示.</td> <td class="right">524288</td> <td class="right">0x80000</td> <td>N/A</td> </tr> <tr> <td>用于希伯来语/阿拉伯语的从右向左的阅读顺序.</td> <td class="right">1048576</td> <td class="right">0x100000</td> <td>N/A</td> </tr> </table> <h2 id="Return_Value">返回值</h2> <p>类型: <a href="../Concepts.htm#strings">字符串</a></p> <p>该函数返回以下字符串中之一来表示用户按下了哪个按钮:</p> <ul> <li>OK</li> <li>Cancel</li> <li>Yes</li> <li>No</li> <li>Abort</li> <li>Retry</li> <li>Ignore</li> <li>TryAgain</li> <li>Continue</li> <li>Timeout(即, 如果消息框超时, 则返回单词 "timeout")</li> </ul> <p>如果对话框无法显示, 返回空字符串. 这通常只发生在 <a href="#max">MsgBox 达到限制</a>的情况下, 但也可能是特殊情况.</p> <h2 id="Error_Handling">错误处理</h2> <p>失败时抛出 <a href="Error.htm">Error</a>, 如选项无效, 达到 <a href="#max">MsgBox 限制</a>, 或由于其他原因无法显示消息框.</p> <h2 id="Remarks">备注</h2> <p>消息框通常看起来像这样:</p> <img src="../static/dlg_message.png" alt="MsgBox" /> <p>要确定用户按下了哪个按钮, 请使用函数的<a href="#Result">返回值</a>. 例如:</p> <pre>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.")</pre> <p>若要自定义按钮的名称, 请参阅<a href="../scripts/index.htm#MsgBoxButtonNames">Changing MsgBox's Button Names</a>.</p> <p class="note"><strong>注意:</strong> 当消息框窗口处于活动状态时, 按下 <kbd>Ctrl</kbd>+<kbd>C</kbd> 将复制其中的文本到剪贴板. 这适用于所有的消息框, 而不仅是 AutoHotkey 生成的那些.</p> <p id="Owner"><strong>GUI 窗口当作 MsgBox:</strong> 通过使用 <a href="Gui.htm#OwnDialogs">OwnDialogs 选项</a>的方法 GUI 窗口可以显示为 <em>modal(模式)</em> 消息框. <em>模式</em> 消息框在其消失之前会阻止用户与原来的 GUI 窗口进行交互. 此时, 不需要指定上表中的系统模式或任务模式选项.</p> <p>当 <a href="Gui.htm#OwnDialogs">OwnDialogs 选项</a> <em>无效</em> 时, 任务模式选项(8192) 可用于禁用所有脚本的窗口, 直到用户取消消息框.</p> <p>如果指定了 <code>Owner<i>HWND</i></code> 选项, 它将优先于任何其他设置. <i>HWND</i> 可以是任何窗口的 HWND, 即使不是脚本所拥有的窗口.</p> <p><strong>帮助按钮:</strong> 在 <em>Options</em> 中使用了帮助按钮选项(16384) 时, 只有符合下面两个条件, 那么按下帮助按钮才会有效果:</p> <ol> <li>消息框是使用 <a href="Gui.htm#OwnDialogs">OwnDialogs 选项</a>的方法为 Gui 窗口所有.</li> <li>脚本正在监控 WM_HELP 消息(0x0053). 例如: <code><a href="OnMessage.htm">OnMessage</a>(0x0053, "WM_HELP")</code>. 当 WM_HELP 函数被调用时, 可能会通过诸如显示另一个窗口或消息框等方式引导用户.</li> </ol> <p><strong>关闭按钮(在消息框的标题栏):</strong> 由于消息框窗口是操作系统内置的特性, 所以它的 X 按钮只有在某些按钮存在时才会被启用. 如果只有一个 OK(确认) 按钮, 点击 X 按钮等同于点击 OK 按钮. 否则, X 按钮将被禁用, 除非有一个取消按钮, 在这种情况下, 点击 X 按钮就等同于按下取消.</p> <p id="max"><strong>最大持续调用数 7:</strong> 显示消息框的<a href="../misc/Threads.htm">线程</a>通常可以被中断, 从而允许新线程在前一个调用返回之前显示自己的消息框. 最多允许对 MsgBox 进行 7 次进行中的调用, 任何第 7 次以后的调用都会抛出 <a href="Error.htm">Error</a>. 注意, 在中断的线程中, 对 MsgBox 的调用只有在线程恢复后才能返回.</p> <h2 id="Related">相关</h2> <p><a href="InputBox.htm">InputBox</a>, <a href="FileSelect.htm">FileSelect</a>, <a href="DirSelect.htm">DirSelect</a>, <a href="ToolTip.htm">ToolTip</a>, <a href="Gui.htm">Gui 对象</a></p> <h2 id="Examples">示例</h2> <div class="ex" id="ExBasic"> <p><a class="ex_number" href="#ExBasic"></a> 显示一个带有特定文字的信息框. 一种快速简单的信息显示方式. 用户可以按确定按钮关闭信息框并继续执行.</p> <pre>MsgBox "This is a string."</pre> </div> <div class="ex" id="ExTitle"> <p><a class="ex_number" href="#ExTitle"></a> 显示一个带有特定文本和标题的消息框.</p> <pre>MsgBox "This MsgBox has a custom title.", "A Custom Title"</pre> </div> <div class="ex" id="ExNoParams"> <p><a class="ex_number" href="#ExNoParams"></a> 显示一个带有默认文本的消息框. 主要用于调试目的, 例如快速设置脚本中的断点.</p> <pre>MsgBox <em>; "Press OK to continue."</em></pre> </div> <div class="ex" id="ExContSec"> <p><a class="ex_number" href="#ExContSec"></a> 显示一个带有特定文本, 标题和信息图标的消息框. 此外, <a href="../Scripts.htm#continuation">延续片段</a>用于更清晰地显示多行文本.</p> <pre> MsgBox " ( 第一个参数显示为信息. 第二个参数成为窗口标题. 第三个参数决定了消息框的类型. )", "Window Title", "iconi" </pre> </div> <div class="ex" id="ExRetValue"> <p><a class="ex_number" href="#ExRetValue"></a> 使用返回值来确定用户在消息框中按了哪个按钮. 注意, 在这种情况下, MsgBox 函数调用必须用<a href="../Language.htm#function-call-statements">括号</a>来指定.</p> <pre>result := MsgBox("Do you want to continue? (Press YES or NO)",, "YesNo") if (result = "No") return</pre> </div> <div class="ex" id="ExTimeout"> <p><a class="ex_number" href="#ExTimeout"></a> 使用 T(超时) 选项, 在一定秒数后自动关闭消息框.</p> <pre>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</pre> </div> <div class="ex" id="ExExpr"> <p><a class="ex_number" href="#ExExpr"></a> 在消息中包含一个变量或子表达式. 另请参阅: <a href="../Variables.htm#concat">连接</a></p> <pre> var := 10 MsgBox "The initial value is: " var MsgBox "The result is: " var * 2 MsgBox <a href="Format.htm">Format</a>("The result is: {1}", var * 2) </pre> </div> </body> </html>