private static FileInfo InternalGetFileInfo(string name, bool linkOverlay, IconSize size) { NativeMethods.Shell32.SHFILEINFO shfi = new NativeMethods.Shell32.SHFILEINFO(); NativeMethods.Shell32.SHGFI flags = NativeMethods.Shell32.SHGFI.Icon | NativeMethods.Shell32.SHGFI.UseFileAttributes | NativeMethods.Shell32.SHGFI.DisplayName | NativeMethods.Shell32.SHGFI.TypeName; if (linkOverlay) { flags |= NativeMethods.Shell32.SHGFI.LinkOverlay; } /* Check the size specified for return. */ if (IconSize.Small == size) { flags |= NativeMethods.Shell32.SHGFI.SmallIcon; // include the small icon flag } else { flags |= NativeMethods.Shell32.SHGFI.LargeIcon; // include the large icon flag } NativeMethods.Shell32.SHGetFileInfo( name, NativeMethods.Shell32.FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi), flags); // Copy (clone) the returned icon to a new object, thus allowing us // to call DestroyIcon immediately System.Drawing.Icon icon = (System.Drawing.Icon) System.Drawing.Icon.FromHandle(shfi.hIcon).Clone(); NativeMethods.User32.DestroyIcon(shfi.hIcon); // Cleanup FileInfo fileInfo = new FileInfo { DisplayName = shfi.szDisplayName, TypeName = shfi.szTypeName }; if (IconSize.Small == size) { fileInfo.SmallIcon = icon.ToImageSource(); } else { fileInfo.LargeIcon = icon.ToImageSource(); } return(fileInfo); }
/// <summary> /// Get the icon for a hWnd /// </summary> /// <param name="hWnd"></param> /// <returns></returns> private static ImageSource GetIcon(IntPtr hWnd) { IntPtr ICON_SMALL = IntPtr.Zero; IntPtr ICON_BIG = new IntPtr(1); IntPtr ICON_SMALL2 = new IntPtr(2); IntPtr iconHandle = User32.SendMessage(hWnd, (int)WindowsMessages.WM_GETICON, ICON_SMALL2, IntPtr.Zero); if (iconHandle == IntPtr.Zero) { iconHandle = User32.SendMessage(hWnd, (int)WindowsMessages.WM_GETICON, ICON_SMALL, IntPtr.Zero); } if (iconHandle == IntPtr.Zero) { iconHandle = User32.GetClassLongWrapper(hWnd, (int)ClassLongIndex.GCL_HICONSM); } if (iconHandle == IntPtr.Zero) { iconHandle = User32.SendMessage(hWnd, (int)WindowsMessages.WM_GETICON, ICON_BIG, IntPtr.Zero); } if (iconHandle == IntPtr.Zero) { iconHandle = User32.GetClassLongWrapper(hWnd, (int)ClassLongIndex.GCL_HICON); } if (iconHandle == IntPtr.Zero) { return(null); } using (System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(iconHandle)) { return(icon.ToImageSource()); } }
public void GetMoreTree(RoutedEventArgs e) { TreeViewItem item = (TreeViewItem)e.OriginalSource; item.ClearValue(ItemsControl.ItemsSourceProperty); ObservableCollection <Element> observableCollection = new ObservableCollection <Element>(); Element fileInfo = (Element)item.DataContext; try { foreach (DirectoryInfo dir in new DirectoryInfo(fileInfo.FullName).GetDirectories().OrderBy(x => x.Name).ToArray()) { ShellObject shellFolder = ShellObject.FromParsingName(dir.FullName.ToString()); BitmapSource shellThumb = shellFolder.Thumbnail.SmallBitmapSource; Folder newItem = new Folder { Name = dir.ToString(), Icon = shellThumb, FullName = dir.FullName, }; try { newItem.CountOfFolders = new DirectoryInfo(dir.FullName).GetDirectories().Length.ToString(); } catch (UnauthorizedAccessException ex) { newItem.CountOfFolders = "Отказано в доступе"; } try { newItem.CountOfFiles = new DirectoryInfo(dir.FullName).GetFiles().Length.ToString(); } catch (UnauthorizedAccessException ex) { newItem.CountOfFiles = "Отказано в доступе"; } try { newItem.CreationTime = dir.CreationTime.ToString(CultureInfo.InvariantCulture); } catch (UnauthorizedAccessException ex) { } newItem.Children.Add(new Element { Name = "*", Icon = null, FullName = dir.FullName }); observableCollection.Add(newItem); } foreach (FileInfo file in new DirectoryInfo(fileInfo.FullName).GetFiles().OrderBy(x => x.Name).ToArray()) { System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.ExtractAssociatedIcon(file.FullName.ToString()); if (file.Extension.ToLower() == ".jpg" || file.Extension.ToLower() == ".jpeg" || file.Extension.ToLower() == ".bmp" || file.Extension.ToLower() == ".png") { observableCollection.Add(new CustomImage { Name = file.ToString(), Icon = icon.ToImageSource(), FullName = file.FullName.ToString(), Size = ByteSize.FromBytes(file.Length).ToString(), CreationTime = file.CreationTime.ToString(CultureInfo.InvariantCulture) }); } else { observableCollection.Add(new CustomFile { Name = file.ToString(), Icon = icon.ToImageSource(), FullName = file.FullName, Size = ByteSize.FromBytes(file.Length).ToString(), CreationTime = file.CreationTime.ToString(CultureInfo.InvariantCulture) }); } } } catch (UnauthorizedAccessException ex) { Folder newIFolder = new Folder() { Name = "Отказано в доступе" }; observableCollection.Add(newIFolder); } item.ItemsSource = observableCollection; }