Icon Auswählen-Dialog

  • Hi!
    Da der windowseigene Auswahldialog in AutoIt Fehler hat und es bei Leerzeichen zum abstürzen bringt, habe ich hier mal einen IconChooser geschrieben.
    (Die Grundlage war mal EnumIcons v2 von Lazycat, aber ich hab das meiste geändert:

    Neue Version im GuiMessageLoop Modus ohne globale Konstanten :)

    Spoiler anzeigen
    [autoit]

    #include<WindowsConstants.au3>
    #include<ButtonConstants.au3>
    #include<EditConstants.au3>
    #include<GUIConstantsEx.au3>
    #include<StaticConstants.au3>
    ;===============================================================================
    ;
    ; Description: Show all icons in the given file and choose one.
    ; Requirement(s): Autoit v3.2.8.1+
    ; Author(s): Prog@ndy (some Functions and Gui-Style by YDY (Lazycat) (in enumicons_v2))
    ; Version: 1.0
    ; Date: 02.11.2007
    ; Parameters: $fFileName [Optional: Filename]
    ; $asNegative [Optional] negative numbers starting with -1 ( TRUE, default)
    ; or positive numbers starting with 0 ( False )
    ; How to Call: $ret = _ChooseIcon( [Optional: Filename] )
    ; Return Values: Array:
    ; [0] = FileName
    ; [1] = Icon Number
    ;
    ; @error: 0 - Success
    ; 1 - Cancel
    ; 2 - Unknown error
    ;===============================================================================
    #cs Example
    $ret = _ChooseIcon()
    MsgBox(0,"Icon","Pfad: " & $ret[0] & @CRLF & "icon-Nr.: " & $ret[1])
    #ce

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _ChooseIcon($fFileName = "",$asNegative=1)

    [/autoit] [autoit][/autoit] [autoit]

    Local $_CI_IconCount, $_CI_FileName, $_CI_SelectedIconName, $_CI_IconRadios[30], $_CI_Labels[30],$_CI_NewFile
    Local $_CI_oldEventMode = Opt("GuiOnEventMode",0)
    Local $_CI_StartIndex = 1
    Local $arTemp[2], $error=2
    If FileExists($fFileName) Then
    Local $_CI_FileName = $fFileName
    Else
    Local $_CI_FileName = @SystemDir & "\shell32.dll"
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Local $_CI_hGui=GUICreate("Choose Icon:", 385, 450, -1, -1, -1, $WS_EX_ACCEPTFILES)
    ;GUISetOnEvent($GUI_EVENT_CLOSE,"_CI_CloseFunc")
    GUICtrlCreateGroup("", 5, 1, 375, 40)
    GUICtrlCreateGroup("", 5, 50, 375, 350)
    Local $_CI_hFile = GUICtrlCreateInput($_CI_FileName, 12, 15, 325, 16, -1, $WS_EX_STATICEDGE)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    GUICtrlSetTip(-1, "You can drop files from shell here...")
    Local $_CI_hStatus = GUICtrlCreateInput("", 90, 49, 200, 16, $ES_READONLY, $WS_EX_STATICEDGE)
    Local $_CI_hFileSel = GUICtrlCreateButton("...", 345, 14, 26, 18)
    ;GUICtrlSetOnEvent(-1,"_CI_LoadButton")
    Local $_CI_Next = GUICtrlCreateButton("&Weiter -|>",310,410)
    ;GUICtrlSetOnEvent(-1,"_CI_Next")
    Local $_CI_Back = GUICtrlCreateButton("<|- &Zurück",20,410)
    ;GUICtrlSetOnEvent(-1,"_CI_Back")
    Local $_CI_OK = GUICtrlCreateButton("Icon wählen:",120,410)
    ;GUICtrlSetOnEvent(-1,"_CI_CloseFunc")
    GUICtrlSetState(-1,$GUI_DISABLE)
    Local $_CI_Icon = GUICtrlCreateIcon("",0,190,404,32,32)

    For $iCntRow = 0 to 4
    For $iCntCol = 0 to 5
    $iCurIndex = $iCntRow * 6 + $iCntCol
    $_CI_IconRadios[$iCurIndex] = GUICtrlCreateRadio("", 55 * $iCntCol + 25, 65 * $iCntRow + 75, 45, 45,$BS_ICON+$BS_PUSHLIKE)
    ;~ GUICtrlSetImage(-1,@SystemDir & "\shell32.dll",-2)
    ;$ahIcons[$iCurIndex] = GUICtrlCreateIcon($glFilename, 0, 60 * $iCntCol + 25, 70 * $iCntRow + 80)
    ;GUICtrlSetOnEvent(-1,"_CI_SelectedIcon")
    $_CI_Labels[$iCurIndex] = GUICtrlCreateLabel("1", 55 * $iCntCol+12, 65 * $iCntRow + 120, 58, 20, $SS_CENTER)
    GUICtrlSetBkColor(-1,$GUI_BKCOLOR_TRANSPARENT )
    Next
    Next

    _CI_NewFileLoad($_CI_FileName,$_CI_IconCount, $_CI_hFile)
    _CI_Update($_CI_IconRadios, $_CI_Labels, $_CI_hStatus, $_CI_StartIndex, $_CI_IconCount, $_CI_FileName, $_CI_SelectedIconName)
    GUISetState () ; will display an dialog box with 1 checkbox
    ; Run the GUI until the dialog is closed
    While 1
    $gMsg = GUIGetMsg()
    Switch $gMsg
    Case $_CI_IconRadios[0] To $_CI_IconRadios[29]
    For $i = 0 To 29
    If GUICtrlRead($_CI_IconRadios[$i]) = $GUI_CHECKED Then
    $_CI_SelectedIconName = 1*GUICtrlRead($_CI_Labels[$i])
    GUICtrlSetImage($_CI_Icon,$_CI_FileName,$_CI_SelectedIconName)
    GUICtrlSetState($_CI_OK,$GUI_ENABLE)
    ExitLoop
    EndIf
    Next

    Case $_CI_hFileSel
    $_CI_NewFile = _CI_NewFileLoad( _
    FileOpenDialog("Select Icon file:", GUICtrlRead($_CI_hFile), "Icon-Files (*.exe;*.dll;*.ocx;*.icl;*.ico)") _
    ,$_CI_IconCount, $_CI_hFile)
    If Not ($_CI_NewFile == -1) Then
    $_CI_FileName = $_CI_NewFile
    GUICtrlSetState($_CI_Back,$GUI_DISABLE)
    GUICtrlSetState($_CI_OK,$GUI_DISABLE)
    $_CI_StartIndex = 1
    $_CI_SelectedIconName = 0
    GUICtrlSetImage($_CI_Icon,"")
    _CI_Update($_CI_IconRadios, $_CI_Labels, $_CI_hStatus, $_CI_StartIndex, $_CI_IconCount, $_CI_FileName, $_CI_SelectedIconName)
    EndIf
    Case $GUI_EVENT_DROPPED
    GUICtrlSetData($_CI_hFile,@GUI_DragFile)
    ContinueCase
    Case $_CI_hFile
    $_CI_NewFile = _CI_NewFileLoad( _
    GUICtrlRead($_CI_hFile) _
    ,$_CI_IconCount, $_CI_hFile)
    If Not ($_CI_NewFile == -1) Then
    $_CI_FileName = $_CI_NewFile
    GUICtrlSetState($_CI_Back,$GUI_DISABLE)
    GUICtrlSetState($_CI_OK,$GUI_DISABLE)
    $_CI_StartIndex = 1
    $_CI_SelectedIconName = 0
    GUICtrlSetImage($_CI_Icon,"")
    _CI_Update($_CI_IconRadios, $_CI_Labels, $_CI_hStatus, $_CI_StartIndex, $_CI_IconCount, $_CI_FileName, $_CI_SelectedIconName)
    EndIf
    Case $_CI_Back
    If $_CI_StartIndex - 30 >= 0 Then $_CI_StartIndex -= 30
    If $_CI_StartIndex - 30 < 0 Then
    GUICtrlSetState($_CI_Back,$GUI_DISABLE)
    EndIf
    GUICtrlSetState($_CI_Next,$GUI_ENABLE)
    _CI_Update($_CI_IconRadios, $_CI_Labels, $_CI_hStatus, $_CI_StartIndex, $_CI_IconCount, $_CI_FileName, $_CI_SelectedIconName)

    Case $_CI_Next
    If $_CI_StartIndex + 30 <= $_CI_IconCount Then $_CI_StartIndex += 30
    If $_CI_StartIndex + 30 > $_CI_IconCount Then
    GUICtrlSetState($_CI_Next,$GUI_DISABLE)
    EndIf
    GUICtrlSetState($_CI_Back,$GUI_ENABLE)
    _CI_Update($_CI_IconRadios, $_CI_Labels, $_CI_hStatus, $_CI_StartIndex, $_CI_IconCount, $_CI_FileName, $_CI_SelectedIconName)

    Case $GUI_EVENT_CLOSE
    $error = 1
    ExitLoop
    Case $_CI_OK
    If Not $asNegative Then $_CI_SelectedIconName = $_CI_SelectedIconName*-1-1
    Local $arTemp[2] = [$_CI_FileName,$_CI_SelectedIconName]
    $error = 0
    ExitLoop
    EndSwitch
    Wend

    GUIDelete($_CI_hGui)
    Opt("GuiOnEventMode",$_CI_oldEventMode)
    SetError($error)
    Return $arTemp
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _CI_Update(ByRef $_CI_IconRadios, ByRef $_CI_Labels, $_CI_hStatus, $_CI_StartIndex,ByRef $_CI_IconCount, $_CI_FileName, $_CI_SelectedIconName)
    For $iCntRow = 0 to 4
    For $iCntCol = 0 to 5
    $iCurIndex = $iCntRow * 6 + $iCntCol
    If ($iCurIndex + $_CI_StartIndex) > $_CI_IconCount Then
    GUICtrlSetState($_CI_IconRadios[$iCurIndex], $GUI_HIDE)
    GUICtrlSetState($_CI_Labels[$iCurIndex], $GUI_HIDE)
    Else
    GUICtrlSetState($_CI_IconRadios[$iCurIndex], $GUI_SHOW)
    GUICtrlSetState($_CI_Labels[$iCurIndex], $GUI_SHOW)
    GUICtrlSetState($_CI_IconRadios[$iCurIndex],$GUI_UNCHECKED)
    GUICtrlSetImage($_CI_IconRadios[$iCurIndex], $_CI_FileName, -($_CI_StartIndex + $iCurIndex))
    GUICtrlSetData($_CI_Labels[$iCurIndex], -($_CI_StartIndex + $iCurIndex))
    If -($_CI_StartIndex + $iCurIndex) = $_CI_SelectedIconName Then GUICtrlSetState($_CI_IconRadios[$iCurIndex],$GUI_CHECKED)
    EndIf
    Next
    Next
    GUICtrlSetData($_CI_hStatus, "Icons "& $_CI_StartIndex & "-"& _CI_MyMin(($_CI_StartIndex+ 29), $_CI_IconCount)& " von " & $_CI_IconCount _
    & " | Seite "& Ceiling(($_CI_StartIndex)/30)& "von " & Ceiling($_CI_IconCount/30))
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _CI_NewFileLoad($sTmpFile, ByRef $_CI_IconCount, $_CI_hFile)
    If Not FileExists($sTmpFile) Then Return -1
    If @error Then Return -1
    ;$glFileName = $sTmpFile
    GUICtrlSetData($_CI_hFile, $sTmpFile)
    $_CI_iStartIndex = 1
    $_CI_IconCount = _GetIconCount($sTmpFile)
    Return $sTmpFile
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; Function by YDY (Lazycat)
    Func _GetIconCount($sFilename)
    Local $iCount= DllCall("Shell32", "int", "ExtractIconEx", "str", $sFilename, "int", -1, "ptr", 0, "ptr", 0, "int", 1)
    If not @error Then Return $iCount[0]
    Return 0
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _CI_MyMin($1,$2)
    If Number($1) < Number($2) Then
    Return Number($1)
    Else
    Return Number($2)
    EndIf
    EndFunc

    [/autoit]


    EDit: Fixed: No Icon Index Rest on New File Load
    //Edit: On-Event Veriosn entfernt, an v 2.3.12.0 angepasst.
    //Edit: Fehler beim wechseln der Dll gefixt
    //Edit: optionally returns zero-based Icon Index.

    7 Mal editiert, zuletzt von progandy (23. September 2008 um 20:12)