public static void UseSystemImageList(ListView control) { IntPtr large, small; int x, y; if (control.LargeImageList == null) { control.LargeImageList = new ImageList(); } if (control.SmallImageList == null) { control.SmallImageList = new ImageList(); } Shell32.FileIconInit(true); if (!Shell32.Shell_GetImageLists(out large, out small)) { throw new Exception("Failed to get system image list"); } ComCtl32.ImageList_GetIconSize(large, out x, out y); control.LargeImageList.ImageSize = new Size(x, y); ComCtl32.ImageList_GetIconSize(small, out x, out y); control.SmallImageList.ImageSize = new Size(x, y); User32.SendMessage(control.Handle, MSG.LVM_SETIMAGELIST, (int)LVSIL.LVSIL_NORMAL, LargeImageList); User32.SendMessage(control.Handle, MSG.LVM_SETIMAGELIST, (int)LVSIL.LVSIL_SMALL, SmallImageList); }
/// <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"); } }