带图标和多行显示的ListBox
阅读原文时间:2023年07月10日阅读:1

源码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() + );

        // calculate bounds for details text drawing  
        var detailBounds = new Rectangle(e.Bounds.X + margin.Horizontal + imageSize.Width,  
                                         e.Bounds.Y + (int) titleFont.GetHeight() +  + margin.Vertical + margin.Top,  
                                         e.Bounds.Width - margin.Right - imageSize.Width - margin.Horizontal,  
                                         e.Bounds.Height - margin.Bottom - (int) titleFont.GetHeight() -  -  
                                         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(, );  
        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 > )  
        {  
            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(, "John", "ICQ 56465464",image1));  
        exListBox1.Items.Add(new exListBoxItem(, "Bill","ICQ 56465464", image2));  
        exListBox1.Items.Add(new exListBoxItem(, "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();  
    }  
}  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器