-- 作者:大红袍
-- 发布时间:2015/12/14 14:48:00
--
mark 提取icon
全局代码
Public Class GetSysicon \'\'\' <summary> \'\'\' 获得指定文件图标句柄 \'\'\' </summary> \'\'\' <param name="path">指定的文件名</param> \'\'\' <param name="fileattribute">文件属性</param> \'\'\' <param name="?">返回的文件信息</param> \'\'\' <param name="SizeFileInfo">sfinfo的比特值</param> \'\'\' <param name="flag">指明要返回的文件信息标识符</param> \'\'\' <returns>文件的图标句柄</returns> <DllImport("shell32.dll", EntryPoint := "SHGetFileInfo")> _ Public Shared Function SHGetFileInfo(path As String, fileattribute As UInteger, ByRef sfinfo As SHFILEINFO, SizeFileInfo As UInteger, flag As UInteger) As IntPtr End Function
\'\'\' <summary> \'\'\' 图标结构 \'\'\' </summary> \'自定义类型 <StructLayout(LayoutKind.Sequential)> _ Public Structure SHFILEINFO Public hIcon As IntPtr \'文件的图标句柄 Public iIcon As IntPtr \'图标的系统索引号 Public aattributes As UInteger \'文件的属性值 \'MarshalAs只是如何在托管代码和非托管代码间传送数据 <MarshalAs(UnmanagedType.ByValTStr, SizeConst := 260)> _ Public displayname As String \'文件的显示名 <MarshalAs(UnmanagedType.ByValTStr, SizeConst := 80)> _ Public typename As String \'文件的类型 End Structure
\'\'\' <summary> \'\'\' 清除图标 \'\'\' </summary> \'\'\' <param name="hIcon">图标句柄</param> \'\'\' <returns>返回非零表示成功,零表示失败</returns> <DllImport("user32.dll", EntryPoint := "DestroyIcon")> _ Public Shared Function DestroyIcon(hIcon As IntPtr) As Integer End Function
\'\'\' <summary> \'\'\' 获取指定路径的系统图标 \'\'\' </summary> \'\'\' <param name="path">路径</param> \'\'\' <param name="imglist">Imagelist对象保存图标</param> \'\'\' <param name="flag">表示是否为获取文件夹图标,true为获取文件夹图标,false为获取文件图标</param> Public Shared Function GetSysIcon(path__1 As String, flag As Boolean) As Icon Dim shfi As New SHFILEINFO() If flag Then Dim dir As New IO.DirectoryInfo(path__1) If dir.Name = "RECYCLER" OrElse dir.Name = "RECYCLED" OrElse dir.Name = "Recycled" OrElse dir.Name = "System Volume Information" Then \'windows系统文件夹 Else \'Marshal.SizeOf返回对象的非托管大小 SHGetFileInfo(path__1, CUInt(&H80), shfi, CUInt(Marshal.SizeOf(shfi)), CUInt(&H100 Or &H400)) \'取得icon和typename dim ico = DirectCast(Icon.FromHandle(shfi.hIcon).Clone(), Icon) DestroyIcon(shfi.hIcon) return ico End If Else Dim file As New FileInfo(path__1) Dim exten As String = io.Path.GetExtension(path__1).Substring(1, io.Path.GetExtension(path__1).Length - 1).ToLower() \'获取路径文件的扩展名,并去掉. If exten = "sys" OrElse exten = "ini" OrElse exten = "bin" OrElse exten = "log" OrElse exten = "com" OrElse exten = "bat" OrElse exten = "db" Then Else SHGetFileInfo(path__1, CUInt(&H80), shfi, CUInt(Marshal.SizeOf(shfi)), CUInt(&H100 Or &H400)) dim ico = DirectCast(Icon.FromHandle(shfi.hIcon).Clone(), Icon) DestroyIcon(shfi.hIcon) return ico End If End If End Function End Class
调用代码
Dim icon = GetSysicon.GetSysIcon( Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), True)
\'Dim fileStream As new System.IO.FileStream("d:\\test2.ico", System.IO.FileMode.Create) \'icon.Save(fileStream) \'fileStream.Close()
https://msdn.microsoft.com/zh-cn/library/system.environment.specialfolder.aspx
|