Module pour choisir une icône (Windows)

Partagez votre expérience de PureBasic avec les autres utilisateurs.
El Papounet
Messages : 57
Inscription : mer. 09/juin/2010 23:47

Module pour choisir une icône (Windows)

Message par El Papounet »

J'ai essayé de faire ce module pour mes besoins. Quelques fonctions similaires existaient bien, mais aucune ne retourne le chemin du fichier dans lequel on sélectionne l'icône.

Je ne suis pas expert, mais ça semble fonctionner comme il faut, aussi bien compilé en Unicode ou non.

Code : Tout sélectionner

DeclareModule IconDlg

    Declare.b Pick(iconfile.s = "", iconindex = 0, hWnd.i = 0)

    Global.s icon
    Global.i index

EndDeclareModule
Module IconDlg

    Prototype.i PickIconDlg(hwnd.i, *pszIconPath, cchIconPath, *piIconIndex)

    Procedure.b Pick(iconfile.s = "", iconindex = 0, hWnd.i = 0)

        Protected.String library
        Protected.PickIconDlg PickIconDlg
        Protected.i idLib, result, szbuffer
        Protected *buffer

        library\s = iconfile
        index = iconindex
        idLib = OpenLibrary(#PB_Any, "Shell32.dll")
        If idLib
            PickIconDlg = GetFunction(idLib, "PickIconDlg")
            If library\s = ""
                library\s = Space(#MAX_PATH)
                GetWindowsDirectory_(library\s, #MAX_PATH)
                library\s  + "\system32\shell32.dll"
                index = 0

            EndIf

            szbuffer = #MAX_PATH + 1
            CompilerIf Not #PB_Compiler_Unicode: szbuffer * 2: CompilerEndIf

            *buffer = AllocateMemory(szbuffer)
            PokeS(*buffer, library\s, -1, #PB_Unicode)
            If PickIconDlg(0, *buffer, szbuffer, @index)
                icon = PeekS(*buffer, -1, #PB_Unicode)
                result = #True

            Else
                result = #False

            EndIf
            FreeMemory(*buffer)
            CloseLibrary(idLib)

        EndIf
        ProcedureReturn result

    EndProcedure

EndModule


;   IconDlg::Pick(iconfile.s, iconindex.i, hWnd.i)
;       iconfile    -> (Optionel - Shell32.dll par défaut)  La librairie, l'exécutable ou l'icône à ouvrir dans le dialogue
;       iconindex   -> (Optionel - 0 par défaut)            L'index de l'icône dans le fichier. Par défaut: 0
;       hWnd        -> (Optionel- 0 par défaut)             Le handle de la fenêtre de votre application si nécessaire. Par défaut: 0
;        Retour      -> #True si ok sinon #False
;
;   IconDlg::icon
;       Retour:     -> Le fichier dans lequel l'icône a été sélectionnée.
;
;    IconDlg::index
;        Retour:    -> L'index de l'icône dans le fichier dans lequel elle a été sélectionnée.

CompilerIf #PB_Compiler_IsMainFile
    If IconDlg::Pick("C:\Windows\System32\imageres.dll", 15)
        Debug "Fichier contenant l'icône: " + IconDlg::icon
        Debug "Index de l'icône: " + IconDlg::index

    EndIf

CompilerEndIf
Avatar de l’utilisateur
Micoute
Messages : 2522
Inscription : dim. 02/oct./2011 16:17
Localisation : 35520 La Mézière

Re: Module pour choisir une icône (Windows)

Message par Micoute »

Merci beaucoup pour ce partage, j'adore.
Microsoft Windows 10 Famille 64 bits : Carte mère : ASRock 970 Extreme3 R2.0 : Carte Graphique NVIDIA GeForce RTX 3080 : Processeur AMD FX 6300 6 cœurs 12 threads 3,50 GHz PB 5.73 PB 6.00 LTS (x64)
Un homme doit être poli, mais il doit aussi être libre !
Répondre