[NSIS]安装前等待读设备结束并根据设备类型选择组件

脚本功能说明:
1、安装包执行前,调用第三方读取插入的设备(第三方程序执行读的功能,并将读设备的结果写到txt中)
2、分析txt,并根据读取结果弹出不同的组件
3、脚本控制了组件选择页必须只选一个才允许“下一步”可用

运行环境说明:
1、等待界面使用NSIS的闪屏效果,需要使用插件NewAdvSplash,该插件解压后释放到NSIS的PLUS目录下即可
2、脚本中的文件均为随意创建的文件,确保脚本能够运行,实际程序不具备功能

!include "MUI.nsh"
!include "LogicLib.nsh"

Name "读设备例子"
OutFile "测试.exe"

!define MUI_WELCOMEFINISHPAGE_BITMAP "files\左侧页面.bmp"
;创建预设函数,控制在欢迎页前执行
!define MUI_PAGE_CUSTOMFUNCTION_PRE Pre
!insertmacro MUI_PAGE_WELCOME
;可控制根据预设条件判断是否要显示组件页,此句要放在需要控制的页前面
!define MUI_PAGE_CUSTOMFUNCTION_Pre ComponentsPre
!insertmacro MUI_PAGE_COMPONENTS
; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
; Finish page
!insertmacro MUI_PAGE_FINISH
; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES
; Language files
!insertmacro MUI_LANGUAGE "SimpChinese"

BrandingText "个人定制,版权所有"
var fDeviceType

ShowInstDetails show # 显示安装进度信息
;Icon "1.ico"   #设置安装图标
;UninstallIcon "1.ico" #设置卸载图标
RequestExecutionLevel admin


Function .oninit
  ;默认未读到设备
  StrCpy $fDeviceType "0"
  ;执行第三方读设备程序,结果写到devtext.txt中:
  SetOverwrite ifnewer
  SetOutPath "$Temp\devtext"
  ;加载闪屏页面,等待读设备结束
  File /oname=splash.bmp "files\加载.bmp"
  newadvsplash::show /NOUNLOAD 2000 1000 500 -2 /BANNER "$Temp\devtext\splash.bmp"

  file  "DeployFiles\ReadDev.exe"
  execwait "$Temp\devtext\ReadDev.exe"
  ClearErrors
  FileOpen $0 $Temp\devtext\devtext.txt r ;//如果文件不存在,或者内容为空,则弹出组件选择
  
  IfErrors done
  FileRead $0 $1
  StrLen $R1 $1
  ${If} $R1 > 0
    ;读到,标记为I
    StrCpy $fDeviceType "1"
  ${EndIf}
  FileClose $0
  done:
  
FunctionEnd

;自定义函数,在欢迎窗体之前执行
Function Pre
    Delete  "$TEMP\splash.bmp"
    Delete  "$TEMP\ReadDev.exe"
    Delete  "$TEMP\devtext.txt"
    
    newadvsplash::stop /wait ; waits or exits immediately if finished, remove /wait to terminate now

;  plug-in requires this to be called in .onGUIInit
;  if you use 'show' in the .onInit function with /BANNER key.
    ;设置欢迎页面置顶
    ShowWindow $HWNDPARENT ${SW_RESTORE}

FunctionEnd

;Section 名字"SEC1"即为页面上展示的组件内容
Section "设备1" SEC1
  # 所有数据库处理在这里写
  MessageBox MB_OK "读到设备1,在这里处理需要的操作"
SectionEnd

Section "设备2" SEC2

  MessageBox MB_OK "读到设备2,在这里处理需要的操作"
  
SectionEnd

Function ComponentsPre

  ;默认都不勾选
  SectionSetFlags ${SEC1} 0
  SectionSetFlags ${SEC2} 0
  ${if} $fDeviceType == "0"
  ;没插设备,默认勾选设备1
    SectionSetFlags ${SEC1} 1
  ${endif}

  ${if} $fDeviceType == "1"
  ;插入设备1,
    SectionSetFlags ${SEC1} 1
    ;不展示控制的组件页
    Abort
  ${endif}

  ${if} $fDeviceType == "2"
  ;插入设备2
    SectionSetFlags ${SEC2} 1
    ;不展示控制的组件页
    Abort
  ${endif}
Functionend


Function .onSelChange
  ;判断组件是否同时勾选或者都没选,只勾选一个才能继续下一步
  SectionGetFlags ${SEC1} $0  # 检测 SEC1 的选择状态,1为已勾选该组件
  IntOp $0 $0 & ${SF_selectED} # 只过滤勾选的状态,Checkbox 的状态可能包含多位
  SectionGetFlags ${SEC2} $1
  IntOp $1 $1 & ${SF_selectED} # 只过滤勾选的状态,Checkbox 的状态可能包含多位

  GetDlgItem $2 $HWNDPARENT 1
  EnableWindow $2 1
  
  ${If} $0 == 0
  ${AndIf} $1 == 0
    GetDlgItem $2 $HWNDPARENT 1 # next/install button
    EnableWindow $2 0
  ${EndIf}
  ${If} $0 == 1
  ${AndIf} $1 == 1
    GetDlgItem $2 $HWNDPARENT 1 # next/install button
    EnableWindow $2 0
  ${EndIf}

FunctionEnd

Function .onGUIEnd
  ;装完后直接删除文件
  RMDIR  "$Temp\devtext"
FunctionEnd

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值