有没有办法从批处理文件中显示消息框(类似于 xmessage 可以在 Linux 中的 bash 脚本中使用)?

有帮助吗?

解决方案

我想使一个非常简单的VBScript文件和使用的CScript解析命令行参数调用它。

类似于下面的保存在MessageBox.vbs

Set objArgs = WScript.Arguments
messageText = objArgs(0)
MsgBox messageText

,你会叫这样的:

cscript MessageBox.vbs "This will be shown in a popup."

MsgBox参考如果有兴趣去这条路线。

其他提示

首先,DOS无关有了它,你可能想在Windows命令行的解决方案。(再次:没有DOS,纯粹的Windows,只是没有一个窗口,而是一个控制台)

您既可以使用由boflynn提供VBScript的方法或可以误用net sendmsgnet send仅适用于旧版本的Windows:

net send localhost Some message to display

此也取决于信使服务来运行,虽然。

有关新版本(XP及以后,显然):

msg "%username%" Some message to display

应当指出的是,使用msg.exe发送的消息框将仅持续60秒。这可以然而可以与/time:xx开关覆盖。

可能会显示一点点闪光灯,但不需要临时文件。如果所有的工作方式回到在(IIRC)IE5时代的地方。

mshta javascript:alert("Message\n\nMultiple\nLines\ntoo!");close();

不要忘记逃脱你的括号,如果你”重新使用if

if 1 == 1 (
   mshta javascript:alert^("1 is equal to 1, amazing."^);close^(^);
)

这会弹出另一个命令提示窗口:

START CMD /C "ECHO My Popup Message && PAUSE"

尝试:

Msg * "insert your message here" 

如果您使用的是Windows XP的command.com,这将打开一个消息框。

打开一个新cmd窗口是不是你问的很什么,我收集。 你也可以使用的VBScript ,并与你的.bat文件使用。你会从bat文件使用此命令打开它:

cd C:\"location of vbscript"

这样做是更改目录command.com会的文件,从搜索,然后在下一行:

"insert name of your vbscript here".vbs

然后,创建一个新的记事本文档,键入

<script type="text/vbscript">
    MsgBox "your text here"
</script>

您会那么这个保存为.vbs文件(通过把“.VBS”,在文件名的末尾),保存为‘所有文件’,在下拉菜单中的文件名下面方框(所以它不保存为.TXT),然后保存!

这样,你的批处理文件将创建一个VBS脚本,并显示一个弹出。它运行后,将批文件将删除中间文件。

使用MSGBOX的优点是,它是真的customaziable(改变标题,图标等),而MSG.exe是不一样多。

echo MSGBOX "YOUR MESSAGE" > %temp%\TEMPmessage.vbs
call %temp%\TEMPmessage.vbs
del %temp%\TEMPmessage.vbs /f /q

还有几个办法。

1) 最极客和最黑客的 - 它使用 IEXPRESS 创建小 exe,它将创建一个带有单个按钮的弹出窗口(它可以创建另外两种类型的弹出消息)。适用于 XP 及以上版本的所有 Windows:

;@echo off
;setlocal

;set ppopup_executable=popupe.exe
;set "message2=click OK to continue"
;
;del /q /f %tmp%\yes >nul 2>&1
;
;copy /y "%~f0" "%temp%\popup.sed" >nul 2>&1

;(echo(FinishMessage=%message2%)>>"%temp%\popup.sed";
;(echo(TargetName=%cd%\%ppopup_executable%)>>"%temp%\popup.sed";
;(echo(FriendlyName=%message1_title%)>>"%temp%\popup.sed"
;
;iexpress /n /q /m %temp%\popup.sed
;%ppopup_executable%
;rem del /q /f %ppopup_executable% >nul 2>&1

;pause

;endlocal
;exit /b 0


[Version]
Class=IEXPRESS
SEDVersion=3
[Options]
PackagePurpose=InstallApp
ShowInstallProgramWindow=1
HideExtractAnimation=1
UseLongFileName=0
InsideCompressed=0
CAB_FixedSize=0
CAB_ResvCodeSigning=0
RebootMode=N
InstallPrompt=%InstallPrompt%
DisplayLicense=%DisplayLicense%
FinishMessage=%FinishMessage%
TargetName=%TargetName%
FriendlyName=%FriendlyName%
AppLaunched=%AppLaunched%
PostInstallCmd=%PostInstallCmd%
AdminQuietInstCmd=%AdminQuietInstCmd%
UserQuietInstCmd=%UserQuietInstCmd%
SourceFiles=SourceFiles
[SourceFiles]
SourceFiles0=C:\Windows\System32\
[SourceFiles0]
%FILE0%=


[Strings]
AppLaunched=subst.exe
PostInstallCmd=<None>
AdminQuietInstCmd=
UserQuietInstCmd=
FILE0="subst.exe"
DisplayLicense=
InstallPrompt=

2) 使用 MSHTA. 。也适用于 XP 及更高版本的每台 Windows 计算机(尽管 OP 不想要“外部”语言,但此处的 JavaScript 已最小化)。应另存为 .bat:

@if (true == false) @end /*!
@echo off
mshta "about:<script src='file://%~f0'></script><script>close()</script>" %*
goto :EOF */

alert("Hello, world!");

或一行:

mshta "about:<script>alert('Hello, world!');close()</script>"

或者

mshta "javascript:alert('message');close()"

或者

mshta.exe vbscript:Execute("msgbox ""message"",0,""title"":close")

3) 这里是参数化的 .bat/jscript 混合(应另存为 bat)。尽管有 OP 请求,它仍然使用 JavaScript,但由于它是一个 bat,因此可以放心地作为 bat 文件调用。它用 弹出窗口 与更流行的相比,它允许更多的控制 信息盒. 。它使用 WSH,但不像上面的示例中那样使用 MSHTA。

 @if (@x)==(@y) @end /***** jscript comment ******
     @echo off

     cscript //E:JScript //nologo "%~f0" "%~nx0" %*
     exit /b 0

 @if (@x)==(@y) @end ******  end comment *********/


var wshShell = WScript.CreateObject("WScript.Shell");
var args=WScript.Arguments;
var title=args.Item(0);

var timeout=-1;
var pressed_message="button pressed";
var timeout_message="timed out";
var message="";

function printHelp() {
    WScript.Echo(title + "[-title Title] [-timeout m] [-tom \"Time-out message\"] [-pbm \"Pressed button message\"]  [-message \"pop-up message\"]");
}

if (WScript.Arguments.Length==1){
    runPopup();
    WScript.Quit(0);
}

if (args.Item(1).toLowerCase() == "-help" || args.Item(1).toLowerCase() == "-h" ) {
    printHelp();
    WScript.Quit(0);
}

if (WScript.Arguments.Length % 2 == 0 ) {
    WScript.Echo("Illegal arguments ");
    printHelp();
    WScript.Quit(1);
}

for (var arg = 1 ; arg<args.Length;arg=arg+2) {

    if (args.Item(arg).toLowerCase() == "-title") {
        title = args.Item(arg+1);
    }

    if (args.Item(arg).toLowerCase() == "-timeout") {
        timeout = parseInt(args.Item(arg+1));
        if (isNaN(timeout)) {
            timeout=-1;
        }
    }

    if (args.Item(arg).toLowerCase() == "-tom") {
        timeout_message = args.Item(arg+1);
    }

    if (args.Item(arg).toLowerCase() == "-pbm") {
        pressed_message = args.Item(arg+1);
    }

    if (args.Item(arg).toLowerCase() == "-message") {
        message = args.Item(arg+1);
    }
}

function runPopup(){
    var btn = wshShell.Popup(message, timeout, title, 0x0 + 0x10);

    switch(btn) {
        // button pressed.
        case 1:
            WScript.Echo(pressed_message);
            break;

        // Timed out.
        case -1:
           WScript.Echo(timeout_message);
           break;
    }
}

runPopup();

4) 和一个 jscript.net/.bat 混合(应另存为 .bat) .这次使用的是 .NET 并编译了一个小 .exe 可以删除的文件:

@if (@X)==(@Y) @end /****** silent jscript comment ******

@echo off
::::::::::::::::::::::::::::::::::::
:::       compile the script    ::::
::::::::::::::::::::::::::::::::::::
setlocal


::if exist "%~n0.exe" goto :skip_compilation

:: searching the latest installed .net framework
for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
    if exist "%%v\jsc.exe" (
        rem :: the javascript.net compiler
        set "jsc=%%~dpsnfxv\jsc.exe"
        goto :break_loop
    )
)
echo jsc.exe not found && exit /b 0
:break_loop



call %jsc% /nologo /out:"%~n0.exe" "%~f0" 
::::::::::::::::::::::::::::::::::::
:::       end of compilation    ::::
::::::::::::::::::::::::::::::::::::
:skip_compilation

::
::::::::::
"%~n0.exe" %*
::::::::
::
endlocal
exit /b 0

****** end of jscript comment ******/

import System;
import System.Windows;
import System.Windows.Forms

var arguments:String[] = Environment.GetCommandLineArgs();
MessageBox.Show(arguments[1],arguments[0]);

5) 最后对 powershell 进行一次调用,创建一个弹出窗口(如果安装了 powershell,可以从命令行或批处理调用):

powershell [Reflection.Assembly]::LoadWithPartialName("""System.Windows.Forms""");[Windows.Forms.MessageBox]::show("""Hello World""", """My PopUp Message Box""")

6) 德本汉姆的做法可见一斑 这里

start "" cmd /c "echo(&echo(&echo              Hello world!     &echo(&pause>nul"

7)对于系统托盘通知,您可以尝试 :

call SystemTrayNotification.bat  -tooltip warning -time 3000 -title "Woow" -text "Boom" -icon question

下面是一个 PowerShell 变体,不需要在创建窗口之前加载程序集,但它的运行速度明显比 @npocmaka 在此发布的 PowerShell MessageBox 命令慢 (~+50%):

powershell (New-Object -ComObject Wscript.Shell).Popup("""Operation Completed""",0,"""Done""",0x0)

您可以将最后一个参数从“0x0”更改为以下值以在对话框中显示图标(请参阅 弹出方法 以供进一步参考):

        Stop 0x10 停止
        Question Mark 0x20 问号
        Exclamation Mark 0x30 感叹号
        Information Mark 0x40 信息标记

改编自 Microsoft TechNet 文章 电源提示:使用PowerShell显示弹出窗口.

echo X=MsgBox("Message Description",0+16,"Title") >msg.vbs

- 你可以从0,1,2,3,4而不是0写入任何数字(的“+”符号之前)这里是每个数字的含义是:

0 = Ok Button  
1 = Ok/Cancel Button  
2 = Abort/Retry/Ignore button  
3 = Yes/No/Cancel  
4 = Yes/No  

- 您可以(的“+”符号之后)写入来自16,32,48,64代替16任何数字&这里是每个数字的含义是:

16 – Critical Icon  
32 – Warning Icon  
48 – Warning Message Icon   
64 – Information Icon  

信息* “在这里留言”

工作正常,只是保存为记事本.bat文件,或者确保格式设置为“所有文件”

为了做到这一点,你需要有一个显示一个消息框的小程序,并运行从您的批处理文件。

您可以打开,虽然显示提示符控制台窗口,但使用的cmd.exe和朋友得到一个GUI消息框,不仅是可能的,据我所知。

我使用的是从这里msgbox.exe命名的实用程序: http://www.paulsadowski.com/WSH/cmdprogs.htm

可以使用 Zenity 。 Zenity允许的对话框中的命令行和shell脚本执行。的详细信息也可以在维基找到。

这是跨平台:一个Windows的Windows安装程序,可以发现这里 <。 / p>

msg * /time:0 /w Hello everybody!

这个消息永远等待被点击确定,直到(它只有一分钟默认情况下),并在Windows 8.1正常工作

继@鸡的回答,您可以用超时提高其使用只出现10秒以下内容:

mshta "javascript:var sh=new ActiveXObject( 'WScript.Shell' ); sh.Popup( 'Message!', 10, 'Title!', 64 );close()"

请参阅此处更多的细节。

您可以从user32.dll中调用DLL函数,我认为 类似

  

的Rundll32.exe user32.dll中,的MessageBox(0, “文本”, “titleText”,{额外标志像最上面的MessageBox e.t.c})

这是我的手机上输入它,不要评判我......否则我将连接额外的标志。

味精* /server:127.0.0.1键入您的信息在这里

应用 如果您将批处理文件转换(包装)为可执行文件,就可以做到这一点。


  1. 简单的消息框

    %extd% /messagebox Title Text
    

  1. 错误消息框

    %extd% /messagebox  Error "Error message" 16
    
  2. 取消重试消息框

    %extd% /messagebox Title "Try again or Cancel" 5
    

4)“永远不要再问我”消息框

%extd% /messageboxcheck Title Message 0 {73E8105A-7AD2-4335-B694-94F837A38E79}

:一种更好的选择

<强> set my_message=Hello world && start cmd /c "@echo off & mode con cols=15 lines=2 & echo %my_message% & pause>nul"


描述: 行的结果,lines=适量,加1 在该消息中的字符,点击cols=适量,加3(但是,最低必须为15

自动计算cols版本:

<强> set my_message=Hello world && (echo %my_message%>EMPTY_FILE123 && FOR %? IN (EMPTY_FILE123 ) DO SET strlength=%~z? && del EMPTY_FILE123 ) && start cmd /c "@echo off && mode con lines=2 cols=%strlength% && echo %my_message% && pause>nul"

它只需要弹出当VM内,所以在技术上,应该有一些这样的代码:

if %machine_type% == virtual_machine then
   echo message box code
else
   continue normal installation code
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top