一个自定义的CheckedListItem

来源:互联网 发布:天猫双11销售额数据图 编辑:程序博客网 时间:2024/04/28 13:58

起因,由于我看到.net1.1自带的CheckedListItem界面不是那么符合要求,所以就想自己来进行绘画。但是CheckedListItem没有OwnerDraw属性,所以就想到用ListBox来进行模拟。里面用到Check.bmp和UnCheck.bmp都放在相册区

1、用于扩展Item的类:
public class ListBoxExtend
 {
  public ListBoxExtend()
  {
   //
   // TODO: Add constructor logic here
   //
  } 
    private string name;
    private bool ischecked;
  private bool isselected;
  public string Name
  {
   get {return this.name; }
   set {this.name = value; }
  }
   
  public bool IsChecked
  {
   get {return this.ischecked; }
   set {this.ischecked = value; }
  }
  public bool IsSelected
  {
   get {return this.isselected; }
   set {this.isselected = value; }
  }
 }

2、定义ListBox2的属性:
//
   // listBox2
   //
   this.listBox2.BorderStyle = System.Windows.Forms.BorderStyle.None;
   this.listBox2.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
   this.listBox2.Location = new System.Drawing.Point(208, 64);
   this.listBox2.Name = "listBox2";
   this.listBox2.Size = new System.Drawing.Size(256, 232);
   this.listBox2.TabIndex = 1;
   this.listBox2.DoubleClick += new System.EventHandler(this.listBox2_DoubleClick);
   this.listBox2.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.listBox2_MeasureItem);
   this.listBox2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox2_DrawItem);
   this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);

3、ListBox2的几个事件
private void listBox2_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
  {
   // calc width
   e.ItemHeight = 20;
  }

  private void listBox2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
  {
   // draw

   ListBoxExtend ext = this.listBox2.Items[e.Index] as ListBoxExtend;
   if (ext.IsSelected)
   {
    e.Graphics.FillRectangle(Brushes.Navy, e.Bounds);
    // draw border
    
    PointF p = new PointF(e.Bounds.Left + 2, e.Bounds.Top + 3);
    e.Graphics.DrawString(ext.Name, e.Font, Brushes.White, p);

    if (ext.IsChecked)
    {
     Point p2 = new Point(e.Bounds.Right - 20, e.Bounds.Top + 2);
     e.Graphics.DrawImage(this.imageList1.Images[0], p2);
    }
    else
    {
     Point p2 = new Point(e.Bounds.Right - 20, e.Bounds.Top + 2);
     e.Graphics.DrawImage(this.imageList1.Images[1], p2);
    }
   }
   else
   {
    e.Graphics.FillRectangle(Brushes.White, e.Bounds);
    // draw border
    e.Bounds.Inflate(-1, -1);
    Rectangle r = new Rectangle(e.Bounds.Location, new Size(e.Bounds.Width - 1, e.Bounds.Height));
    e.Graphics.DrawRectangle(Pens.Navy, r);
    //Point p3 = new Point(e.Bounds.Right - 20, e.Bounds.Top);
    //Point p4 = new Point(e.Bounds.Right - 20, e.Bounds.Bottom);
    //e.Graphics.DrawLine(Pens.Navy, p3, p4);
    PointF p = new PointF(e.Bounds.Left + 2, e.Bounds.Top + 3);
    e.Graphics.DrawString(ext.Name, e.Font, Brushes.Black, p);

    if (ext.IsChecked)
    {
     Point p2 = new Point(e.Bounds.Right - 20, e.Bounds.Top + 2);
     e.Graphics.DrawImage(this.imageList1.Images[0], p2);
    }
    else
    {
     Point p2 = new Point(e.Bounds.Right - 20, e.Bounds.Top + 2);
     e.Graphics.DrawImage(this.imageList1.Images[1], p2);
    }

    //p = null;
    //p2 = null;
    //p3 = null;
    //p4 = null;
   }
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   for (int i=0; i< 20; i++)
   {
    ListBoxExtend ext = new ListBoxExtend();
    if (i < 5)
     ext.Name = "Item" + i.ToString();
    else if ( i > 8)
     ext.Name = "HightItem " + i.ToString();
    else
     ext.Name = "NormalItem" + i.ToString();
    if (i == 6 || i == 8)
     ext.IsChecked = true;
    else
     ext.IsChecked = false;
    ext.IsSelected = false;

    this.listBox2.Items.Add(ext);
   }
  }

  private void listBox2_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   foreach (ListBoxExtend ext2 in listBox2.Items)
   {
    ext2.IsSelected = false;
   }

   ListBoxExtend ext = listBox2.Items[listBox2.SelectedIndex] as ListBoxExtend;
   ext.IsSelected = true;

   listBox2.Invalidate();
  }

  private void listBox2_DoubleClick(object sender, System.EventArgs e)
  {
   int selIdx = listBox2.SelectedIndex;
   if (selIdx > -1 && selIdx < listBox2.Items.Count)
   {
    ListBoxExtend ext = listBox2.Items[selIdx] as ListBoxExtend;
    ext.IsChecked = !ext.IsChecked;
   }

   listBox2.Invalidate();
  }

原创粉丝点击