Results 1 to 13 of 13

Thread: DLL to ImageList

  1. #1

    Thread Starter
    Junior Member xuralarux's Avatar
    Join Date
    Oct 2001
    Location
    I'm somewhere where I don't know where I am!
    Posts
    20

    Question DLL to ImageList

    Here's what I want to do. On form_load(), I want to load the folder/drive/etc icons in the shell32.dll into an imagelist for use in a treeview. I was thinking that LoadIcon would be the API function to use, but I don't know with which lib to declare that, nor what variables to pass to it. Any thoughts on how my goal might be accomplished?
    Before you say anything about this post...you're welcome.

    "I love animals... I eat them and wear their skin."

    "Why do those who know the least, know it the loudest?"

    "You live and learn. Or you don't live long."



    Which Colossal Death Robot Are You?

  2. #2
    Lively Member
    Join Date
    Mar 2001
    Location
    *.*
    Posts
    85
    try this


    Code:
    Private Declare Function ExtractIconEx Lib "shell32.dll" Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal nIconIndex As Long, phiconLarge As Long, phiconSmall As Long, ByVal nIcons As Long) As Long
    rivate Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
    Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long
    
    Private Sub Form_Load()
        mIcon As Long, Cnt As Long
        
        For Cnt = 0 To Me.ScaleWidth / 32
            ExtractIconEx "shell32.dll", Cnt, mIcon, ByVal 0&, 1
            DrawIcon Me.hdc, 32 * Cnt, 0, mIcon
            DestroyIcon mIcon
        Next Cnt
    End Sub
    Everytime

    "I'm not normally a religious man, but if you're up there, save me, Superman!" Homer Simpson

    Visit my site

  3. #3
    Megatron
    Guest
    Instead of drawing on the Form's DC, draw on a PictureBox's DC. Make sure the AutoRedraw is set to True, and you refresh the PictureBox after drawing on it.

    To add the drawn picture to the ImageList, use:
    Code:
    ImageList1.ListImages.Add , "key", Picture1.Image

  4. #4
    Lively Member
    Join Date
    Mar 2001
    Location
    *.*
    Posts
    85
    You could use the sub below to paint 1 icon to the passed picturebox

    Code:
    Public Sub ExtractLargeIcon(Pic As PictureBox)
        
        Dim mIcon As Long
        Dim Cnt As Long
    
        'could loop through all the icons if wanted
        ExtractIconEx "PATH TO DLL", Cnt, mIcon, 0, 1
        DrawIconEx Pic.hdc, 0, 0, mIcon, 0, 0, 0, 0, DI_NORMAL
        DestroyIcon mIcon
    End Sub
    Then add the image from that picture box to the imagelist

    Oh yeah for the ExtractIconEx call the parameters are:

    · lpszFile
    Pointer to a null-terminated string specifying the name of an executable file, DLL, or icon file.

    · nIconIndex
    Specifies the index of the icon to retrieve. If this value is 0, the function returns the handle of the first icon in the specified file. If this value is -1 and phIconLargeand phiconSmall are both NULL, the function returns the total number of icons in the specified file.

    · phiconLarge
    Pointer to an array of handles of large icons returned. This parameter can be NULL.

    · phiconSmall
    Pointer to an array of handles of small icons returned. This parameter can be NULL.

    · nIcons
    Specifies the count of the number of icons to extract.


    So... to get the small icons use:

    Code:
    Public Sub ExtractSmallIcon(WinHandle As Long, Pic As PictureBox)
        Dim mIcon As Long
        Dim Cnt As Long
    
        'could loop through all the icons if wanted
        ExtractIconEx "PATH TO DLL", Cnt, 0, mIcon, 1
        DrawIconEx Pic.hdc, 0, 0, mIcon, 0, 0, 0, 0, DI_NORMAL
        DestroyIcon mIcon
    End Sub
    Both the above examples just get the first icon, change the nIconIndex value to get others
    Everytime

    "I'm not normally a religious man, but if you're up there, save me, Superman!" Homer Simpson

    Visit my site

  5. #5
    Lively Member
    Join Date
    Mar 2001
    Location
    *.*
    Posts
    85
    Thanks Mega - forgot that bit - that's a real 'hair tearer'
    Everytime

    "I'm not normally a religious man, but if you're up there, save me, Superman!" Homer Simpson

    Visit my site

  6. #6
    Lively Member
    Join Date
    Mar 2001
    Location
    *.*
    Posts
    85
    sorry missed these constants off the last example

    Code:
    Const DI_MASK = &H1
    Const DI_IMAGE = &H2
    Const DI_NORMAL = DI_MASK Or DI_IMAGE
    Everytime

    "I'm not normally a religious man, but if you're up there, save me, Superman!" Homer Simpson

    Visit my site

  7. #7
    Megatron
    Guest
    Since you're not using DI_MASK or DI_IMAGE individually, you could just omit them, and create one constant.
    Code:
    Const DI_NORMAL = 3

  8. #8

    Thread Starter
    Junior Member xuralarux's Avatar
    Join Date
    Oct 2001
    Location
    I'm somewhere where I don't know where I am!
    Posts
    20
    From Everytime's Post:
    Code:
    Private Declare Function ExtractIconEx Lib "shell32.dll" Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal nIconIndex As Long, phiconLarge As Long, phiconSmall As Long, ByVal nIcons As Long) As Long
    rivate Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
    Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long
    
    Private Sub Form_Load()
        mIcon As Long, Cnt As Long
        
        For Cnt = 0 To Me.ScaleWidth / 32
            ExtractIconEx "shell32.dll", Cnt, mIcon, ByVal 0&, 1
            DrawIcon Me.hdc, 32 * Cnt, 0, mIcon
            DestroyIcon mIcon
        Next Cnt
    End Sub
    Is there a way of finding out how many icons are in the dll, so you can loop using that? Also, will I need the path to the dll, and if so, what are the specifics on the API's to get the Win and Win\Sys dirs? Finally, what should mIcon equal?
    Before you say anything about this post...you're welcome.

    "I love animals... I eat them and wear their skin."

    "Why do those who know the least, know it the loudest?"

    "You live and learn. Or you don't live long."



    Which Colossal Death Robot Are You?

  9. #9
    Lively Member
    Join Date
    Mar 2001
    Location
    *.*
    Posts
    85
    I'm not sure how to find how many icons there are - if you find out let me know. I will look over the weekend.

    you will need the path to the dll

    micon will be zero to start with as it is not set to anything

    check out API calls on the link here for calls you need. there are loads of resources out there as well.
    Everytime

    "I'm not normally a religious man, but if you're up there, save me, Superman!" Homer Simpson

    Visit my site

  10. #10
    Megatron
    Guest
    I'm not sure if this will work, but what if you made looped through the line: ExtractIconEx "shell32.dll", Cnt, mIcon, ByVal 0&, 1. As soon as mIcon = 0, then there are no more icons, thus the number of icons equals your counter variable - 1.

  11. #11
    Megatron
    Guest
    Hey, it worked. I've put it into a function to make it more organized.
    VB Code:
    1. Private Declare Function ExtractIconEx Lib "shell32.dll" Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal nIconIndex As Long, phiconLarge As Long, phiconSmall As Long, ByVal nIcons As Long) As Long
    2. Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long
    3.  
    4. 'sFile = The filename you want to search
    5. Private Function GetIconCount(ByVal sFile As String) As Integer
    6.     Dim hIcon As Long
    7.     Dim iCount As Integer
    8.    
    9.     Do
    10.         ExtractIconEx sFile, iCount, hIcon, ByVal 0&, 1
    11.         If hIcon = 0 Then
    12.             Exit Do
    13.         Else
    14.             DestroyIcon hIcon
    15.             iCount = iCount + 1
    16.         End If
    17.     Loop
    18.    
    19.     GetIconCount = iCount
    20. End Function
    21.  
    22. Private Sub Command1_Click()
    23.  
    24.     'Call the function
    25.     icons = GetIconCount("Shell32.dll")
    26.     MsgBox "There are: " & icons & " icons in this file"
    27.  
    28. End Sub

  12. #12
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    To find out the number of icons in the file:
    VB Code:
    1. Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
    2.    
    3.  
    4. Public Function GetNumberOfIcons(ByVal p_strFile As String) As Long
    5.     GetNumberOfIcons = ExtractIcon(0&, p_strFile, -1)
    6. End Function

  13. #13
    Megatron
    Guest
    This will loop through the DLL, and add it's icons to an ImageList (thanks to Serge who made things easier )

    VB Code:
    1. Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
    2. Private Declare Function ExtractIconEx Lib "shell32.dll" Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal nIconIndex As Long, phiconLarge As Long, phiconSmall As Long, ByVal nIcons As Long) As Long
    3. Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long
    4.  
    5. Private Sub Command1_Click()
    6.    
    7.     Dim hIcon As Long
    8.     Dim iCount As Integer
    9.    
    10.     For I = 0 To ExtractIconEx("Shell32.dll", -1, hIcon, ByVal 0&, 1)
    11.         ExtractIconEx "Shell32.dll", iCount, hIcon, ByVal 0&, 1
    12.         If hIcon <> 0 Then
    13.             DrawIcon Picture1.hdc, 0, 0, hIcon
    14.             DestroyIcon hIcon
    15.             Picture1.Refresh
    16.             ImageList1.ListImages.Add , "PIC" & iCount, Picture1.Image
    17.             Picture1.Cls
    18.             iCount = iCount + 1
    19.         End If
    20.     Next I
    21.    
    22. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width