ListView中的图像不匹配BackColor属性

塔可

总览

我目前正在为我的应用程序开发一个主题系统,允许用户选择浅色和深色主题(类似于Visual Studio)。整个过程是非常简单的,这是我如何改变BackColor的财产ListView目前控制(虽然我已经尝试了几个在线的解决方案未能如刚刚设置BackColorListViewListViewItem)。

分解

我有ImageList6张图片;所有这些都有透明的背景。每个图像都是不同颜色的玻璃球和阴影的图像。根据从数据库中提取的状态,将这六个图像之一分配给ListViewItem我的ListView(设置为Details查看)控件中的每个

只要将ListViewListViewItem分配给图像并将其BackColor属性设置为,一切都会很好Color.Control如果更改为其他任何颜色(绿色,灰色,蓝色,红色等);则图像的底色不匹配。它继续保留默认Color.Control颜色。

主题代码

public static void ApplyTheme(Form f) {
    foreach (Control c in f.Controls) {
        if (c is MenuStrip)
            ThemeMenu((MenuStrip)c);
        else {
            ApplyStyles(c);
            if (c.Controls != null || c.Controls.Count > 0)
                RecurseChildControls(c);
        }
    }
}
public static void RecurseChildControls(Control parent) {
    foreach (Control child in parent.Controls) {
        ApplyStyles(child)

        if (child.Controls != null || child.Controls.Count > 0)
            RecurseChildControls(child);
    }
}
public static void ApplyStyles(Control c) {
    if (c is Button) {
        Button b = (Button)c;
        b.FlatStyle = FlatStyle.Flat;
        b.FlatAppearance.BorderSize = 0;
    }
    if (c is RoundedPanel || c is PictureBox) {
        // Do nothing.
    } else {
        if (c is Label) {
            if (c.Parent is RoundedPanel) {
                // Do nothing.
            } else {
                c.BackColor = BackColor;
                c.ForeColor = ForeColor;
            }
        } else {
            c.BackColor = BackColor;
            c.ForeColor = ForeColor;
        }

        if (c is ListView) {
            ListView lv = (ListView)c;
            if (Style = Themes.Dark)
                lv.GridLines = false;

            foreach (ListViewItem lvi in lv.Items) {
                lvi.BackColor = BackColor;
                lvi.ForeColor = ForeColor;
            }
        }
    }
}

我尝试添加一个方法来更新控件及其所有对象BackColor属性,然后在事件上调用此方法(均不起作用);在更改a的属性后立即调用此方法代替的方法我什至尝试调用表单本身并重新绘制所有内容。ListViewListViewItemDrawItemDrawSubItemInvalidateListViewImageIndexListViewItemInvalidate

我真的可以在这方面使用一些指导,因为到目前为止其他所有工作都很好。这是让我转圈的唯一问题。我浏览过Google几次,但从未找到与图片BackColor包含ListView的图片不相关的任何结果ListViewItem也许我没有使用正确的词来执行搜索,或者也许我是第一个抱怨这个问题的人。谁知道。任何帮助表示赞赏。

注意

如果您觉得我没有任何必要的信息,或者在某些事情上我应该更加清楚,请随时发表评论并告诉我,以便我为以后的读者更新该文章的清晰度。

新尝试(失败)

  • 尝试ListView在设置ImageIndex属性之前(和之后)更改对象的背景颜色
  • 尝试使用OwnerDrawDrawItem事件绘制图像。
  • 尝试创建Bitmap具有指定背景色的原始图像,如下所示

尝试密码

// Bullet 2
e.Graphics.DrawImage(Image, Rectangle);

// Bullet 3
Rectangle r = new Rectangle(0, e.Bounds.Y, 16, 16);
Image I = imageList1.Images[e.Item.ImageIndex];
Bitmap b = new Bitmap(i.Width, i.Height);
using (Graphics g = Graphics.FromImage(b)) {
    g.Clear(Theme.BackColor);
    g.DrawImageUnscaledAndClipped(i, new Rectangle(Point.Empty, i.Size));
}
e.Graphics.DrawImage(b, r);

图片

所需结果

目前的结果,其中绿点为图像,红点是背景的问题,蓝框是ListViewItem

塔瓦

ListViewItem.BackColors在图片下方显示个人,您需要所有者绘画,否则ListView.BackColor将显示。但是您注意到图像的半透明部分中存在难看的伪影。

经过无数次测试,我相信ImageList是罪魁祸首。

似乎在应该是半透明像素的地方插入了灰色像素。完全透明的像素不受影响。

这独立于其属性而发生。

这是所有者ListView从中绘制一次图像的结果ImageList,一次绘制相同图像但直接从磁盘加载的结果。

在此处输入图片说明在此处输入图片说明

您肯定可以看到哪个。

图像是透明背景上的黄色斑点,具有半透明的光晕和两个半透明的孔。

ListView.DrawItem事件中的绘图代码相同:

e.DrawBackground();
e.DrawText();
Rectangle rect = new Rectangle(0, e.Bounds.Y, 32, 16);

Bitmap bmp1 = (Bitmap)imageList1.Images[e.Item.ImageIndex];
Bitmap bmp2 = (Bitmap)Bitmap.FromFile("filepath.png");

e.Graphics.DrawImage(bmp1_OR_bmp2, rect);

因此,如果您需要图像中的半透明性,则不能真正将它们存储在中ImageList

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Rapidminer 中的属性不匹配

对象数组中的属性不匹配

属性与主类中的属性类型不匹配

延续类中的属性属性与主类不匹配

ListView的OnClickListener _id与SQLite中的正确行_id不匹配

在Pillow和OpenCV中打开的图像不匹配

图像查看器中的颜色不匹配

(图像,遮罩)对在语义分割任务中不匹配

pygame中绘制的图像与给定的位置不匹配

ValueError:在PIL中混合图片时图像不匹配

将属性绑定到 Xamarin 中的 ListView,不更新 View

类型不匹配?“ ...缺少类型中的以下属性...”

AppVeyor 上 Aspnet 图像的 OSversion 和其他属性不匹配

图像分割:图像和标签 ID 不匹配以评估预测步骤中的结果

属性比较在tinyxpath中给出错误匹配和不匹配

控制器的长度属性与 TabBar 的 tabs 属性中存在的选项卡数量不匹配

如果属性与比较值不匹配,如何更改数组中对象属性的值?

JavaFX ListView中的图像

Listview 与内容不匹配以及如何定位

Android ListView宽度与父项不匹配

当listview字段与数据库中的字段不匹配时如何给出反馈“找不到”

在listView上显示图像而不滚动?

Xamarin表单图像大小不匹配

OpenCV 和 PIL 图像大小不匹配

模糊与图像视图的大小不匹配Swift

批处理图像的索引不匹配

xmlns 中的命名空间和 XML 文档中的 schemaLocation 属性不匹配

JavaFX:在不破坏选择模型的情况下在 ListView 中显示图像

CSS显示:单元格中的表格图像高度不匹配