ListView中的图像不匹配BackColor属性

时间:2018-06-04 16:25:34

标签: c# winforms listview themes

概述

我目前正在为我的应用程序开发一个主题系统,允许用户选择Light and Dark主题(类似于visual studio)。整个过程非常简单,这就是我当前正在更改BackColor控件的ListView属性的方法(尽管我尝试了一些失败的内联解决方案,例如设置BackColor } ListViewListViewItem)。

击穿

我有ImageList,有6张图片;所有这些都具有透明背景。每个图像都是不同颜色和投影的玻璃球。我根据从数据库中提取的状态,将{6}中的一个图像分配给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控件及其所有ListView对象的ListViewItem属性,然后我在{{1}上调用了此方法}和DrawItem事件(既没有奏效);调用此方法代替DrawSubItem的{​​{1}}方法,在更改Invalidate的{​​{1}}属性后立即调用此方法;我甚至尝试在表单上调用ListView并重新绘制所有内容。

我真的可以在这个上使用一些指导,因为到目前为止其他一切都很好;这是让我旋转的唯一问题。我已多次浏览过Google,但从未发现任何与该图片相关的结果与ImageIndexListViewItem不相同Invalidate。也许我没有用正确的条款进行搜索,或者我可能是第一个抱怨这个问题的人;谁知道。任何帮助表示赞赏。

如果您觉得我遗漏了任何必要的信息,或者我对某些事情应该更清楚,请随时发表评论并告诉我,以便我可以为未来的读者更新帖子的清晰度。

新尝试(失败)

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

尝试代码

OwnerDraw

图片

Desired Result

绿色点为图像的Current Result,红点为背景问题,蓝色框为DrawItem

1 个答案:

答案 0 :(得分:1)

要在所有者绘制所需的图片下显示个人ListViewItem.BackColors,否则ListView.BackColor会显示。但是你在图像的半透明部分注意到了丑陋的文物。

经过多次测试后,我认为ImageList是罪魁祸首。

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

这与其属性无关。

以下是所有者使用ListView中的图片绘制ImageList一次并绘制相同图片但直接从磁盘加载的结果...:

enter image description here enter image description here

你一定可以看到哪个是..

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

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中。