/// <summary> /// Initialises the specified parent folder. /// </summary> /// <param name="parentFolder">The parent folder.</param> public void Initialise(ShellItem parentFolder) { // If we're not double buffered, we want to be. if (!DoubleBuffered) { DoubleBuffered = true; } // Clear children. Items.Clear(); // Clear the map. itemsToFolders.Clear(); // Work out the types to show. var childTypes = ChildTypes.Files; if (ShowHiddenFilesAndFolders) { childTypes |= ChildTypes.Hidden; } if (ShowFolders) { childTypes |= ChildTypes.Folders; } // Go through the children. foreach (var child in parentFolder.GetChildren(childTypes)) { // Do we need to create the icon for the item? if (shellIconIndexesToCacheIconIndexes.ContainsKey(child.IconIndex) == false) { // Get the shell icon for the item. var smallIcon = Icon.FromHandle(ComCtl32.ImageList_GetIcon(ShellImageList.GetImageList(ShellImageListSize.Small), child.IconIndex, 0)); var largeIcon = Icon.FromHandle(ComCtl32.ImageList_GetIcon(ShellImageList.GetImageList(ShellImageListSize.Large), child.IconIndex, 0)); // Create it and add it. var mappedIndex = SmallImageList.Images.Count; SmallImageList.Images.Add(smallIcon); LargeImageList.Images.Add(largeIcon); shellIconIndexesToCacheIconIndexes[child.IconIndex] = mappedIndex; } // Create an item. var item = new ListViewItem { Text = child.DisplayName, ImageIndex = shellIconIndexesToCacheIconIndexes[child.IconIndex] }; // Map it. itemsToFolders[item] = child; // Insert the item. Items.Add(item); // Fire the event handler. FireOnShellItemAdded(item); } }
/// <summary> /// Sets the image list. /// </summary> /// <param name="this">The list view instance.</param> /// <param name="imageListType">Type of the image list.</param> /// <param name="imageListHandle">The image list handle.</param> public static void SetImageList(this ListView @this, ImageListType imageListType, IntPtr imageListHandle) { // If we're using the group header image list (not wrapped by .NET), // just go straight to the Win32 API - we'll be using it from there // anyway. if (imageListType == ImageListType.GroupHeader) { // Set the image list. User32.SendMessage(@this.Handle, LVM_SETIMAGELIST, (uint)imageListType, imageListHandle); // That's all we need to do. return; } // Get the image list details. int cx, cy; ComCtl32.ImageList_GetIconSize(imageListHandle, out cx, out cy); var imageCount = ComCtl32.ImageList_GetImageCount(imageListHandle); // This isn't actually enough for a Winforms list view - we actually need to create a copy of the // image list. var imageList = new ImageList(); imageList.ImageSize = new Size(cx, cy); for (int i = 0; i < imageCount; i++) { // Get the icon. var hIcon = ComCtl32.ImageList_GetIcon(imageListHandle, i, 0); // Create it. var icon = Icon.FromHandle(hIcon); // Add it. imageList.Images.Add(icon); } // Set the image list. switch (imageListType) { case ImageListType.Normal: @this.LargeImageList = imageList; break; case ImageListType.Small: @this.SmallImageList = imageList; break; case ImageListType.State: @this.StateImageList = imageList; break; default: throw new ArgumentOutOfRangeException("imageListType"); } }