Listviewitem高亮颜色 [英] Listviewitem highlight colour

查看:69
本文介绍了Listviewitem高亮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在 windows窗体应用程序中有一个listview,其中有2列,如下所示:

Hi,

I have a listview in a windows forms application with 2 columns as shown below:

listUsers1.View = View.Details;
listUsers1.FullRowSelect = true;
listUsers1.Columns.Add("Icon", 35, HorizontalAlignment.Left);
listUsers1.Columns.Add("Name", 200, HorizontalAlignment.Left);



这将显示和图标,然后是一个字符串.我需要能够选择一行,并且当其突出显示时,只有名称在蓝色背景上,而不是图标.

我需要保留fullrow select选项,因为当我尝试删除它时,我失去了一些功能.

基本上,我将图标的突出显示颜色设置为白色.

我尝试更改背景色,但正如预期的那样,在删除选择项后,此效果才生效.

有什么想法吗?



This shows and icon and then a string. I need to be able to select a row and when it is highlighted only the name is on a blue background and not the icon.

I need to leave the fullrow select option as when I tried to remove it I lost some functionality.

Basically I wnat to set the highlight colour of the icon to white.

I tried changing the backcolor but as expected this takes effect after the selection is removed.

Any ideas please?

推荐答案

网上有一些例子.其中之一是ListViewItem的DrawText方法的文档: http://msdn.microsoft.com/zh-cn/library/989zcatz.aspx [ ^ ]

我发现DrawText()方法不能正确放置文本,无论我通过了什么标记.因此,我改用了DrawString(它仍然无法完美定位,但至少对e.DrawString有更多控制权).您可能会在第一栏中显示一个图标,因此必须使用e.Graphics绘制图像.

附带说明. ListView不是显示数据的好控件.如果您需要自定义更多内容,将为您选择它感到遗憾.请考虑使用DataGridView.

There are a few examples on the Net. One of them is in documentation of the DrawText method of the ListViewItem: http://msdn.microsoft.com/en-us/library/989zcatz.aspx[^]

I found that the DrawText() method does not position the text properly, no matter what flags I passed it. So I used the DrawString instead (which still didn''t position it perfectly, but at least there''s more control over e.DrawString). You are probably showing an icon in the first column, so you will have to use the e.Graphics to draw the image.

On a side note. ListView is not a good control to display data. You''ll be sorry you ever chose it if you ever need to customize some more. Consider a DataGridView instead.

public partial class Form1 : Form
    {
        private ColumnHeader _iconHeader;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            listUsers1.View = View.Details;
            listUsers1.FullRowSelect = true;
            _iconHeader = listUsers1.Columns.Add("Icon", 35, HorizontalAlignment.Left);
            listUsers1.Columns.Add("Name", 200, HorizontalAlignment.Left);

            listUsers1.Items.Add(new ListViewItem(new[] { "def", "def" }));
            listUsers1.Items.Add(new ListViewItem(new[] { "abc2", "def" }));
            listUsers1.Items.Add(new ListViewItem(new[] { "abc3", "def" }));
            listUsers1.Items.Add(new ListViewItem(new[] { "abc4", "def" }));

            listUsers1.OwnerDraw = true;
            listUsers1.DrawItem += new DrawListViewItemEventHandler(listUsers1_DrawItem);
            listUsers1.DrawSubItem += new DrawListViewSubItemEventHandler(listUsers1_DrawSubItem);
            listUsers1.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(listUsers1_DrawColumnHeader);
        }

        void listUsers1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            ListViewItemStates selectedState = ListViewItemStates.Selected | ListViewItemStates.Focused;
            if (selectedState == (e.State & selectedState))
            {
                e.DrawBackground();
                e.DrawText();
            }
            else
            {
                e.DrawDefault = true;
            }            
        }

        void listUsers1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawDefault = true;
        }

        void listUsers1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            if (e.ColumnIndex == listUsers1.Columns.IndexOf(_iconHeader)
                && (e.ItemState & ListViewItemStates.Focused) == ListViewItemStates.Focused)
            {
                e.DrawBackground();
                using (StringFormat sf = new StringFormat())
                {
                    sf.Alignment = StringAlignment.Near;
                    sf.LineAlignment = StringAlignment.Center;

                    e.Graphics.DrawString(e.SubItem.Text, listUsers1.Font, Brushes.Black, e.Bounds, sf);
                }
            }
            else
            {
                e.DrawDefault = true;
            }
        }
    }


这篇关于Listviewitem高亮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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