NSIS - 如何在.txt文件上写入用户选择的选项

时间:2013-05-15 16:34:21

标签: nsis

我得到了这个脚本(作者:Red Wine),它显示了一个包含用户可以选择安装文件的可用驱动程序的下拉列表。我需要获取用户选择的选项并将其写在.txt文件中。我怎样才能做到这一点? (例如:如果用户选择C:\,恰好是“C:\”那里)

!define SIZE "100" ;add here total uncompressed data size in Mb of your application
!define APPNAME "My Application"
Name '${APPNAME}'
OutFile 'test.exe'
LicenseData '${NSISDIR}\License.txt'
LicenseBkColor 0xFFFFFF
ShowInstDetails show
AllowRootDirInstall true

!include WinMessages.nsh
!include FileFunc.nsh
!insertmacro GetDrives
!insertmacro DriveSpace

Page License
Page Custom CustomCreate CustomLeave
Page InstFiles

Section "boo"
        SetOutPath '$INSTDIR'
SectionEnd

Function CustomCreate
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Settings' 'NumFields' '6'

         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 1' 'Type' 'Label'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 1' 'Left' '5'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 1' 'Top' '5'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 1' 'Right' '-6'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 1' 'Bottom' '17'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 1' 'Text' \
         'Select Installation drive:'

         StrCpy $R2 0
         StrCpy $R0 ''
         ${GetDrives} "HDD" GetDrivesCallBack

         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'Type' 'DropList'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'Left' '30'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'Top' '26'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'Right' '-31'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'Bottom' '100'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'Flags' 'Notify'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'State' '$R1'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 2' 'ListItems' '$R0'

         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 3' 'Type' 'Label'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 3' 'Left' '5'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 3' 'Top' '109'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 3' 'Right' '59'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 3' 'Bottom' '119'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 3' 'Text' \
         'Space required:'

         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 4' 'Type' 'Label'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 4' 'Left' '60'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 4' 'Top' '109'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 4' 'Right' '-5'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 4' 'Bottom' '119'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 4' 'Text' \
         '${SIZE} Mb'

         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 5' 'Type' 'Label'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 5' 'Left' '5'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 5' 'Top' '120'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 5' 'Right' '59'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 5' 'Bottom' '130'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 5' 'Text' \
         'Space available:'

         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Type' 'Label'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Left' '60'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Top' '120'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Right' '-5'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Bottom' '130'
         WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Text' \
         '$R3 Mb'

         push $0
         InstallOptions::Dialog '$PLUGINSDIR\custom.ini'
         pop $0
         pop $0
FunctionEnd

Function CustomLeave
        ReadIniStr $0 '$PLUGINSDIR\custom.ini' 'Settings' 'State'
        StrCmp $0 '2' 0 next
        ReadIniStr $0 '$PLUGINSDIR\custom.ini' 'Field 2' 'State'
        StrCpy $0 $0 3
        ${DriveSpace} "$0" "/D=F /S=M" $R3
        WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Text' \
        '$R3 Mb'
        ReadIniStr $0 '$PLUGINSDIR\custom.ini' 'Field 6' 'HWND'
        SendMessage $0 ${WM_SETTEXT} 0 'STR:$R3 Mb'
        Abort

     next:
        ReadIniStr $0 '$PLUGINSDIR\custom.ini' 'Field 2' 'State'
        StrCpy '$INSTDIR' '$0'
FunctionEnd

Function GetDrivesCallBack
         ${DriveSpace} "$9" "/D=F /S=M" $R4
         IntCmp $R4 '${SIZE}' end end def
      def:
         StrCmp $R2 '0' 0 next
         StrCpy $R3 '$R4'
         StrCpy $R1 '$9${APPNAME}'
         IntOp $R2 $R2 + 1
      next:
         StrCpy $R0 '$R0$9${APPNAME}|'
      end:
     Push $0
FunctionEnd

Function .onInit
         InitPluginsDir
         GetTempFileName $0
         Rename $0 '$PLUGINSDIR\custom.ini'
FunctionEnd

1 个答案:

答案 0 :(得分:1)

按以下方式更改CustomLeave功能:

Function CustomLeave
        ReadIniStr $0 '$PLUGINSDIR\custom.ini' 'Settings' 'State'
        StrCmp $0 '2' 0 next
        ReadIniStr $0 '$PLUGINSDIR\custom.ini' 'Field 2' 'State'
        strcpy $R0  $0
        StrCpy $0 $0 3
        FileOpen $1 'Myfile.txt' w
        FileWrite $1 $R0
        FileClose $1

        ${DriveSpace} "$0" "/D=F /S=M" $R3
        WriteIniStr '$PLUGINSDIR\custom.ini' 'Field 6' 'Text' \
        '$R3 Mb'
        ReadIniStr $0 '$PLUGINSDIR\custom.ini' 'Field 6' 'HWND'
        SendMessage $0 ${WM_SETTEXT} 0 'STR:$R3 Mb'

        Abort

     next:
        ReadIniStr $0 '$PLUGINSDIR\custom.ini' 'Field 2' 'State'
        StrCpy '$INSTDIR' '$0'
FunctionEnd