大佬教程收集整理的这篇文章主要介绍了wpf – 获取程序图标,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
public static Imagesource GetIcon(String fileName) { Icon icon = Icon.ExtractAssociateDicon(fileName); return System.Windows.Interop.Imaging.CreateBitmapsourceFromHIcon( icon.Handle,new Int32Rect(icon.Width,icon.Height),BitmapSizeOptions.FromEmptyOptions()); }
另一个选项,如果不想使用System.Drawing,则是从SHGetFileInfo API获取图标.更困难,也更灵活;例如,您可以根据扩展名获得不存在的文件的图标(当然,对于可执行文件,它不是非常有用,因为它将返回可执行文件的默认图标).
public static Imagesource GetIcon(String path,bool smallIcon,bool isDirectory) { // SHGFI_USEFILEATTRIBUTES takes the file name and attributes into account if it doesn't exist uint flags = SHGFi_iCON | SHGFI_USEFILEATTRIBUTES; if (smallIcon) flags |= SHGFI_smaLLICON; uint attributes = FILE_ATTRIBUTE_NORMAL; if (isDirectory) attributes |= FILE_ATTRIBUTE_DIRECTORY; SHFILEINFO shfi; if (0 != SHGetFileInfo( path,attributes,out shfi,(uint)R_313_11845@arshal.SizeOf(typeof(SHFILEINFO)),flags)) { return System.Windows.Interop.Imaging.CreateBitmapsourceFromHIcon( shfi.hIcon,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions()); } return null; } [StructLayout(LayoutKind.Sequential)] private struct SHFILEINFO { public intPtr hIcon; public int iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.byValTStr,SizeConst = 260)] public String szDisplayName; [MarshalAs(UnmanagedType.byValTStr,SizeConst = 80)] public String szTypename; } [DllImport("sHell32")] private static extern int SHGetFileInfo(String pszPath,uint dwFileAttributes,out SHFILEINFO psfi,uint cbFileInfo,uint flags); private const uint FILE_ATTRIBUTE_READONLY = 0x00000001; private const uint FILE_ATTRIBUTE_HIDDEN = 0x00000002; private const uint FILE_ATTRIBUTE_SYSTEM = 0x00000004; private const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010; private const uint FILE_ATTRIBUTE_ARCHIVE = 0x00000020; private const uint FILE_ATTRIBUTE_DEVICE = 0x00000040; private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080; private const uint FILE_ATTRIBUTE_TEMPORARY = 0x00000100; private const uint FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200; private const uint FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400; private const uint FILE_ATTRIBUTE_COMPRESSED = 0x00000800; private const uint FILE_ATTRIBUTE_OFFLINE = 0x00001000; private const uint FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000; private const uint FILE_ATTRIBUTE_ENCRYPTED = 0x00004000; private const uint FILE_ATTRIBUTE_VIRTUAL = 0x00010000; private const uint SHGFi_iCON = 0x000000100; // get icon private const uint SHGFI_DISPLAYNAME = 0x000000200; // get display name private const uint SHGFI_TYPename = 0x000000400; // get type name private const uint SHGFI_ATTRIBUTES = 0x000000800; // get attributes private const uint SHGFi_iCONLOCATION = 0x000001000; // get icon LOCATIOn private const uint SHGFI_EXETYPE = 0x000002000; // return exe type private const uint SHGFI_SYSICONINDEX = 0x000004000; // get system icon index private const uint SHGFI_LINKOVERLAY = 0x000008000; // put a link overlay on icon private const uint SHGFI_SELECTED = 0x000010000; // show icon in SELEcted state private const uint SHGFI_ATTR_SPECIFIED = 0x000020000; // get only specified attributes private const uint SHGFI_LARGEICON = 0x000000000; // get large icon private const uint SHGFI_smaLLICON = 0x000000001; // get small icon private const uint SHGFI_OPENICON = 0x000000002; // get open icon private const uint SHGFI_SHelLICONSIZE = 0x000000004; // get sHell size icon private const uint SHGFI_PIDL = 0x000000008; // pszPath is a pidl private const uint SHGFI_USEFILEATTRIBUTES = 0x000000010; // use passed dwFileAttribute
这种方法的另一个优点是它还可以获取目录的图标(请参阅代码中的上一编辑),这不可能与Icon.ExtractAssociateDicon
以上是大佬教程为你收集整理的wpf – 获取程序图标全部内容,希望文章能够帮你解决wpf – 获取程序图标所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。