【www.gdgbn.com--WinForm】

asp教程.net winform的 listview控件

using system.collections.generic;
using system.drawing;
using system.windows.forms;

namespace qiqi.windows.forms
{
    public class qiqilistbox : control
    {
        private readonly list _items;
        private color _border_color;
        private int _item_height;

        private color _selected_back_color;
        private int _selected_index;

        public qiqilistbox()
        {
            setstyle(controlstyles.userpaint | controlstyles.allpaintinginwmpaint, true);
            _items = new list();
            _item_height = 24;
            _selected_index = -1;
            _border_color = color.fromargb(245, 170, 114);
            _selected_back_color = color.fromargb(255, 237, 209);
        }

        public color bordercolor
        {
            get { return _border_color; }
            set { _border_color = value; }
        }

        public color selectedbackcolor
        {
            get { return _selected_back_color; }
            set { _selected_back_color = value; }
        }

        public int selectedindex
        {
            get { return _selected_index; }
            set { _selected_index = value; }
        }

        public int itemheight
        {
            get { return _item_height; }
            set { _item_height = value; }
        }

        public void insert_item(qiqilistboxitem item, int index)
        {
            _items.insert(index, item);
        }

        public void delete_item(int index)
        {
            _items.removeat(index);
        }

        protected override void onpaintbackground(painteventargs pevent)
        {
            base.onpaintbackground(pevent);
            //边框
            using (var pen = new pen(_border_color))
            {
                rectangle rect = displayrectangle;
                rect.width -= 1;
                rect.height -= 1;
                pevent.graphics.drawrectangle(pen, rect);
            }
        }

        protected override void onpaint(painteventargs e)
        {
            for (int i = 0; i < _items.count; i++)
            {
                draw_item(e.graphics, i, i == _selected_index);
            }
        }

        private void draw_item(graphics g, int index, bool selected)
        {
            qiqilistboxitem item = _items[index];
            rectangle rect = get_item_rect(index);
            using (brush bru = new solidbrush(selected ? _selected_back_color : backcolor))
            {
                g.fillrectangle(bru, rect);
            }
            //图标
            g.drawimage(item.image, 1, 1 + index*_item_height, 24, 24);
            rectangle rect_text = rect;
            rect_text.width -= 24;
            rect_text.x += 24 + 10;
            var sf = new stringformat();
            sf.linealignment = stringalignment.center;
            g.drawstring(item.text, font, brushes.black, rect_text, sf);
        }

        private rectangle get_item_rect(int index)
        {
            return new rectangle(2, index*_item_height + 2, width - 4, _item_height);
        }

        public int hit_test(int x, int y)
        {
            for (int i = 0; i < _items.count; i++)
            {
                if (get_item_rect(i).contains(x, y))
                    return i;
            }
            return -1;
        }

        protected override void onmousedown(mouseeventargs e)
        {
            base.onmousedown(e);
            int index = hit_test(e.x, e.y);
            graphics g = creategraphics();
            if (_selected_index != -1)
                draw_item(g, _selected_index, false);
            if (index != -1)
            {
                _selected_index = index;
                draw_item(g, index, true);
            }
        }
    }

    public class qiqilistboxitem
    {
        public image image { get; set; }
        public string text { get; set; }
        public object tag { get; set; }
    }
}

//调用方法

  list.insert_item(new qiqilistboxitem
                                 {
                                     image = image.fromfile(@"f:\temp\i1.png"),
                                     text = "智能秘书",
                                 }, 0);
            list.insert_item(new qiqilistboxitem
                                 {
                                     image = image.fromfile(@"f:\temp\i2.png"),
                                     text = "信息订阅",
                                 }, 0);
            list.insert_item(new qiqilistboxitem
                                 {
                                     image = image.fromfile(@"f:\temp\i3.png"),
                                     text = "空间信息",
                                 }, 0);
            list.invalidate();

本文来源:http://www.gdgbn.com/asp/25475/