NSIS安装类似于TeamViewer(便携式模式选项)

时间:2014-04-30 13:08:50

标签: installer nsis

我基本上想要的是类似TeamViewer的第一页,我的NSIS安装程序有以下选项:

  1. 3个单选按钮:仅运行;为当前用户安装;为所有用户安装(需要使用管理员权限重新启动)。
  2. TeamViewer中的许可证标签(即没有实际的EULA页面,只有页脚中的链接)。
  3. 可以更改文本的按钮,即“接受并运行”或“接受并安装”。
  4. 我无法弄清楚如何在UI和控制流方面轻松完成。

    如果用户决定为所有用户安装程序,我还需要能够重启安装程序(即我猜应该有一个可检测的命令行开关,这样如果现有的安装程序将自动采用第三种安装类型)。

    所请求的示例用户界面的屏幕截图:

    enter image description here

    非常感谢NSIS模板示例。

    感谢。

1 个答案:

答案 0 :(得分:2)

...
RequestExecutionLevel user

!include LogicLib.nsh
!include nsDialogs.nsh
!include FileFunc.nsh
!include MUI2.nsh

Var mode
Var modeRadioRun
Var modeRadioInstCU
Var modeRadioInstLM

Function OnRadioChange
GetDlgItem $1 $hwndparent 1 ; Find Install/Next button
${NSD_GetState} $modeRadioRun $0
${If} $0 = ${BST_CHECKED} 
    ${NSD_SetText} $1 "Accept && Run" 
${Else}
    ${NSD_SetText} $1 "Accept && Install" 
${EndIf}
FunctionEnd

Function ModePageCreate
!insertmacro MUI_HEADER_TEXT "Welcome to blah" "blah blah"

${GetParameters} $0
ClearErrors
${GetOptions} "$0" "/ELEVATEDINSTALL" $0
${IfNot} ${Errors}
    UserInfo::GetAccountType
    Pop $0
    ${If} $0 == "Admin"
        StrCpy $mode 1
        Abort ; Skip page and start installing
    ${Else}
        MessageBox mb_iconstop "Admin rights required!"
    ${EndIf}
${EndIf}

nsDialogs::Create 1018
Pop $0

${NSD_CreateRadioButton} 30u 20u 50% 12u "Run"
Pop $modeRadioRun
${NSD_OnClick} $modeRadioRun OnRadioChange

${NSD_CreateRadioButton} 30u 40u 50% 12u "Install for current user"
Pop $modeRadioInstCU
${NSD_OnClick} $modeRadioInstCU OnRadioChange

${NSD_CreateRadioButton} 30u 60u 50% 12u "Install for all users"
Pop $modeRadioInstLM
${NSD_OnClick} $modeRadioInstLM OnRadioChange

${NSD_CreateLink} 20u -14u 50% 12u "License"
Pop $0
${NSD_OnClick} $0 ShowLicense

${NSD_Check} $modeRadioRun
call OnRadioChange ; Trigger button change
nsDialogs::Show
FunctionEnd

Function ModePageLeave
${NSD_GetState} $modeRadioRun $0
${NSD_GetState} $modeRadioInstCU $1
${If} $0 = ${BST_CHECKED}
    InitPluginsDir
    SetOutPath $pluginsdir
    File "myapp.exe"
    ExecWait '"$pluginsdir\myapp.exe"'
    SetOutPath $temp ; Don't lock $pluginsdir
    Quit
${ElseIf} $1 = ${BST_CHECKED}
    StrCpy $mode 0
${Else}
    StrCpy $mode 1
    UserInfo::GetAccountType
    Pop $0
    ${If} $0 != "Admin"
        ExecShell "runas" '"$exepath"' "/ELEVATEDINSTALL"
        Quit
    ${EndIf}
${EndIf}
FunctionEnd

Function ShowLicense
ExecShell "" "http://example.com/license"
FunctionEnd

Page Custom ModePageCreate ModePageLeave
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"

Section
${If} $mode > 0
    SetShellVarContext all
    StrCpy $InstDir "$ProgramFiles\MyApp"
${Else}
    SetShellVarContext current
    StrCpy $InstDir "$LocalAppData\Programs\MyApp"
${EndIf}

SetOutPath $InstDir
File myapp.exe
CreateShortcut "$SMPrograms\MyApp.lnk" "$InstDir\myapp.exe"
WriteUninstaller "$InstDir\Uninst.exe"
WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyAppGuid" "UninstallString" '"$InstDir\Uninst.exe"'
WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyAppGuid" "DisplayName" "MyApp blah blah"
SectionEnd

Section Uninstall
; Todo: Remove files and registry entries (You should write to a .ini in $InstDir so you know if it was a per user or machine install)
RMDir "$InstDir"
SectionEnd

您可能希望编辑基本UI以使用Resource Hacker(在NSIS \ Contrib \ UIs中的一个文件上)更大的安装按钮,并在脚本中使用ChangeUI来应用。< / p>