NSIS脚本学习:弹出窗口MessageBox的使用方法详解

这几天准备系统性地学习一下NSIS脚本的编写。函数

NSIS脚本中的MessageBox,语法以下:工具

MessageBox mb_option_list messagebox_text [/SD return] 
    [return_check jumpto] [return_check_2 jumpto_2]

mb_option_list中列出了MessageBox的设定,有多个设定同时起做用时可用竖线(|)隔开,messagebox_text列出了MessageBox中正文部分显示的文字,/SD表示静默安装时默认返回的结果,return_check、return_check_2列出了两种不一样的返回值,jumpto、jumpto_2分别列出了收到两种不一样返回值后应跳转到的Goto语句标签。学习

不一样按钮的返回值以下:测试

一、IDABORT - Abort button - 【停止】按钮
code

二、IDCANCEL - Cancel button - 【取消】按钮
orm

三、IDIGNORE - Ignore button - 【忽略】按钮
ip

四、IDNO - No button - 【否】按钮
it

五、IDOK - OK button - 【肯定】按钮
io

六、IDRETRY - Retry button - 【重试】按钮
编译

七、IDYES - Yes button - 【是】按钮

下面这段代码,能够当作一个模板:

!define DEBUG_PATH "E:\NSIS_Test\TmpProgram"
!define OUTPUT_PATH "E:\NSIS_Test\Output"

Name "NSIS_MessageBox_Test"
Caption "NSIS_MessageBox_Test"

Function .onInit
  ;TODO - 这里输入要测试的代码
FunctionEnd

OutFile "Galatea.exe"
Section "My Program"
  SetOutPath ${OUTPUT_PATH}
  File /r "${DEBUG_PATH}\*.*"
SectionEnd

我使用 HM NSIS Edit 2.0.3 工具编辑NSIS脚本,使用编译工具 makensis.exe(版本号2.46) 进行编译

本文中测试用的代码都写在.onInit函数中

就mb_option_list中的每一个属性,我都写了段代码测试效果:

一、MB_OK - Display with an OK button

MessageBox只显示一个肯定按钮

Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_OK "MB_OK - Display with an OK button" /SD IDOK IDOK label_ok
label_ok:
  MessageBox MB_OK "你点击了OK"
FunctionEnd

二、MB_OKCANCEL - Display with an OK and a cancel button

MessageBox显示肯定和取消两个按钮

Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_OKCANCEL "MB_OKCANCEL - Display with an OK and a cancel button" \
    /SD IDOK IDOK label_ok IDCANCEL label_cancel
label_ok:
  MessageBox MB_OK "你点击了OK"
  Goto end
label_cancel:
  MessageBox MB_OK "你点击了Cancel"
  Goto end
end:
FunctionEnd

三、MB_ABORTRETRYIGNORE - Display with abort, retry, ignore buttons

MessageBox显示停止、重试和忽略三个按钮

下面是一个错误的写法:

Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_ABORTRETRYIGNORE "MB_ABORTRETRYIGNORE - Display with abort, retry, ignore buttons" \
    /SD IDABORT IDABORT label_abort IDRETRY label_retry IDIGNORE label_ignore
label_abort:
  MessageBox MB_OK "你点击了Abort"
  Goto end
label_retry:
  MessageBox MB_OK "你点击了Retry"
  Goto end
label_ignore:
  MessageBox MB_OK "你点击了Ignore"
  Goto end
end:
FunctionEnd

这个写法的报错信息以下:

Function: ".onInit"
MessageBox expects 2-8 parameters, got 10.
Usage: MessageBox mode messagebox_text [/SD return] _
        [return_check label_to_goto_if_equal [return_check2 label2]]
    mode=modeflag[|modeflag[|modeflag[...]]]
    modeflag=(MB_ABORTRETRYIGNORE|MB_OK|MB_OKCANCEL|MB_RETRYCANCEL|MB_YESNO|MB_YESNOCANCEL _
        |MB_ICONEXCLAMATION|MB_ICONINFORMATION|MB_ICONQUESTION|MB_ICONSTOP|MB_USERICON _
        |MB_TOPMOST|MB_SETFOREGROUND|MB_RIGHT
Error in script "E:\NSIS_Test\galatea.nsi" on line 7 -- aborting creation process

里面说MessageBox只能有2-8个参数,在上面那段错误的代码中咱们传入了10个参数。解决这一问题的方法,就是少写一个return_check条件。

Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_ABORTRETRYIGNORE "MB_ABORTRETRYIGNORE - Display with abort, retry, ignore buttons" \
    /SD IDABORT IDRETRY label_retry IDIGNORE label_ignore
  MessageBox MB_OK "你点击了Abort"
  Goto end
label_retry:
  MessageBox MB_OK "你点击了Retry"
  Goto end
label_ignore:
  MessageBox MB_OK "你点击了Ignore"
  Goto end
end:
FunctionEnd

四、MB_RETRYCANCEL - Display with retry and cancel buttons

MessageBox显示重试和取消两个按钮

Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_RETRYCANCEL "MB_RETRYCANCEL - Display with retry and cancel buttons" \
    /SD IDRETRY IDRETRY label_retry IDCANCEL label_cancel
label_retry:
  MessageBox MB_OK "你点击了Retry"
  Goto end
label_cancel:
  MessageBox MB_OK "你点击了Cancel"
  Goto end
end:
FunctionEnd

五、MB_YESNO - Display with yes and no buttons

MessageBox显示是和否两个按钮

Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_YESNO "MB_YESNO - Display with yes and no buttons" \
    /SD IDYES IDYES label_yes IDNO label_no
label_yes:
  MessageBox MB_OK "你点击了Yes"
  Goto end
label_no:
  MessageBox MB_OK "你点击了No"
  Goto end
end:
FunctionEnd

六、MB_YESNOCANCEL - Display with yes, no, cancel buttons

MessageBox显示是、否和取消三个按钮

Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_YESNOCANCEL "MB_YESNOCANCEL - Display with yes, no, cancel buttons" \
    /SD IDYES IDNO label_no IDCANCEL label_cancel
label_yes:
  MessageBox MB_OK "你点击了Yes"
  Goto end
label_no:
  MessageBox MB_OK "你点击了No"
  Goto end
label_cancel:
  MessageBox MB_OK "你点击了Cancel"
  Goto end
end:
FunctionEnd

七、MB_ICONEXCLAMATION - Display with exclamation icon

MessageBox显示警告标记,可与前面按钮设置相关的功能选项共用

Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_ICONEXCLAMATION "MB_ICONEXCLAMATION - Display with exclamation icon" \
    /SD IDYES IDYes label_yes
label_yes:
  MessageBox MB_OK "你点击了Yes"
FunctionEnd

八、MB_ICONINFORMATION - Display with information icon

MessageBox显示信息标记,可与前面按钮设置相关的功能选项共用

Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_ICONINFORMATION "MB_ICONINFORMATION - Display with information icon" \
    /SD IDYES IDYes label_yes
label_yes:
  MessageBox MB_OK "你点击了Yes"
FunctionEnd

九、MB_ICONQUESTION - Display with question mark icon

MessageBox显示询问标记,可与前面按钮设置相关的功能选项共用

Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_YESNO|MB_ICONQUESTION "MB_ICONQUESTION - Display with question mark icon" \
    /SD IDYES IDYES label_yes IDNO label_no
label_yes:
  MessageBox MB_OK "你点击了Yes"
  Goto end
label_no:
  MessageBox MB_OK "你点击了No"
  Goto end
end:
FunctionEnd

十、MB_ICONSTOP - Display with stop icon

MessageBox显示禁止标记,可与前面按钮设置相关的功能选项共用


Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_ICONSTOP "MB_ICONSTOP - Display with stop icon" \
    /SD IDYES IDYes label_yes
label_yes:
  MessageBox MB_OK "你点击了Yes"
FunctionEnd

十一、MB_USERICON - Display with installer's icon

MessageBox显示用户定义图标,可与前面按钮设置相关的功能选项共用


Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_USERICON "MB_USERICON - Display with installer's icon" \
    /SD IDYES IDYes label_yes
label_yes:
  MessageBox MB_OK "你点击了Yes"
FunctionEnd

十二、MB_TOPMOST - Make messagebox topmost

MessageBox提示窗置顶,可与前面的设置选项同时使用

Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_YESNO|MB_TOPMOST "MB_TOPMOST - Make messagebox topmost" \
    /SD IDYES IDYES label_yes IDNO label_no
label_yes:
  MessageBox MB_OK "你点击了Yes"
  Goto end
label_no:
  MessageBox MB_OK "你点击了No"
  Goto end
end:
FunctionEnd

1三、MB_SETFOREGROUND - Set foreground

设置MessageBox为前景窗口

1四、MB_RIGHT - Right align text

设置MessageBox右对齐


Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_YESNO|MB_RIGHT "MB_RIGHT - Right align text" \
    /SD IDYES IDYES label_yes IDNO label_no
label_yes:
  MessageBox MB_OK "你点击了Yes"
  Goto end
label_no:
  MessageBox MB_OK "你点击了No"
  Goto end
end:
FunctionEnd

1五、MB_RTLREADING - RTL reading order

设置MessageBox阅读顺序为自右向左,该模式下易致使界面显示错乱,故不推荐使用


Function .onInit
  ;这里输入要测试的代码
  MessageBox MB_YESNO|MB_RTLREADING "MB_RTLREADING - RTL reading order" \
    /SD IDYES IDYES label_yes IDNO label_no
label_yes:
  MessageBox MB_OK "你点击了Yes"
  Goto end
label_no:
  MessageBox MB_OK "你点击了No"
  Goto end
end:
FunctionEnd

1六、MB_DEFBUTTON1 - Button 1 is default

1七、MB_DEFBUTTON2 - Button 2 is default

1八、MB_DEFBUTTON3 - Button 3 is default

1九、MB_DEFBUTTON4 - Button 4 is default

注:本文写做过程当中参考了NSIS官方使用手册 NSIS.chm

END

相关文章
相关标签/搜索