我试图创建一个facebook应用程序,以显示时间轴和其他消息 我需要自定义列表框,以粗体显示发件人的图像和发件人的名称作为单个列表项中的标题和邮件内容。
我不知道自定义面板,如何使用...
像这样......提前感谢....
答案 0 :(得分:1)
我已经尝试写这个让你看看自定义面板的样子(:),这不是一个完整的列表控件,需要进一步扩展,但它可以满足你的大多数需求。我认为这是一个很好的例子,可以帮助您入门,以便您轻松构建自己的自定义控件:
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();
}
}
代码尚未优化,例如某些代码可以在OnPaint
方法之外完成,但为了简单起见,我将其保留在那里。
以下是我的列表控件(或列表框)的外观: