Results 1 to 2 of 2

Thread: Write file, and cause Explorer ro show Icon

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Post

    My VB program writes files. When viewed via Windows Explorer, a default Icon displays with the File Name. VB or windows choses this Icon.

    I would like an Icon of my own to display. Is there a way for my VB program to cause this to happen?

    Thanx Governrcad@aol.com

    ------------------

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,176

    Post

    You can make an entry in the Registy for your files Extensions and associate an Icon Resource with the DefaultIcon Subkey, eg.
    .xyz : XYZ File
    XYZ File
    - DefaultIcon : "Project1.exe,0"


    Would use the Project Icon you set when you compiled your application.

    Here's some example code I've put together to associate an application with a particular file extension
    which also assigns the default Icon to use for that extension..
    Code:
    Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    
    Private Const HKEY_CLASSES_ROOT = &H80000000
    Private Const REG_CREATED_NEW_KEY = &H1
    Private Const REG_OPENED_EXISTING_KEY = &H2
    Private Const REG_SZ = 1
    
    Private Const STANDARD_RIGHTS_ALL = &H1F0000
    Private Const KEY_QUERY_VALUE = &H1
    Private Const KEY_SET_VALUE = &H2
    Private Const KEY_CREATE_SUB_KEY = &H4
    Private Const KEY_ENUMERATE_SUB_KEYS = &H8
    Private Const KEY_NOTIFY = &H10
    Private Const KEY_CREATE_LINK = &H20
    Private Const SYNCHRONIZE = &H100000
    
    Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
    
    Private Sub Command1_Click()
        Dim lRegKey As Long
        Dim lResult As Long
        Const sApp = "Notepad.exe %1"   'Application to Launch
        Const sExt = ".xyz"             'Extension to Associate
        Const sExtDesc = "XYZ File"     'Extension Description
        Const sIcon = "C:\MyApp.exe,0"  'Icon Resource for Extension
        
        'Create the Extension Entry
        Call RegCreateKeyEx(HKEY_CLASSES_ROOT, sExt & "\", 0&, "", 0&, KEY_ALL_ACCESS, ByVal 0&, lRegKey, lResult)
        If lResult = REG_CREATED_NEW_KEY Or lResult = REG_OPENED_EXISTING_KEY Then
            'Reg Opened/Created Successfully, set Reference to the Application Key
            lResult = RegSetValueEx(lRegKey, "", 0&, REG_SZ, ByVal sExtDesc, Len(sExtDesc))
            Call RegCloseKey(lRegKey)
            If lResult = 0 Then
                'Value set successfully, Assign Extension Icon
                Call RegCreateKeyEx(HKEY_CLASSES_ROOT, sExtDesc & "\DefaultIcon\", 0&, "", 0&, KEY_ALL_ACCESS, ByVal 0&, lRegKey, lResult)
                If lResult = REG_CREATED_NEW_KEY Or lResult = REG_OPENED_EXISTING_KEY Then
                    'Key Created, Set Icon Resource
                    lResult = RegSetValueEx(lRegKey, "", 0&, REG_SZ, ByVal sIcon, Len(sIcon))
                    Call RegCloseKey(lRegKey)
                End If
                'Create Associated App Key
                Call RegCreateKeyEx(HKEY_CLASSES_ROOT, sExtDesc & "\Shell\Open\Command\", 0&, "", 0&, KEY_ALL_ACCESS, ByVal 0&, lRegKey, lResult)
                If lResult = REG_CREATED_NEW_KEY Or lResult = REG_OPENED_EXISTING_KEY Then
                    'Reg Opened/Created Successfully..
                    lResult = RegSetValueEx(lRegKey, "", 0&, REG_SZ, ByVal sApp, Len(sApp))
                    If lResult = 0 Then
                        MsgBox "Extension Associated"
                    Else
                        MsgBox "Failed to Associate Extension"
                    End If
                End If
            End If
        End If
        Call RegCloseKey(lRegKey)
        
    End Sub

    ------------------
    Aaron Young
    Analyst Programmer
    aarony@redwingsoftware.com
    adyoung@win.bright.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width