源码https://www.codeproject.com/Articles/15464/Extending-the-ListBox-to-show-more-complex-items

定义控件

using System.Drawing;
using System.Windows.Forms;

namespace testexListBox
{
    internal class exListBoxItem
    {
        public exListBoxItem(int id, string title, string details, Image image)
        {
            Id = id;
            Title = title;
            Details = details;
            ItemImage = image;
        }

        public int Id { get; set; }

        public string Title { get; set; }

        public string Details { get; set; }

        public Image ItemImage { get; set; }

        public void drawItem(DrawItemEventArgs e, Padding margin,
                             Font titleFont, Font detailsFont, StringFormat aligment,
                             Size imageSize)
        {
            // if selected, mark the background differently
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                e.Graphics.FillRectangle(Brushes.CornflowerBlue, e.Bounds);
            }
            else
            {
                e.Graphics.FillRectangle(Brushes.Beige, e.Bounds);
            }

            // draw some item separator
            e.Graphics.DrawLine(Pens.DarkGray, e.Bounds.X, e.Bounds.Y, e.Bounds.X + e.Bounds.Width, e.Bounds.Y);

            // draw item image
            e.Graphics.DrawImage(ItemImage, e.Bounds.X + margin.Left, e.Bounds.Y + margin.Top, imageSize.Width,
                                 imageSize.Height);

            // calculate bounds for title text drawing
            var titleBounds = new Rectangle(e.Bounds.X + margin.Horizontal + imageSize.Width,
                                            e.Bounds.Y + margin.Top,
                                            e.Bounds.Width - margin.Right - imageSize.Width - margin.Horizontal,
                                            (int) titleFont.GetHeight() + 2);

            // calculate bounds for details text drawing
            var detailBounds = new Rectangle(e.Bounds.X + margin.Horizontal + imageSize.Width,
                                             e.Bounds.Y + (int) titleFont.GetHeight() + 2 + margin.Vertical + margin.Top,
                                             e.Bounds.Width - margin.Right - imageSize.Width - margin.Horizontal,
                                             e.Bounds.Height - margin.Bottom - (int) titleFont.GetHeight() - 2 -
                                             margin.Vertical - margin.Top);

            // draw the text within the bounds
            e.Graphics.DrawString(Title, titleFont, Brushes.Black, titleBounds, aligment);
            e.Graphics.DrawString(Details, detailsFont, Brushes.DarkGray, detailBounds, aligment);

            // put some focus rectangle
            e.DrawFocusRectangle();
        }
    }

    public partial class exListBox : ListBox
    {
        private readonly Font _detailsFont;
        private readonly StringFormat _fmt;
        private readonly Size _imageSize;
        private readonly Font _titleFont;

        public exListBox(Font titleFont, Font detailsFont, Size imageSize,
                         StringAlignment aligment, StringAlignment lineAligment)
        {
            _titleFont = titleFont;
            _detailsFont = detailsFont;
            _imageSize = imageSize;
            ItemHeight = _imageSize.Height + Margin.Vertical;
            _fmt = new StringFormat();
            _fmt.Alignment = aligment;
            _fmt.LineAlignment = lineAligment;
            _titleFont = titleFont;
            _detailsFont = detailsFont;
        }

        public exListBox()
        {
            InitializeComponent();
            _imageSize = new Size(80, 60);
            ItemHeight = _imageSize.Height + Margin.Vertical;
            _fmt = new StringFormat();
            _fmt.Alignment = StringAlignment.Near;
            _fmt.LineAlignment = StringAlignment.Near;
            _titleFont = new Font(Font, FontStyle.Bold);
            _detailsFont = new Font(Font, FontStyle.Regular);
        }


        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            // prevent from error Visual Designer
            if (Items.Count > 0)
            {
                var item = (exListBoxItem) Items[e.Index];
                item.drawItem(e, Margin, _titleFont, _detailsFont, _fmt, _imageSize);
            }
        }


        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
    }
}

DEMO

using System;
using System.Drawing;
using System.Windows.Forms;

namespace testexListBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Image image1 = Image.FromFile(@"image1.jpg");
            Image image2 = Image.FromFile(@"image2.jpg");
            Image image3 = Image.FromFile(@"image3.jpg");
            // exListBoxItem = int id, string title, string details, Image image
            exListBox1.Items.Add(new exListBoxItem(1, "John", "ICQ 56465464",image1));
            exListBox1.Items.Add(new exListBoxItem(2, "Bill","ICQ 56465464", image2));
            exListBox1.Items.Add(new exListBoxItem(3, "Peter", "ICQ 56465464",image3));
        }

        private void exListBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
        }

        private void exListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Text = ((exListBoxItem) exListBox1.SelectedItem).Id.ToString();
        }
    }
}

posted on 2018-07-03 16:02  181976640  阅读(830)  评论(0编辑  收藏  举报