手工绘制ListBox Tips

2007-09-14 19:26:00 | , , ,

生成任意的实体类

ListBoxExItem

继承ListBox类public partial class ListBoxEx : ListBox
设置

 public override DrawMode DrawMode
        {
            get
            {
                return DrawMode.OwnerDrawVariable;
            }
        }
重载
protected override void OnMeasureItem(MeasureItemEventArgs e)
 protected override void OnDrawItem(DrawItemEventArgs e)

 

internal void OnMeasureItem(MeasureItemEventArgs e,
            Padding Margin, Font TitleFont, Font myFont,
            StringFormat sFormat, Size ImageSize)
        {
            int nImageHeight = ImageSize.Height+4;
            int x = (int)Math.Max(nImageHeight,
                   Margin.Top + Margin.Bottom +
                   2+e.Graphics.MeasureString(this.Title == null ? "A" :
                        this.Title, TitleFont, e.ItemWidth, sFormat).Height +
                   Margin.Top +
                   (this.Details == null || this.Details.Length<1?0:
                   2 + e.Graphics.MeasureString(this.Details == null ? "A" :
                        this.Details, myFont, e.ItemWidth, sFormat).Height));
            e.ItemHeight = x;       
        }

Add comment




biuquote
  • Comment
  • Preview
Loading