Visual Basic
  Home arrow Visual Basic arrow Page 3 - Finding Default App Icons With Visual Basi...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC

Finding Default App Icons With Visual Basic
By: Phil Couling
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 34
    2002-12-27

    Table of Contents:
  • Finding Default App Icons With Visual Basic
  • How It's All Done
  • The Registry
  • The Complete Code
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Finding Default App Icons With Visual Basic - The Registry


    (Page 3 of 5 )

    Firstly, we need to convert the filename into just a file extension. That is, remove everything before the ".":

    TempFileName = Right(FileName, Len(FileName) - InStrRev(FileName, ".") + 1)

    Now we take a look at the registry and see if it has any record of this file type (it is, of course, possible that it doesn't):

    lngError = RegOpenKey(HKEY_CLASSES_ROOT, TempFileName, lngRegKeyHandle)
    If lngError Then GoTo No_Icon 'we do not even have a valid extension so lets NOT try to find an icon!
    lngStringLength = 260
    'Make space for the incoming string
    strProgramName = Space$(260)
    'Get the key value:
    lngError = RegQueryValueEx(lngRegKeyHandle, vbNullString, 0, 0, strProgramName, lngStringLength)
    If lngError Then
    'if there's an error then BIG TROUBLE so lets use the normal "windows" icon
    'the world may be about to end (or its just an error) but we'll clean up anyway
    lngError = RegCloseKey(lngRegKeyHandle)
    GoTo No_Icon
    End If
    'if this generates an error then we can't do anthing about it anyway
    lngError = RegCloseKey(lngRegKeyHandle)
    'Cut the name down to size
    strProgramName = Left(strProgramName, lngStringLength - 1)


    You will notice that even when I am not going to use it I still make "lngError" receive any errors. I have left this in because it's useful for debugging.

    You will notice that if any errors occur in this code I use the line "GoTo No_Icon". This is because even if we can't get a default icon for this file type, we will still want to display something, i.e. the default windows icon:



    Ok, now we have the key name of our second registry key (stored in the strProgramName variable). What we need to do now is get the location for the default icon. The value that we want is held inside the sub-key "DefaultIcon". So very similarly to the last piece of code, we need to get this value:

    'Use the value of the last key in the name of the next one (strProgramName )
    lngError = RegOpenKey(HKEY_CLASSES_ROOT, strProgramName & "\DefaultIcon", lngRegKeyHandle)
    'there is no icon for this extension so lets NOT try to load what doesn't exist!
    If lngError Then GoTo No_Icon
    'The rest is just the same as before
    lngStringLength = 260
    strDefaultIcon = Space$(260)
    lngError = RegQueryValueEx(lngRegKeyHandle, vbNullString, 0, 0, strDefaultIcon, lngStringLength)
    If lngError Then
    lngError = RegCloseKey(lngRegKeyHandle)
    GoTo No_Icon
    End If
    lngError = RegCloseKey(lngRegKeyHandle)
    strDefaultIcon = Trim$(Left(strDefaultIcon, lngStringLength - 1))


    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. But we just need to do one thing first. The default icon it stored in the form of: "C:\BlahBlah\Blah.ico, 42".

    We 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 draw the icon. 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


    The Contingency
    In case we can't find the default icon, we need to display the Windows icon. Here's how:

    No_Icon:
    'No icon could be found so we use the normal windows icon
    'This icon is held in shell32.dll in the system directory, Icon 0
    strDefaultIcon = Space(260)
    lngStringLength = GetSystemDirectory(strDefaultIcon, 260)
    strDefaultIcon = Left(strDefaultIcon, lngStringLength) & "SHELL32.DLL"
    lngIconNumber = 0
    GoTo Draw_Icon
    End Sub


    The Windows icon will be held in the file "SHELL32.DLL", which is in the Windows system folder. So, we get the system directory and use that (along with "SHELL32.DLL") for the filename and then jump back to the "Draw_Icon" portion of code.

    At this point I am just going to add an extra piece of code that will handle EXE's. This is because EXE's have no default icon but it's probably not best to just show the windows icon. So let's use the icon that Windows normally uses for EXE's:



    Add this near the begging of your code, under "TempFileName = Right..." and above "lngError = RegOpenKey..."

    If LCase(TempFileName) = ".exe" Then
    strDefaultIcon = Space(260)
    lngStringLength = GetSystemDirectory(strDefaultIcon, 260)
    strDefaultIcon = Left(strDefaultIcon, lngStringLength) & "SHELL32.DLL"
    lngIconNumber = 2
    GoTo Draw_Icon
    End If


    This code is the same code used for getting the Windows icon except that we now want icon 2 instead of 0.

    More Visual Basic Articles
    More By Phil Couling


     

    VISUAL BASIC ARTICLES

    - Developing an XML Web Service Using Visual S...
    - Creating an HTML File List with VB
    - Fun with Email: VB6, CDO, MAPI, and a Remote...
    - Extranet/Intranet Dictionary Cracker in VB
    - Finding Default App Icons With Visual Basic
    - Registry Fever With Visual Basic
    - Implementing An ADO Data Control With VB6
    - Printing With Visual Basic
    - MSMQ Part 1/2: Architecture and Simple Imple...
    - Magnifying The Desktop With Visual Basic
    - Sending Email With MAPI Components in Visual...
    - Two Person Chat With The Winsock Control And...
    - A Real-Time ActiveX News Control
    - Accessing the Windows API in Visual Basic







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT