I'm building a custom ListView control for a specific purpose/application wherein I have to display gallery. For this I'm using owner drawn process to manually draw images in the ListView.
My images in ListView will be 128x128 pixels so I've assigned a blank ImageList control (with 128x128 image dimensions) as image list to ListView to automatically define the item size.
This works fine for me up till now. But I need to eliminate the space between the item rows (as shown in the example image). My goal is to make the custom listview look like an images grid. I'm not worried about the space on the left and right of items, just need to get rid of space between the rows so that it looks like a continuous grid.
Any help is appreciated. Thanks.
Switching to Tile View and performing your own drawing can avoid the row spacing issue:
listView1.TileSize = new Size(128, 128);
listView1.View = View.Tile;
listView1.OwnerDraw = true;
listView1.DrawItem += listView1_DrawItem;
and a simple drawing routine:
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) {
Color textColor = SystemColors.WindowText;
if (e.Item.Selected) {
if (listView1.Focused) {
textColor = SystemColors.HighlightText;
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
} else if (!listView1.HideSelection) {
textColor = SystemColors.ControlText;
e.Graphics.FillRectangle(SystemBrushes.Control, e.Bounds);
}
} else {
using (SolidBrush br = new SolidBrush(listView1.BackColor)) {
e.Graphics.FillRectangle(br, e.Bounds);
}
}
e.Graphics.DrawRectangle(Pens.Red, e.Bounds);
TextRenderer.DrawText(e.Graphics, e.Item.Text, listView1.Font, e.Bounds,
textColor, Color.Empty,
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
}
Result: