2013-06-20 4 views
-1

인스턴트 메신저 응용 프로그램을 만들고 타임 라인 및 기타 메시지를 표시하려고합니다. 보낸 사람의 이미지와 보낸 사람의 이름을 표시하려면 목록 상자를 사용자 지정해야합니다. 하나의 목록 항목에서 표제와 메시지 내용으로 굵게 표시됩니다.사용자 지정 목록 상자 (또는) 사용자 지정 패널 형태의 Windows

내가 정의 패널을 잘 모릅니다, 그 사용 방법을 ...이 같은

... 사전에 I want this type of list item

감사합니다 ....

+0

어떤 사용자 정의 패널에 대해 : 여기

내리스트 컨트롤 (또는 목록 상자)의 모습인가? 그것은리스트 박스보다 더 적합 할 것이다. –

+0

** 감사합니다 ** ** 사용자 정의 패널 **에 대해 알고 싶습니다 ** 그것에 대해 검색 할 것입니다 ... – Anjan

+0

사용자 정의 패널이 표준'Panel'에서 상속받습니다 –

답변

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 외부에서 수행 할 수있는 최적화되지 않은 방법을 사용하지만 단순함을 위해 거기에 보관합니다.

enter image description here

+0

ur 솔루션에 대한 감사, 그것은 나에게 매우 유용합니다 ... – Anjan

관련 문제