gpt4 book ai didi

C#:Listview LargeIcon View :消除行之间的空间

转载 作者:太空宇宙 更新时间:2023-11-03 14:52:13 25 4
gpt4 key购买 nike

我正在为特定目的/应用程序构建自定义 ListView 控件,其中我必须显示画廊。为此,我使用所有者绘制过程在 ListView 中手动绘制图像。

我在 ListView 中的图像将是 128x128 像素,因此我分配了一个空白 ImageList 控件(具有 128x128 图像尺寸)作为图像列表到 ListView 以自动定义项目大小。

enter image description here

到目前为止,这对我来说效果很好。但我需要消除项目行之间的空间(如示例图像所示)。我的目标是使自定义 ListView 看起来像图像网格。我不担心项目左侧和右侧的空间,只需要摆脱行之间的空间,使其看起来像一个连续的网格。

感谢任何帮助。谢谢。

最佳答案

切换到平铺 View 并执行您自己的绘图可以避免行间距问题:

listView1.TileSize = new Size(128, 128);
listView1.View = View.Tile;
listView1.OwnerDraw = true;
listView1.DrawItem += listView1_DrawItem;

和一个简单的绘图例程:

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);
}

结果:

enter image description here

关于C#:Listview LargeIcon View :消除行之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51269374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com