欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

学习 NSIS 脚本:弹出窗口 MessageBox 的使用细节

最编程 2024-03-23 09:18:01
...
【直播预告】国产数据库,一半都是花架子?”

这几天准备系统性地学习一下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语句标签。

不同按钮的返回值如下:

1、IDABORT - Abort button - 【中止】按钮

2、IDCANCEL - Cancel button - 【取消】按钮

3、IDIGNORE - Ignore button - 【忽略】按钮

4、IDNO - No button - 【否】按钮

5、IDOK - OK button - 【确定】按钮

6、IDRETRY - Retry button - 【重试】按钮

7、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中的每个属性,我都写了段代码测试效果:

1、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

2、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

3、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

4、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

5、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

6、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

7、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

8、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

9、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

10、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

11、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

12、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

13、MB_SETFOREGROUND - Set foreground

设置MessageBox为前景窗口

14、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

15、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

16、MB_DEFBUTTON1 - Button 1 is default

17、MB_DEFBUTTON2 - Button 2 is default

18、MB_DEFBUTTON3 - Button 3 is default

19、MB_DEFBUTTON4 - Button 4 is default

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

END