Visual Basic
  Home arrow Visual Basic arrow Page 2 - 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  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
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 / 33
    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

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Finding Default App Icons With Visual Basic - How It's All Done


    (Page 2 of 5 )

    All the registry keys that we are going to use are stored under HKEY_CLASSES_ROOT. The registry stores them in this form:
    • Every file extension is given a key. This key will be exactly the same as the file extension including the dot (eg: HKEY_CLASSES_ROOT\.bmp).
    • This keys "default" value refers to an application's registry key.
    • Every application is given a key (or a group of keys if the program is associated with multiple file types). This is not necessarily the same as its "EXE name", but in this key, among other things, there is a value that contains the location of the default file icon for this file type. For example: a ".frm" file. You should recognize this as a VB form.
    This is what my registry has to say about a ".frm":



    Your registry may differ, but the format should still be the same. The important thing to notice is how we got from ".frm" to "C:\program files\Microsoft Visual Studio\VB98\vb6.exe,1"

    Finding the Default Icon
    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 add this code into a module and refer back to the subroutine that 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 raise an event)
    So let's 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 know that it 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 of Windows API's available here.

    Next, we need to declare the function and all of its private 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

    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


    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway