GetAssociatedIcon

 

Description

Retrieves an icon associated with a file. The icon is extracted from the file itself or from an executable file associated with that file.

The icon (image) can be assigned to the Picture, DisabledPicture or DownPicture property of other controls (like commandbuttons) or can be added to an ImageList control.

Syntax

[Set oPic =] ODlg.GetAssociatedIcon ( File, SmallICon, BackColor )

Parameters

Part

Description

Set oPic =

An object variable or picture property you want to assign the icon to.

oDlg

A reference to the WshDialog.Kit automation object (as returned by the Wscript or VBScript CreateObject methods)

File

The name of the file for which an icon is desired. Both absolute and relative paths are valid.

SmallIcon

(optional) Boolean indicating whether to retrieve a large or small icon.
The default is False.

BackColor

(optional) The background color to be used when drawing the icon. Normally you would specify the background color of the control on which you want the icon to be drawn. The default is &H8000000F (which is the standard Windows Button Face color).

Example

Option Explicit

Const vbModal = 1

Dim oShell, oFso, oDlg, oFrm

'Create the Shell object and FileSystemObject
Set oShell = Wscript.CreateObject("WScript.Shell")
Set oFso = WScript.CreateObject("Scripting.FileSystemObject")

'Create the WshDialog.Kit object and store a reference in oDlg
Set oDlg = WScript.CreateObject("WshDialog.Kit", "oDlg_")

'Call BuildForm to build the form and store a reference in oFrm
Set oFrm = BuildForm

'Show the form
oFrm.Show vbModal

'-------------------------------------------------------------------------------
' The BuildForm function builds the form and returns a reference to it
'-------------------------------------------------------------------------------

Function BuildForm

    Dim X, oFrm, oCtl, oItem, oSubItem
    Dim oFolder, oFiles, oFile, sAttributes, sIconLibrary

    
'Add a form and store a reference to it in the variable oFrm
    Set oFrm = oDlg.NewForm("Sample")

    
'Set the form's caption
    oFrm.Caption = "Testing the Listview control with the GetAssociatedIcon... methods"

    
'Add a label to show the current path
    Set oCtl = oFrm.NewLabel("PATH", 130, 150, 8940, 300, "")
    oCtl.Borderstyle = 1

    
'Add an imagelist control (for small icons)
    oFrm.NewImageList "ILS", 16, 16

    
'Add a listview control with 5 columns
    Set oCtl = oFrm.NewListView("LVW1", 100, 500, 9000, 5000)
    oCtl.Columnheaders.Add , "#1", "Name", oCtl.Width * 0.35
    Set oItem = oCtl.Columnheaders.Add(, "#2", "Size", oCtl.Width * 0.10)
    oItem.Alignment = 1    
'Right-align
    oCtl.Columnheaders.Add , "#3", "Type", oCtl.Width * 0.25
    oCtl.Columnheaders.Add , "#4", "Date", oCtl.Width * 0.15
    oCtl.Columnheaders.Add , "#5", "Attributes", oCtl.Width * 0.15

    oCtl.Sorted = True    
'Enable sorting
    oCtl.SortOrder = 0    
'Ascending order
    oCtl.SortKey = 0      
'Sort on first column (text property)
    oCtl.View = 3           'Use report view

    
'Add another label
    oFrm.NewLabel "INFO", 130, 5600, 4000, 300, "Double-click a file to open it ..."

    
'Automatically size the form to the controls placed on it
    oFrm.AutoSize

    
'Show the form in the taskbar
    oFrm.TaskBar = True

    
'Enable event handling (callback) for this form
    oFrm.CallBack = True

    
'Return the form object
    Set BuildForm = oFrm

End Function

'-------------------------------------------------------------------------------
' oDlg_ClickHandler handles the events sent by the controls
'-------------------------------------------------------------------------------

Sub oDlg_ClickHandler(sForm, sControl)

    Dim oFrm, oCtl, sFolder, sProg, sFile, oFile, sPrompt

    Set oFrm = oDlg.Frm(sForm)
    Set oCtl = oFrm.Ctl(sControl)

    sFolder = "C:\"

    Select Case UCase(sControl)
    Case "*ACTIVATE"
        
'Populate the listview with the files in sFolder
        ListFolder oFrm.Ctl("LVW1"), oFrm.Ctl("ILS"), sFolder
    Case "LVW1"
        
'Get the program associated with the selected file
        sFile = sFolder & oCtl.SelectedItem
        Set oFile = oFso.GetFile(sFile)
        SProg =
oDlg.GetAssociatedProgram(sFile)
        If sProg = "" Then
            MsgBox "No program associated with " & sFile
        ElseIf Ucase(sProg) = Ucase(sFile) Or Ucase(sProg) = Ucase(oFile.ShortPath) Then
            
'The associated program is the selected file itself, so just run it
            If vbYes = MsgBox("Run " & sFile & " ?", vbYesNo, "Run Program") Then
                sFile = Chr(34) & sFile & Chr(34)
                oShell.Run sFile, 1, False
            End If
        Else
            
'Run the associated program with the selected file
            sPrompt = "Run " & sProg & vbCrLf & "with file " & sFile & " ?"
            If vbYes = MsgBox(sPrompt, vbYesNo, "Run Associated Program") Then
                sProg = Chr(34) & sProg & Chr(34)
                sFile = Chr(34) & sFile & Chr(34)
                oShell.Run sProg & " " & sFile, 1, False
            End If
        End If
    Case "*CLOSE"
        
'Close button chosen
        oFrm.Hide
    Case Else
        
'Ignore all other events. Do NOT use oFrm.Hide here, or any event
        'not handled above will dismiss the form
    End Select

End Sub

'-------------------------------------------------------------------------------
' Populate the listview
'-------------------------------------------------------------------------------
Sub ListFolder(oListView, oImageListSmall, sFolder)

    Dim oFolder, oFiles, oFile, oItem, sAttributes
    Dim sIconKey, oSmallIcon, nSmallIndex

    Set oFolder = oFso.GetFolder(sFolder)
    Set oFiles = oFolder.Files

    
'Show the pathname
    oFrm.Ctl("PATH").Caption = " " & sFolder

    
'Clear the listview first
    oListView.ListItems.Clear
    
    For Each oFile in oFiles
        
'----------------------------------------------------------------------------------
        'Get the small icon associated with the file
        Set oSmallIcon = oDlg.GetAssociatedIcon(oFile.Path, True, oListView.BackColor)
        
'Return a 'unique' key for the icon associated with the file
        sIconKey = oDlg.GetAssociatedIconKey(oFile.Path)
        
'Add the icon to the imagelist
        nSmallIndex = oFrm.NewImageListPicture(oImageListSmall, oSmallIcon, sIconKey)
        
'Associate the imagelist with the listview's Smallicons property
        oListView.SmallIcons = oImageListSmall
        
'Add a listitem to the listview
        Set oItem = oListView.ListItems.Add(, , oFso.GetFilename(oFile.Path), ,nSmallIndex)
        
'----------------------------------------------------------------------------------
        
'The entire section above can be replaced with a single call to NewListViewFileItem :
        'Set oItem = oFrm.NewListViewFileItem(oListView, oFile.Path, oImageListSmall)
        '----------------------------------------------------------------------------------
        
'Fill the listitem's subitems
        oItem.SubItems(1) = oFile.Size
        oItem.SubItems(2) = oFile.Type
        oItem.SubItems(3) = oFile.DateLastModified
        sAttributes = ""
        If oFile.Attributes And 2 Then sAttributes = "H"
        If oFile.Attributes And 4 Then sAttributes = sAttributes & "S"
        If oFile.Attributes And 1 Then sAttributes = sAttributes & "R"
        If oFile.Attributes And 32 Then sAttributes = sAttributes & "A"
        oItem.SubItems(4) = sAttributes
    Next

End Sub