在listview VB.NET中显示网络文件夹图标

时间:2016-02-09 03:56:56

标签: vb.net api listview shell32.dll

如何在列表视图中显示网络文件夹图标?在文件夹下有绿色管的那个,我的代码适用于文件和文件夹但是当通过网络访问其他计算机时,我看不到看起来像这个的网络文件夹。

enter image description here

我应该添加什么?

这是我的代码: 这是我在ListView

中显示图标的方式
Dim fPath As String = Form2.TextBox1.Text
Dim di = New DirectoryInfo(fPath)

  ' store imagelist index for known/found file types
  Dim exts As New Dictionary(Of String, Int32)

  If di.Exists = True Then
      Dim img As Image
      Dim lvi As ListViewItem
      For Each d In di.EnumerateDirectories("*.*", SearchOption.TopDirectoryOnly)
          lvi = New ListViewItem(d.Name)
          lvi.SubItems.Add("")
          lvi.SubItems.Add(d.CreationTime.Date)

          ListView1.Items.Add(lvi)

          img = NativeMethods.GetShellIcon(d.FullName)
          ImageList1.Images.Add(img)
          lvi.ImageIndex = ImageList1.Images.Count - 1
      Next

这就是我从shell32获取图标的方式。

Partial Public Class NativeMethods
Private Const MAX_PATH As Integer = 256
Private Const NAMESIZE As Integer = 80
Private Const SHGFI_ICON As Int32 = &H100
<StructLayout(LayoutKind.Sequential)>
    Private Structure SHFILEINFO
        Public hIcon As IntPtr
        Public iIcon As Integer
        Public dwAttributes As Integer
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)>
        Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=NAMESIZE)>
        Public szTypeName As String
    End Structure
    <DllImport("Shell32.dll")>
    Private Shared Function SHGetFileInfo(ByVal pszPath As String,
                                      ByVal dwFileAttributes As Integer,
                                      ByRef psfi As SHFILEINFO,
                                      ByVal cbFileInfo As Integer,
                                      ByVal uFlags As Integer) As IntPtr
    End Function
    <DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function DestroyIcon(ByVal hIcon As IntPtr) As Boolean
    End Function
    Public Shared Function GetShellIcon(ByVal path As String) As Bitmap
        Dim shfi As SHFILEINFO = New SHFILEINFO()
        Dim ret As IntPtr = SHGetFileInfo(path, 0, shfi, Marshal.SizeOf(shfi), SHGFI_ICON)
        If ret <> IntPtr.Zero Then
            Dim bmp As Bitmap = System.Drawing.Icon.FromHandle(shfi.hIcon).ToBitmap
            DestroyIcon(shfi.hIcon)
            Return bmp
        Else
            Return Nothing
        End If
    End Function
End Class

1 个答案:

答案 0 :(得分:2)

您可以将Shell32的图标作为小图像或大图像。正如Cody Gray在评论中指出的那样,&#34; imageres.dll&#34;中有更多图标(200+)。要通过索引获取这些内容,请将此方法添加到NativeMethods类:

<DllImport("shell32.dll", CharSet:=CharSet.Auto)>
Private Shared Function ExtractIconEx(szFileName As String,
                            nIconIndex As Integer,
                            ByRef phiconLarge As IntPtr,
                            ByRef phiconSmall As IntPtr,
                            nIcons As UInteger) As UInteger
End Function

Private Shared ImgResFile As String = "imageres.dll"
Private Shared ShellFile As String = "shell32.dll"

Friend Shared Function GetShellIconByIndex(ndx As Int32,
                         largeIcon As Boolean = False,
                         Optional FromShell As Boolean = True) As Bitmap
    Dim largeIco As IntPtr
    Dim smallIco As IntPtr
    Dim thisIco As IntPtr
    Dim ico As Icon
    Dim bmp As Bitmap = Nothing

    Dim targtFile = If(FromShell, ShellFile, ImgResFile)
    ExtractIconEx(targtFile, ndx, largeIco, smallIco, 1)

    Try
        If largeIcon Then
            ico = Icon.FromHandle(largeIco)
            thisIco = largeIco
        Else
            ico = Icon.FromHandle(smallIco)
            thisIco = smallIco
        End If
        bmp = ico.ToBitmap()
    Catch ex As Exception           ' swallow exception to return nothing
        ' really stupid index values can throw ArgumentException
        ' when the result is IntPtr.Zero
        ' Rather than test it, catch it an any other(s)
    Finally
        DestroyIcon(thisIco)
    End Try

    Return bmp
End Function

第一个参数是要获取的图标的索引,第二个参数表示您是想要大版本还是小版本,最后一个参数是从imageres.dllshell32.dll获取的可选标记。请注意,如果出现问题,该方法可能会导致Nothing

然后修改文件夹循环,以便在检测到网络驱动器时从shell32.dll获取管道文件夹映像(#275):

For Each d In di.EnumerateDirectories("*.*", SearchOption.TopDirectoryOnly)
    ...
    If IsNetworkFolder(d) Then
        ' get #275 as small image from Shell 
        img = NativeMethods.GetShellIconByIndex(275, False)
        If img Is Nothing Then
            ' ToDo: perhaps load a default image from Resources?
        End If
    Else
        img = NativeMethods.GetShellIcon(d.FullName)
        If img Is Nothing Then
            img = IconFromFile(d.FullName)
        End If
    End If
    '... add code 
Next

Private Function IsNetworkFolder(di As DirectoryInfo) As Boolean
    Dim drv As New DriveInfo(di.Root.Name)
    Return (drv.DriveType = DriveType.Network)
End Function

这使用辅助函数来确定文件夹是否已联网。如果是,则从DLL中获取特定文件夹图标,即#275。结果:

enter image description here

同一文件夹图像也在imageres.dll中作为#137(#68和#69与世界叠加层相似)。取而代之的是:

'  137 is the index, false for large icon, false to use imageres instead: 
img = NativeMethods.GetShellIconByIndex(137, False, False)

如果您想在代码中避免 Magic Numbers ,请使用常量或所用图标的枚举。你可以NativeMethods类中定义它们,但这是500项,你可能不记得它们在6个月后的含义:

Private Enum ShellIcons
    NetworkFolder1 = 275
    NetworkFolder2 = 103
    SharedFolder = 158
    AddNetworkFolder = 278
End Enum
...
img = NativeMethods.GetShellIconByIndex(ShellIcons.NetworkFolder1, False)

这会将shell32.dll中存储的图标及其索引设置为Listview设置为LargeIcon查看,以便您浏览它们:

Dim ndx As Int32 = 0
Dim img As Image = Nothing
Dim lvi As ListViewItem

Do
    ' change second Bool to False to get the ones in imageres.dll
    img = NativeMethods.GetShellIconByIndex(ndx, True, True)

    If img IsNot Nothing Then
        lvi = New ListViewItem(ndx.ToString)

        ImageList1.Images.Add(img)
        lvi.ImageIndex = ImageList1.Images.Count - 1
        myLV.Items.Add(lvi)
        ndx += 1
    Else
        Exit Do
    End If
Loop Until img Is Nothing

enter image description here