C# windows窗体中的自定义列表框(或)自定义面板

C# windows窗体中的自定义列表框(或)自定义面板,c#,winforms,listbox,C#,Winforms,Listbox,我正在尝试创建一个facebook应用程序,以显示时间线和其他消息 我需要自定义列表框,在单个列表项中显示发件人的图像,以及以粗体显示的发件人名称作为标题和邮件内容 我不知道自定义面板,如何使用它 像这样。。。 提前感谢….我写这篇文章是为了让您了解自定义面板的外观(:),这不是一个完整的列表控件,需要进一步扩展,但它满足您的大多数需求。我认为这是一个很好的示例,可以帮助您开始创建自己的自定义控件: public class ListPanel : Panel { public Lis

我正在尝试创建一个facebook应用程序,以显示时间线和其他消息 我需要自定义列表框,在单个列表项中显示发件人的图像,以及以粗体显示的发件人名称作为标题和邮件内容

我不知道自定义面板,如何使用它

像这样。。。


提前感谢….

我写这篇文章是为了让您了解自定义面板的外观(:),这不是一个完整的列表控件,需要进一步扩展,但它满足您的大多数需求。我认为这是一个很好的示例,可以帮助您开始创建自己的自定义控件:

public class ListPanel : Panel
{
    public ListPanel()
    {
        AutoScroll = true;
        BorderStyle = BorderStyle.FixedSingle;
    }
    private List<ListPanelItem> items = new List<ListPanelItem>();
    public void AddItem(ListPanelItem item)
    {
        item.Index = items.Count;
        items.Add(item);            
        Controls.Add(item);
        item.BringToFront();
        item.Click += ItemClicked;
    }
    public int SelectedIndex { get; set; }
    public ListPanelItem SelectedItem { get; set; }
    private void ItemClicked(object sender, EventArgs e)
    {
        ListPanelItem item = sender as ListPanelItem;
        if (SelectedItem != null) SelectedItem.Selected = false;
        SelectedItem = item;
        SelectedIndex = item.Index;
        item.Selected = true;
        if (ItemClick != null) ItemClick(this, new ItemClickEventArgs() {Item = item});
    }
    public class ItemClickEventArgs : EventArgs
    {
        public ListPanelItem Item { get; set; }
    }
    public delegate void ItemClickEventHandler(object sender, ItemClickEventArgs e);
    public event ItemClickEventHandler ItemClick;
}
//Here is the class for ListPanelItem
public class ListPanelItem : Panel
{
    public ListPanelItem()
    {
        DoubleBuffered = true;
        ImageSize = new Size(100, 100);
        CaptionColor = Color.Blue;
        ContentColor = Color.Green;
        CaptionFont = new Font(Font.FontFamily, 13, FontStyle.Bold | FontStyle.Underline);
        ContentFont = new Font(Font.FontFamily, 10, FontStyle.Regular);
        Dock = DockStyle.Top;
        SelectedColor = Color.Orange;
        HoverColor = Color.Yellow;
        Caption = "";
        Content = "";
    }
    private bool selected;
    public Size ImageSize { get; set; }
    public Image Image { get; set; }
    public string Caption { get; set; }
    public string Content { get; set; }
    public Color CaptionColor { get; set; }
    public Color ContentColor { get; set; }
    public Font CaptionFont { get; set; }
    public Font ContentFont { get; set; }
    public int Index { get; set; }
    public bool Selected
    {
        get { return selected; }
        set
        {
            selected = value;
            Invalidate();
        }
    }
    public Color SelectedColor { get; set; }
    public Color HoverColor { get; set; }        
    protected override void OnPaint(PaintEventArgs e)
    {
        Color color1 = mouseOver ? Color.FromArgb(0, HoverColor) : Color.FromArgb(0, SelectedColor);
        Color color2 = mouseOver ? HoverColor : SelectedColor;
        Rectangle actualRect = new Rectangle(ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Width, ClientRectangle.Height - 2);
        using (System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, color1, color2, 90))
        {                                
            if (mouseOver)
            {                                        
                e.Graphics.FillRectangle(brush, actualRect);
            }
            else if (Selected)
            {
                e.Graphics.FillRectangle(brush, actualRect);
            }
        }
        if (Image != null)
        {
            e.Graphics.DrawImage(Image, new Rectangle(new Point(5, 5), ImageSize));
        }
        //Draw caption
        StringFormat sf = new StringFormat() { LineAlignment = StringAlignment.Center};            
        e.Graphics.DrawString(Caption, CaptionFont, new SolidBrush(CaptionColor), new RectangleF(ImageSize.Width + 10, 5, Width - ImageSize.Width - 10, CaptionFont.SizeInPoints * 1.5f), sf);
        //Draw content
        int textWidth = Width - ImageSize.Width - 10;
        SizeF textSize = e.Graphics.MeasureString(Content, ContentFont);
        float textHeight = (textSize.Width / textWidth) * textSize.Height + textSize.Height;
        int dynamicHeight = (int)(CaptionFont.SizeInPoints * 1.5) + (int)textHeight + 1;
        if (Height != dynamicHeight)
        {
            Height = dynamicHeight > ImageSize.Height + 10 ? dynamicHeight : ImageSize.Height + 10;
        }
        e.Graphics.DrawString(Content, ContentFont, new SolidBrush(ContentColor), new RectangleF(ImageSize.Width + 10, CaptionFont.SizeInPoints * 1.5f + 5, Width - ImageSize.Width - 10, textHeight));
        e.Graphics.DrawLine(Pens.Silver, new Point(ClientRectangle.Left, ClientRectangle.Bottom-1), new Point(ClientRectangle.Right, ClientRectangle.Bottom-1));
        base.OnPaint(e);
    }        
    bool mouseOver;
    protected override void OnMouseEnter(EventArgs e)
    {
        mouseOver = true;
        base.OnMouseEnter(e);
        Invalidate();
    }
    protected override void OnMouseLeave(EventArgs e)
    {
        mouseOver = false;
        base.OnMouseLeave(e);
        Invalidate();
    }
}
公共类列表面板:面板
{
公共列表面板()
{
AutoScroll=true;
BorderStyle=BorderStyle.FixedSingle;
}
私有列表项=新列表();
公共无效附加项(ListPanelItem)
{
item.Index=items.Count;
项目。添加(项目);
控件。添加(项);
项目.BringToFront();
item.Click+=ItemClicked;
}
public int SelectedIndex{get;set;}
公共ListPanelItem SelectedItem{get;set;}
已单击私有无效项(对象发送者、事件参数)
{
ListPanelItem=作为ListPanelItem的发件人;
如果(SelectedItem!=null)SelectedItem.Selected=false;
选择editem=项目;
SelectedIndex=项目索引;
item.Selected=true;
如果(ItemClick!=null)ItemClick(这是新的ItemClickEventArgs(){Item=Item});
}
公共类项ClickEventArgs:EventArgs
{
公共ListPanelItem项{get;set;}
}
公共委托无效ItemClickEventHandler(对象发送者,ItemClickEventArgs e);
公共事件项ClickEventHandler项Click;
}
//下面是ListPanelItem的类
公共类ListPanelItem:面板
{
公共ListPanelItem()
{
双缓冲=真;
ImageSize=新尺寸(100100);
CaptionColor=颜色。蓝色;
ContentColor=Color.Green;
CaptionFont=新字体(Font.FontFamily,13,FontStyle.Bold | FontStyle.Underline);
ContentFont=新字体(Font.FontFamily,10,FontStyle.Regular);
Dock=DockStyle.Top;
SelectedColor=Color.Orange;
HoverColor=颜色。黄色;
标题=”;
Content=“”;
}
选择私立学校;
公共大小ImageSize{get;set;}
公共映像映像{get;set;}
公共字符串标题{get;set;}
公共字符串内容{get;set;}
公共颜色字幕颜色{get;set;}
公共颜色ContentColor{get;set;}
公共字体标题字体{get;set;}
公共字体ContentFont{get;set;}
公共int索引{get;set;}
公共图书馆获选
{
获取{return selected;}
设置
{
所选=值;
使无效();
}
}
公共颜色SelectedColor{get;set;}
公共颜色HoverColor{get;set;}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
Color color1=mouseOver?Color.FromArgb(0,HoverColor):Color.FromArgb(0,SelectedColor);
Color color2=鼠标悬停?悬停颜色:SelectedColor;
Rectangle actualRect=新矩形(ClientRectangle.Left、ClientRectangle.Top、ClientRectangle.Width、ClientRectangle.Height-2);
使用(System.Drawing.Drawing2D.LinearGradientBrush笔刷=新的System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle,color1,color2,90))
{                                
如果(鼠标悬停)
{                                        
e、 图形。填充矩形(画笔、实际等);
}
else if(已选择)
{
e、 图形。填充矩形(画笔、实际等);
}
}
如果(图像!=null)
{
e、 DrawImage(图像,新矩形(新点(5,5),图像大小));
}
//绘图标题
StringFormat sf=new StringFormat(){LineAlignment=StringAlignment.Center};
e、 图形.字符串(标题,标题字体,新的SolidBrush(标题颜色),新的矩形F(ImageSize.Width+10,5,Width-ImageSize.Width-10,标题字体.SizeInPoints*1.5f),sf);
//绘制内容
int textWidth=Width-ImageSize.Width-10;
SizeF textSize=e.Graphics.MeasureString(Content,ContentFont);
浮动文本高度=(textSize.Width/textWidth)*textSize.Height+textSize.Height;
int dynamicHeight=(int)(CaptionFont.SizeInPoints*1.5)+(int)text高度+1;
if(高度!=动态灯光)
{
高度=动态灯光>图像大小。高度+10?动态灯光:图像大小。高度+10;
}
e、 Graphics.DrawString(内容、内容字体、新SolidBrush(内容颜色)、新矩形F(ImageSize.Width+10、CaptionFont.SizeInPoints*1.5f+5、宽度-ImageSize.Width-10、文本高度));
e、 图形。抽绳(钢笔。银色,新点(ClientRectangle.Left,ClientRectangle.Bottom-1),新点(ClientRectangle.Right,ClientRectangle.Bottom-1));
基础漆(e);
}        
布尔鼠标器;
MouseCenter上受保护的覆盖无效(事件参数e)
{
mouseOver=true;
base.onmouseinter(e);
使无效();
}
MouseLeave上的受保护覆盖无效(事件参数e)
{
mouseOver=false;
基地,离港(e);;
使无效();
}
}
代码没有经过优化,例如一些代码可以在
OnPaint
方法之外完成,但为了简单起见,我将其保留在那里

下面是我的列表控件(或列表框)的外观:


定制面板怎么样?这比列表框更合适。谢谢,我不知道自定义面板,我将搜索它…自定义面板继承自标准
面板
您可以给我一个自定义面板的示例吗