在 ListView 的网格中显示 128x128 像素或更大的缩略图图标 [英] Displaying thumbnail icons 128x128 pixels or larger in a grid in ListView
问题描述
原始问题(请参阅下面的更新)
我有一个 WinForms 程序,它需要一个带有大图标(实际上是 128x128 或更大的缩略图)的可滚动图标控件,可以单击以突出显示或双击以执行某些操作.最好尽量减少浪费的空间(每个图标下方可能需要简短的文件名标题;如果文件名太长,我可以添加省略号).
顺便说一下,为了保持缩略图的适当纵横比,我必须制作自己的 128x128 位图,清除背景以匹配控件,并将这些图像居中: 更新: I have a WinForms program that needs a decent scrollable icon control with large icons (128x128 or larger thumbnails, really) that can be clicked to hilight or double clicked to perform some action. Preferably there would be minimal wasted space (short filename captions might be needed below each icon; if the filename is too long I can add an ellipsis). I tried using a ListView with LargeIcon (default .View) and the results are disappointing: Perhaps I am populating the control incorrectly? Code: I need large icons with little empty space, not large empty space with embarrassingly small icons. I found this tutorial about OwnerDraw but work from that basically amounts to number 3 or 4 above since that demo just shows how to spice up the rows in the details view. Adding the line before the for loop fixed the size problem but now the images are palette-ized to 8-bit (looks like system colors?) even though the debugger shows that the images are inserted into the ImageList as 24bpp System.Drawing.Bitmap's:
Along with adding the line next after setting ilist.ImageSize, I followed arbiter's advice and changed the spacing: The ListView control may not be perfect or have the defaults I'd expect (like a Spacing property) but I'm glad I could tame it, in the end: By the way, to maintain the proper aspect ratio for the thumbnails, I had to make my own 128x128 bitmaps, clear the background to match the control, and center those images:
For update:
这篇关于在 ListView 的网格中显示 128x128 像素或更大的缩略图图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
(来源:
(来源:
(来源:
(来源:updike.org) >public void CenterDrawImage(Bitmap target, Color background, Bitmap centerme){图形 g = Graphics.FromImage(target);g.清除(背景);int x = (target.Width - centerme.Width)/2;int y = (target.Height - centerme.Height)/2;g.DrawImage(centerme, x, y);g.Dispose();}
Original Question (see Update below)
(source: updike.org)
(source: updike.org) ImageList ilist = new ImageList();
this.listView.LargeImageList = ilist;
int i = 0;
foreach (GradorCacheFile gcf in gc.files)
{
Bitmap b = gcf.image128;
ilist.Images.Add(b);
ListViewItem lvi = new ListViewItem("text");
lvi.ImageIndex = i;
this.listView.Items.Add(lvi);
i++;
}
Update
ilist.ImageSize = new Size(128, 128);
(source: updike.org)
Update 2
ilist.ColorDepth = ColorDepth.Depth24Bit;
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public int MakeLong(short lowPart, short highPart)
{
return (int)(((ushort)lowPart) | (uint)(highPart << 16));
}
public void ListView_SetSpacing(ListView listview, short cx, short cy)
{
const int LVM_FIRST = 0x1000;
const int LVM_SETICONSPACING = LVM_FIRST + 53;
// http://msdn.microsoft.com/en-us/library/bb761176(VS.85).aspx
// minimum spacing = 4
SendMessage(listview.Handle, LVM_SETICONSPACING,
IntPtr.Zero, (IntPtr)MakeLong(cx, cy));
// http://msdn.microsoft.com/en-us/library/bb775085(VS.85).aspx
// DOESN'T WORK!
// can't find ListView_SetIconSpacing in dll comctl32.dll
//ListView_SetIconSpacing(listView.Handle, 5, 5);
}
///////////////////////////////////////////////////////////
ListView_SetSpacing(this.listView, 128 + 12, 128 + 4 + 20);
(source: updike.org) public void CenterDrawImage(Bitmap target, Color background, Bitmap centerme)
{
Graphics g = Graphics.FromImage(target);
g.Clear(background);
int x = (target.Width - centerme.Width) / 2;
int y = (target.Height - centerme.Height) / 2;
g.DrawImage(centerme, x, y);
g.Dispose();
}