Library tutorials & articles
File Extensions: Finding the default Icon
Declares
We’re going to make a function which, when given a file name, will find the default icon for it’s type (by its extension) and then draw it onto an object. It needs two inputs, the file name and the “hDC” of the object that it is going to draw onto.
My advice to you would be to make this in a module and refer back to the subroutine we are about to write (GetDefaultIcon). You will also need (for testing purposes at least) a form containing
- A picture box (With AutoRedraw = true). You could draw onto most things but they must have a device context (hDC value at runtime).
- A text box (or some way of entering a file name)
- A command button (or something to generate an event)
So lets start by declaring every thing:
'For looking at registry keys
'To: Open key ready to look at
Private Declare Function RegOpenKey
Lib "advapi32.dll" Alias
"RegOpenKeyA" (ByVal hKey As
Long, ByVal lpSubKey As
String, phkResult As Long) As
Long
'To: Look at key
Private Declare Function
RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey
As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Any, lpcbData As Long) As Long
'To: Close the key when it's finished
with
Private Declare
Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const
KEY_READ = &H20019 'To allow us to READ the registery
keys
'For Drawing the icon
'To:
Retrieve the icon from the .EXE, .DLL or .ICO
Private Declare Function
ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst
As Long, ByVal lpszExeFileName
As String, ByVal nIconIndex
As Long) As Long
'To: Draw the icon into our picture box
Private
Declare Function DrawIcon Lib "user32.dll" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal hIcon As Long) As Long
'To: Clean
up after our selves (destroy the icon that "ExtractIcon" created)
Private Declare Function
DestroyIcon Lib "user32.dll" (ByVal hIcon As Long) As Long
'For Finding the System folder
Private Declare
Function GetSystemDirectory Lib "kernel32.dll"
Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
If none of this code means anything to you then don’t worry too much, just accept that what it is doing is adding seven extra functions to this program that aren’t normally in VB. If you are interested then there is an article written by James Crowley on the subject (Windows APIs) available here.
Now we need to declare the function and all its private/temporary variables:
Private Sub GetDefaultIcon(FileName As
String, Picture_hDC As Long )
Dim TempFileName
As String 'Never manipulate an input unless it doubles as an
output
Dim lngError As Long'For receiving error numbers
Dim
lngRegKeyHandle As Long'Stores the
"handle" of the registry key that is currently open
Dim
strProgramName As String'Stores the
contents of the first registry key
Dim
strDefaultIcon As String'Stores the
contents of the second registry key
Dim
lngStringLength As Long'Sets /
Returns the length of the output string
Dim
lngIconNumber As Long'Stores the icon
number within a file
Dim lngIcon As Long'Stores the "Icon Handle" for the default icon
Dim intN As
Integer 'For any temporary numbers
Related articles
Related discussion
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
-
Can you describe Above simple VB6 code?
by pramodmca09 (0 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
the icon may be in an exe file or dll file.
this is a veryyyyyyyy long way, and there are ready softwares available for it.
but it is discovered by me, so posting
in folder options, create a dummy file extension
click advanced,
change the icon of that file extension to desired icon; from dll or exe file.
open google talk (sounds funny naa)
now create a file with that extension
using file transfer option, send that file to someone
it is not needed that actual transfer shd take place, u only need to initiate the transfer & then u can abort the transfer.
now go to
%userprofile%\Local Settings\Temp\Google Talk\File Extensions
the icon file is seen there as a png image
open it with mspaint or similar software & save it as 256 color bitmap
change its extension to .ico from .bmp
& u r done
Thanks a lot for the help and the good job you're doing
Yves
I AM AN UNDERGRADUATE STUDYING COMPUTER PRORAMMING. i WILL NEED MORE EXPLANATION ON THIS TOPIC.
tOCHUKWU.
Absolutely brilliant! Works a treat.
THANKS VERY MUCH!!!
Looks like my reply is too large to fit in one post so I've put it here.
Hope this helps
Lovely coding couling! Its helped me alot.
I'm not too hot with API work though and have had trouble incorporating your two lots of code. I'm ultimately trying to display the associated file icons from a directory of scanned files from a database on a Listview (like Explorer), using a 16x16 picture box. 32x32 is no problem (in theoy) but I'm unable to get my head round the 16x16 bit.
Any help VERY much appreciated.
Really Very Nice
In the properties of the picturebox, set "AutoRedraw" to "True"
If you want to draw to other objects, you can either
1.
2. If your object has a “Picture” or “Image” property then you could use:
Object.Picture = Picture1.Image
Or
Object.Image = Picture1.Image
Hope this helps
Hi, this is great code! I just have one question...
When form minimize or is hidden behind another one, the drawn icon just dissapear, how can I make it persistant?? how can I convert it to a Picture Object I could manipulate to assign it to any other control???
Sorry this took a while
Ok, Small Icons:
These will be a little more complex, you will need to use two more windows APIs:
ExtractIconEx (instead of ExtractIconEx)
DrawIconEx (instead of DrawIcon)
These functions do basically the same thing, but they are capable of much more. ExtractIconEx can extract any number of Icons into two arrays (although in the example below I’ve only used two long variables “lngSmall” and “lngLarge”). DrawIconEx will allow you to draw the small icon as a 16 X 16 image instead of 32 X 32. This is just a small example of how to use the two functions, hopefully you can see how to alter the origional code. You will need:
two picture boxes (to receve the images)
an icon (in the code Ive used the path "C:\1.ico")
A command button (the subroutine is "command1_CLick()")
So for some code:
Private Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Long) As Long
Private Declare Function DrawIconEx Lib "user32.dll" (ByVal hDC As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long
Private Const DINORMAL = &H3 'Draw both the image mask in image data
Private Const DIDEFAULTSIZE = &H8 'Draw the icon at 32 X 32
Private Sub Command1Click()
Dim Retval As Long, lngLarge As Long, lngSmall As Long, hbrush As Long
' \/firstIcon \/Number of icons
Retval = ExtractIconEx("C:\1.ico", 0, lngLarge, lngSmall, 1)
Retval = DrawIconEx(Picture1.hDC, 0, 0, lngLarge, 0, 0, 0, 0, DINORMAL Or DIDEFAULTSIZE)
'note: DIDEFAULTSIZE tells windows to ignore the two parameters after "lngLarge".
Retval = DrawIconEx(Picture2.hDC, 0, 0, lngSmall, 16, 16, 0, 0, DINORMAL)
'"16,16 tells windows to draw the Icon 16 X 16 pixals
' left top Icon width height for drawing animated cursors brush type Flags
'Retval = DrawIconEx(Picture2.hDC, 0, 0, lngSmall, 16, 16, 0, 0, DINORMAL)
Retval = DestroyIcon(lngLarge) 'As in the article
Retval = DestroyIcon(lngSmall)
End Sub
To get ExatractIconEx to retrieve more than one Icon, replace lngLarge and lngSmall with arrays and set the last parameter “nIcons” to the number of icons to extract.
If this isn't clear just say so.
Fantastic tutorial. Especially as I've just started a project, this evening, that'll have file lists and icons - solved quite a head scratcher for me.
But I was wandering, is it a simple matter to use the small versions of the icons, as you get in explorer? If so, could someone tell me where to look for how
Tom.
This thread is for discussions of File Extensions: Finding the default Icon.