Results 1 to 4 of 4

Thread: [RESOLVED] Listview and Drive Icon

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    57

    Resolved [RESOLVED] Listview and Drive Icon

    hi... vb forumers...

    i used this code for show drive:

    Code:
    Dim fso, a, DriveX, B
    Dim lstItm As ListItem
    Dim aneh As New FileSystemObject
    
    Sub ShowDrive()
        Set B = aneh.Drives   
        lstHDD.ListItems.Clear
        For Each DriveX In B
            If DriveX.DriveType = 2 Or DriveX.DriveType = 3 Then  'tell appart hdd
                With lstHDD
                    Set lstItm = .ListItems.Add(, , DriveX.Path & ":\" & DriveX.VolumeName)               
                End With
            End If
        Next
    End Sub
    and my question, how to get the drive icon and add this on on listview??


    this preview of my listview

    thanks b4
    Attached Images Attached Images  

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Listview and Drive Icon

    1. You'll need to use an ImageList control & associate it with the listview
    2. Upload the icon into the imagelist control
    3. When adding your listview, reference that icon (one of the parameters in the .ListItems.Add method)

    To get icons related to drives, you might opt for the SHFileInfo API. Searching this forum for the following should return even more examples: SHFileInfo
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Listview and Drive Icon

    A demo of LaVolpe's comments:
    vb Code:
    1. Option Explicit
    2. Private Const MAX_PATH As Long = 260
    3. Private Const SHGFI_DISPLAYNAME        As Long = &H200
    4. Private Const SHGFI_EXETYPE            As Long = &H2000
    5. Private Const SHGFI_SYSICONINDEX       As Long = &H4000 ' System icon index
    6. Private Const SHGFI_SHELLICONSIZE      As Long = &H4
    7. Private Const SHGFI_TYPENAME           As Long = &H400
    8. Private Const SHGFI_LARGEICON           As Long = &H0 ' Large icon
    9. Private Const SHGFI_SMALLICON           As Long = &H1 ' Small icon
    10. Private Const BASIC_SHGFI_FLAGS         As Long = SHGFI_TYPENAME Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX Or SHGFI_DISPLAYNAME
    11. Private Const ILD_TRANSPARENT           As Long = &H1 ' Display transparent
    12.  
    13. Private Type SHFILEINFO
    14.  hIcon As Long
    15.  iIcon As Long
    16.  dwAttributes As Long
    17.  szDisplayName As String * MAX_PATH
    18.  szTypeName As String * 80
    19. End Type
    20.  
    21. Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbSizeFileInfo As Long, ByVal uFlags As Long) As Long
    22. Private Declare Function ImageList_Draw Lib "comctl32.dll" (ByVal himl As Long, ByVal i As Long, ByVal hdcDst As Long, ByVal X As Long, ByVal Y As Long, ByVal fStyle As Long) As Long
    23.  
    24. Private Sub Form_Load()
    25.  Dim Dr() As String
    26.  Dim i As Long
    27.  'my drives - use your own array of drives *******************
    28.  Dr = Split("C:\,D:\,E:\,H:\", ",")
    29.  With Picture1
    30.   .ScaleMode = vbPixels
    31.   .AutoRedraw = True
    32.   .Width = 240
    33.   .Height = 240 '16x16 pixels
    34.   .BorderStyle = 0
    35.   .Appearance = 0
    36.   .Visible = False 'change if desired
    37.  End With
    38.  With ListView1
    39.   .View = lvwReport
    40.   .ColumnHeaders.Add , , "Drives"
    41.  End With
    42.  'Load the imagelist.  Note, you can load
    43.  ' images and the LV in one loop if you
    44.  ' assign a dummy 16x16 pic at design time
    45.  ' Otherwise, you can't assign the ImageList
    46.  ' to the LV when it has no images assigned
    47.  For i = 0 To UBound(Dr)
    48.   Picture1.Cls
    49.   RenderIcon Dr(i), Picture1.hDC, 0, 0, False
    50.   ImageList1.ListImages.Add , Dr(i), Picture1.Image
    51.  Next
    52.  ListView1.SmallIcons = ImageList1
    53.  For i = 0 To UBound(Dr)
    54.   With ListView1.ListItems.Add
    55.    .Text = Dr(i)
    56.    .SmallIcon = Dr(i)
    57.   End With
    58.  Next
    59. End Sub
    60.  
    61. Public Sub RenderIcon(ByVal Path As String, ByVal PicHdc As Long, ByVal X As Long, ByVal Y As Long, Optional ByVal LargeIcon As Boolean = False)
    62.  Dim hImg As Long, Flags As Long
    63.  Dim SFO As SHFILEINFO
    64.  Flags = BASIC_SHGFI_FLAGS
    65.  If LargeIcon Then
    66.   Flags = Flags Or SHGFI_LARGEICON
    67.  Else
    68.   Flags = Flags Or SHGFI_SMALLICON
    69.  End If
    70.  hImg = SHGetFileInfo(Path, ByVal 0&, SFO, Len(SFO), Flags)
    71.  ImageList_Draw hImg, SFO.iIcon, PicHdc, X, Y, ILD_TRANSPARENT
    72. End Sub
    Last edited by Hack; Sep 27th, 2011 at 11:37 AM. Reason: Added Highlight Tags

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    57

    Talking Re: Listview and Drive Icon

    Thanks......... Thanks......... Thanks......... LaVolpe


    Thanks......... Thanks......... Thanks......... VBClassicRock



Tags for this Thread

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