ListView的背景图像和自定义单元格的颜色 [英] ListView with background image and custom cell colors

查看:177
本文介绍了ListView的背景图像和自定义单元格的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用自定义单元格颜色列表视图,但是当我在ListView设置背景图片,自定义单元格的颜色将不会再出现。我试着暂时删除背景图像(组装列表时)和应用单元格的颜色后恢复。这将导致没有自定义颜色,但显示的背景。我想如果可能的话,这些列表视图2特性结合起来。

I have a listview that uses custom cell colors, but when I set a background image in the listview, the custom cell colors will not appear anymore. I tried to remove the background image temporarily (when assembling the list) and restore it after applying cell colors. This results in no custom colors but shows the background. I would like to combine these 2 listview properties if possible.

我的code设置/移除背景图片:

My code for setting/removing background image:

list.BackgroundImage = Properties.Resources.bgalpha;
list.BackgroundImage = null;

我的code的设置自定义单元格颜色的部分:

A part of my code for setting custom cell colors:

for (int i = 0; i < kavels.Count(); i++ )
{
    if (list.Items[i].SubItems[1].Text != "0")
    {
        list.Items[i].UseItemStyleForSubItems = false;
        list.Items[i].SubItems[1].BackColor = Color.LightGreen;
    }
}

下面是两张截图:

列表背景观点: http://i.imgur.com/aHUXAVh.png

列表没有背景的看法: http://i.imgur.com/sO83wTP.png

List view without background: http://i.imgur.com/sO83wTP.png

我也试过做一个图片与上ListView控件的顶部透明的PNG图像一起透明背景,但也没有明显的工作。

I also tried making a PictureBox with a transparent background along with a png image with transparency on top of the ListView, but that also didn't work obviously.

推荐答案

您有两种选择:


  • 您可以覆盖一个面板图片框具有半反式preNT图像。对于这个工作,你就必须让坐在里面的ListView,使其覆盖的

  • You could overlay a Panel or a PictureBox with a semi-transprent Image. For this to work you would have to make it sit inside the ListView, so that it is the Parent of the overlay.

但是,这将使列表视图 非可点击。 - 这带来的另一个问题是,它会稍微颜色的文本,所以它不会看起来很正确

But that will make the Listview non-clickable. - Another problem with this is that it will slightly color the text, so it won't look quite right.


  • 或者你也可以设置的ListView 的OwnerDraw = TRUE 并添加code自己做绘图。

  • Or you can set the ListView to OwnerDraw = true and add code to do the drawing yourself.

下面是一个例子,非滚动滚动和:

Here is an example, non-scrolled and scrolled:

请注意,原来的的BackgroundImage 照彻emtpy空间的权利。

Note that the original BackgroundImage shines through the emtpy space to the right.

如果您所有者绘制详细模式一个ListView需要code事件绘制子项和头文件;注意类级别变量保存itemHeight;这假定它们都具有相同的高度。该另一个是需要水平滚动。

If you owner-draw a ListView in Details mode you need to code events to draw subitems and headers; note the class level variable to hold the itemHeight; this assumes they all have the same Height .. The other one is need for horizontal scrolling.

int itemHeight = 0;  // we need this number!
int itemLeft = 0;    // we need this number, too

private void listView1_DrawColumnHeader(object sender,
                                        DrawListViewColumnHeaderEventArgs e)
{
    Rectangle R0 = listView1.GetItemRect(0);
    itemHeight = R0.Height;   // we need this number!
    itemLeft = R0.Left;       // we need this number too

    e.DrawBackground();
    e.DrawText();
}

private void listView1_DrawSubItem(object sender,
                                   DrawListViewSubItemEventArgs e)
{
    Rectangle rrr = listView1.GetItemRect(e.ItemIndex);
    Rectangle rect = e.Bounds;
    Rectangle rect0 = new Rectangle(rect.X - itemLeft , itemHeight * e.ItemIndex,
                                    rect.Width, rect.Height);
    Image img = listView1.BackgroundImage;
    e.Graphics.DrawImage(img, rect, rect0, GraphicsUnit.Pixel);
    using (SolidBrush brush = new SolidBrush(e.SubItem.BackColor))
        e.Graphics.FillRectangle(brush, rect);
    e.DrawText();
}

下面是code设置颜色在的ListViewItem LVI 的例子:

Here is the code to set the colors in the ListViewItem lvi for the example:

lvi.UseItemStyleForSubItems = false;
lvi.BackColor = Color.FromArgb(66, Color.LightBlue);
lvi.SubItems[1].BackColor = Color.FromArgb(77, Color.LightGreen);
lvi.SubItems[2].BackColor = Color.FromArgb(88, Color.LightPink);

注意,code假定你的背景有一个大的图像和没有贴砖参与!另外,code仅如果你不具备的小组

Note that the code assumes your background is one large image and no tiling is involved! Also the code works only if you don't have groups!

这篇关于ListView的背景图像和自定义单元格的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆