c# - C#:Listview LargeIcon 视图:消除行之间的空间
问题描述
我正在为特定目的/应用程序构建一个自定义 ListView 控件,我必须在其中显示画廊。为此,我使用所有者绘制的过程在 ListView 中手动绘制图像。
我在 ListView 中的图像将是 128x128 像素,因此我将一个空白 ImageList 控件(图像尺寸为 128x128)作为图像列表分配给 ListView 以自动定义项目大小。
到目前为止,这对我来说很好。但我需要消除项目行之间的空间(如示例图像所示)。我的目标是使自定义列表视图看起来像一个图像网格。我不担心项目左右的空间,只需要去掉行之间的空间,使它看起来像一个连续的网格。
任何帮助表示赞赏。谢谢。
解决方案
切换到平铺视图并执行您自己的绘图可以避免行间距问题:
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);
}
结果:
推荐阅读
- html - 图像和标题之间不需要的空间
- google-cloud-monitoring - 监控 PubSub 的推送请求?
- c - 为什么我的 C 程序在迭代变量达到 2100 时停止?
- flutter - 如何在颤动中制作这样的自定义应用栏?
- python-3.x - 用 django 模型对象中的默认值替换所有等于 Null 的字段的更优雅的方法
- ios - 如何在 HStack 中始终位于中心的 Spacers 之间创建视图?
- apache-spark - Glue Crawler 可以爬取 deltalake 文件以在 aws 胶水目录中创建表吗?
- c# - Azure Active Directory: Bearer error="invalid_token", error_description="The signature is invalid"
- scala - IntelliJ 在创建 Scala 项目后在 build.sbt 中给出“无法解析符号”的错误
- python - 如何使用 python matplotlib 调整水平图表上的线宽