|
-
Jan 3rd, 2002, 03:57 PM
#1
Thread Starter
Junior Member
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?
-
Jan 3rd, 2002, 04:13 PM
#2
Lively Member
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
-
Jan 3rd, 2002, 04:18 PM
#3
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
-
Jan 3rd, 2002, 04:20 PM
#4
Lively Member
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
-
Jan 3rd, 2002, 04:22 PM
#5
Lively Member
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
-
Jan 3rd, 2002, 04:36 PM
#6
Lively Member
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
-
Jan 3rd, 2002, 05:40 PM
#7
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
-
Jan 3rd, 2002, 05:50 PM
#8
Thread Starter
Junior Member
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?
-
Jan 4th, 2002, 04:40 AM
#9
Lively Member
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
-
Jan 4th, 2002, 10:49 AM
#10
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.
-
Jan 4th, 2002, 11:05 AM
#11
Hey, it worked. I've put it into a function to make it more organized.
VB 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
Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long
'sFile = The filename you want to search
Private Function GetIconCount(ByVal sFile As String) As Integer
Dim hIcon As Long
Dim iCount As Integer
Do
ExtractIconEx sFile, iCount, hIcon, ByVal 0&, 1
If hIcon = 0 Then
Exit Do
Else
DestroyIcon hIcon
iCount = iCount + 1
End If
Loop
GetIconCount = iCount
End Function
Private Sub Command1_Click()
'Call the function
icons = GetIconCount("Shell32.dll")
MsgBox "There are: " & icons & " icons in this file"
End Sub
-
Jan 4th, 2002, 11:13 AM
#12
To find out the number of icons in the file:
VB Code:
Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Public Function GetNumberOfIcons(ByVal p_strFile As String) As Long
GetNumberOfIcons = ExtractIcon(0&, p_strFile, -1)
End Function
-
Jan 4th, 2002, 11:23 AM
#13
This will loop through the DLL, and add it's icons to an ImageList (thanks to Serge who made things easier )
VB Code:
Private 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 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
Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long
Private Sub Command1_Click()
Dim hIcon As Long
Dim iCount As Integer
For I = 0 To ExtractIconEx("Shell32.dll", -1, hIcon, ByVal 0&, 1)
ExtractIconEx "Shell32.dll", iCount, hIcon, ByVal 0&, 1
If hIcon <> 0 Then
DrawIcon Picture1.hdc, 0, 0, hIcon
DestroyIcon hIcon
Picture1.Refresh
ImageList1.ListImages.Add , "PIC" & iCount, Picture1.Image
Picture1.Cls
iCount = iCount + 1
End If
Next I
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|