2016-11-23 56 views
0

在NSIS SelectFolderDialog可不知何故,我只顯示硬盤的樹?我需要限制用戶選擇任何不在HDD上的目錄。NSIS SelectFolderDialog特定的驅動器

+0

究竟你的意思是什麼硬盤?只是不是網絡的東西?特別是一個本地硬盤,不包括軟盤和cdrw? – Anders

+0

另外請記住,您可以在指向Vista和更高版本上的網絡位置的HDD上創建符號鏈接。 – Anders

+0

是,僅限本地硬盤驅動器,不包括軟盤驅動器,cdrw等。 – Erlas

回答

0

要完全控制哪些項目存在,您必須編寫一個定製的NSIS插件,該插件調用SHBrowseForFolder並提供IFolderFilterSite的實現。

另一種方法是禁用當用戶選擇的東西,是不能接受你的OK按鈕:

!include LogicLib.nsh 

!ifndef DRIVE_FIXED 
!define DRIVE_FIXED 3 
!endif 
; This function decides which items are acceptable 
Function MyFolderValidator ; Input: $1=PIDL, $2=Path Output:$0=0 if invalid 
System::Call 'KERNEL32::GetVolumePathName(tr2, t.r4, i ${NSIS_MAX_STRLEN})i.r5' 
${If} $5 <> 0 
    StrCpy $2 $4 
${ElseIf} $5 == "error" ; GetVolumePathName is Win2000+ 
    StrCpy $2 $2 3 ; GetDriveType only accepts root paths (This will not work for UNC) 
${EndIf} 
System::Call 'KERNEL32::GetDriveType(tr2)i.r3' 
${If} $3 = ${DRIVE_FIXED} 
    StrCpy $0 1 ; Allow this path 
${Else} 
    StrCpy $0 0 ; Don't allow this path 
${EndIf} 
FunctionEnd 

!include WinMessages.nsh 
!define /math BFFM_ENABLEOK ${WM_USER} + 101 
!define BFFM_SELCHANGED 2 
!define BFFM_VALIDATEFAILEDA 3 
!define BFFM_VALIDATEFAILEDW 4 
!if "${NSIS_CHAR_SIZE}" > 1 
!define BFFM_VALIDATEFAILED ${BFFM_VALIDATEFAILEDW} 
!else 
!define BFFM_VALIDATEFAILED ${BFFM_VALIDATEFAILEDA} 
!endif 
!if "${NSIS_PTR_SIZE}" <= 4 
Function BrowseForValidatedFolder ; NSIS 2.51+ 
System::Store S 
Pop $2 ; HeadingText 
Pop $1 ; ValidatorFunc 
System::Get "(p.R1, i.R2, p.R3, p)i R8R8" ; BFFCALLBACK 
Pop $R9 ; The system plug-in callback function 
StrCpy $4 "kR9" ; SHBrowseForFolder callback parameter 
System::Call '*(&t261 "")p.r7' ; pszDisplayName buffer 
System::Call '*(p $hwndparent, p0, pr7, t r2, i 0x51, $4, p0, i)p.r8' ; BROWSEINFO struct 
!if "${NSIS_CHAR_SIZE}" > 1 
System::Call 'SHELL32::SHBrowseForFolderW(pr8)p.r9' 
!else 
System::Call 'SHELL32::SHBrowseForFolderA(pr8)p.r9' 
!endif 
BFFCALLBACK_loop: 
    StrCpy $R8 $R8 8 ; HACKHACK: Working around 2.x bug where the callback IDs are never released 
    StrCmp $R8 "callback" 0 BFFCALLBACK_done 
    ${If} $R2 = ${BFFM_SELCHANGED} 
    ${AndIf} $R3 P<> 0 
     System::Store S 
     StrCpy $0 $1 
     StrCpy $1 $R3 
     System::Call 'SHELL32::SHGetPathFromIDList(p $R3, t.r2)' 
     Call $0 
     SendMessage $R1 ${BFFM_ENABLEOK} 0 $0 
     System::Store L 
    ${EndIf} 
    StrCpy $R8 0 ; Yep, the return value is in the same place as the callback id 
    ${IfThen} $R2 = ${BFFM_VALIDATEFAILED} ${|} StrCpy $R8 1 ${|} ; Keep the dialog open 
    System::Call $R9 
    goto BFFCALLBACK_loop 
BFFCALLBACK_done: 
System::Free $R9 ; BFFCALLBACK 
System::Free $7 ; pszDisplayName 
System::Free $8 ; BROWSEINFO 
${If} $9 Z<> 0 
    System::Call 'SHELL32::SHGetPathFromIDList(p r9, t.s)i' 
    System::Call 'OLE32::CoTaskMemFree(p r9)' 
${Else} 
    Push "" ; Error/cancel, return empty string 
${EndIf} 
System::Store L 
FunctionEnd 
!endif 
!macro BrowseForValidatedFolder HeadingText ValidatorFuncName VarResult 
GetFunctionAddress ${VarResult} ${ValidatorFuncName} 
Push ${VarResult} 
Push "${HeadingText}" 
Call BrowseForValidatedFolder 
Pop ${VarResult} 
!macroend 

Section 
!insertmacro BrowseForValidatedFolder "Choose a location for blah blah" MyFolderValidator $0 
${If} $0 != "" 
    MessageBox mb_ok "Result: $0" 
${Else} 
    MessageBox mb_iconstop "User cancelled" 
${EndIf} 
SectionEnd 
相關問題