如何在NSIS中编辑“选择组件”页面详细脚本
时间: 2024-09-18 16:03:42 浏览: 41
在NSIS(Nullsoft Scripting Installation System)中,编辑“选择组件”页面的详细脚本通常涉及到`WriteIniFile`和`ReadIniFile`命令,以及`PageCustom`标记来创建自定义对话框。以下是基本步骤:
1. **初始化组件列表**:
首先,在安装开始之前,你可以创建一个包含所有组件及其描述的ini文件,如`components.ini`:
```nsis
[Components]
ComponentA=Component A description
ComponentB=Component B description
```
2. **创建选择页面**:
使用`Section`和`SectionEnd`定义每个组件的区域,然后在`WriteUninstaller`段落中添加`PageCustom`标签,指定要插入自定义对话框的位置:
```nsis
Section "Select Components"
WriteIniFile $SMPROGRAMS\$ProductName$\$InstDir\Components.ini "Components" "SelectedComponents" ""
Page Custom "Choose Components" Plugin mydialog.dll
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_STARTMENU_WRITE_BEGIN
!insertmacro MUI_STARTMENU_WRITE_DEFAULT Programs
!insertmacro MUI_STARTMENU_END
SectionEnd
```
3. **编写自定义对话框脚本(mydialog.dll)**:
创建一个DLL插件,其中包含用户界面元素(如Listview或Treeview),并使用`WriteIniFile`读取ini文件,然后根据用户的选择更新它:
```c++
#include <nsis.h>
!define INSTANCE_NAME "MyDialog"
!define CLASS_NAME "MyDialogClass"
Func _MyDialog_OnInit()
${IfNotRegKey} HKCU\Software\$(COMPANYNAME)\My Product\Components $(INSTANCE_NAME)
CreateWindowEx $WS_EX_CLIENTEDGE $(CLASS_NAME) "$(INSTANCE_NAME)" WS_POPUP 0,0,0,0 0x0
Pop $0
EndFunc -Create
Func _MyDialog_Create()
WinSetState $HWND $(INSTANCE_NAME) visible
EndFunc -Create
Func _MyDialog_Destroy()
WinClose $HWND
EndFunc -Destroy
Func _MyDialog_OnClose()
DeleteRegKey HKCU\Software\$(COMPANYNAME)\My Product\Components $(INSTANCE_NAME)
EndFunc -Destroy
Func _MyDialog_DrawItem(
HWND hListView, INT item, DC hdc, RECT FAR *rc, UINT flags, LPARAM lParam
)
Local $str = ReadIniStr $0 "Components.ini" "SelectedComponents" $item
DrawText $hdc $str $rc $DT_LEFT | $DT_VCENTER | $DT_NOPREFIX
EndFunc -DrawItem
```
4. **处理用户选择**:
当用户关闭自定义对话框时,修改ini文件的值以反映他们的选择:
```nsis
Function .onInit
Call _MyDialog_Init
FunctionEnd
Function .onShow
Call _MyDialog_Create
FunctionEnd
Function .onHide
Call _MyDialog_Destroy
FunctionEnd
Function .onClose
Call _MyDialog_Destroy
WriteIniFile $SMPROGRAMS\$ProductName$\$InstDir\Components.ini "Components" "SelectedComponents" $0
FunctionEnd
```
阅读全文