2

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

在此处输入图像描述

我应该添加什么?

这是我的代码:这就是我在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
4

1 回答 1

3

Shell32您可以从小图像或大图像中获取该图标。正如 Cody Gray 在评论中指出的那样,“imageres.dll”中有更多图标(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.dllvs中获取的可选标志shell32.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。结果:

在此处输入图像描述

相同的文件夹图像也在imageres.dll#137 中(并且 #68 和 #69 与世界叠加层相似)。要从中获得:

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

如果您想在代码中避免使用幻数,请使用常量或使用的图标的枚举。你可以在课堂上定义它们NativeMethods,但那是 500 个项目,你可能在 6 个月后记不起它们的含义:

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

这将显示存储在其中的图标shell32.dll及其索引到一ListviewLargeIcon视图,以便您可以浏览它们:

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

在此处输入图像描述

于 2016-02-09T14:19:42.137 回答