File Extensions: Finding the default Icon

Drawing the Icon

So we now have the location of the default icon. There is just one problem with this, the icon may be stored in a .exe, .dll or .ico file. This is not as much of a problem as it may sound using the other group of three APIs that we declared at the beginning.

But we just need to do one thing first. The default icon it stored in the form of: “C:\BlahBlah\Blah.ico, 42”. We just need to split the number up from the location


    'Find the comma
    intN = InStrRev(strDefaultIcon, ",")
    'We MUST have an icon number and it will be after the ",": NO COMMA NO DEFAULT ICON
    If intN < 1 Then GoTo No_Icon
    'What number is after the comma
    lngIconNumber = Trim$(Right(strDefaultIcon, Len(strDefaultIcon) - intN))
    'We only want whatever is before the comma in the file name
    strDefaultIcon = Trim$(Left(strDefaultIcon, intN - 1))

So now we just need to do is draw the thing. This is where we need the “Device Context” (hDC) of our picture box:

Draw_Icon:
    'Extract the Icon
    lngIcon = ExtractIcon(App.hInstance, strDefaultIcon, lngIconNumber)
    'if 1 or 0 then after all that the Icon could not be retrieved
    If lngIcon = 1 Or lngIcon = 0 Then GoTo No_Icon
    lngError = DrawIcon(Picture_hDC, 0, 0, lngIcon) 'Draw the icon in the box
    'If that was unsucessful then we can't do anything about it now!
    lngError = DestroyIcon(lngIcon)
    'Again we can't correct any errors now
    Exit Sub

You might also like...

Comments

About the author

Phil Couling United Kingdom

Software developer, working in Derby, England. Currently I'm mostly developing in C++ with a combination of Borland, Visual Studio and MinGW.

Much of my work at the moment is with Databa...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”