.
In Depth Banner
Skip Navigation Links

Select your preferred language

Drawing the items.

    #region Custom drawing methods

 

    private void ProcessDrawItem(ref Message m)

    {

      //Extract the structure from the LParam.

      DRAWITEMSTRUCT dis=(DRAWITEMSTRUCT)Marshal.PtrToStructure(m.LParam, typeof(DRAWITEMSTRUCT));

 

      //Wrap the hDC in a Graphics object

      Graphics g=Graphics.FromHdc(dis.hDC);

 

      //read the horizontal scroll position

      int hScroll=GetScrollPos(this.Handle,SBDefs.SB_HORZ);

 

      //obtain the item that needs to be drawn

      ListViewItem i=this.Items[dis.itemID];

      //get the rectangle of the item

      Rectangle rcItem=new Rectangle(dis.rcItem.left, dis.rcItem.top, this.ClientSize.Width+hScroll, dis.rcItem.Height);

 

      //Get the header positions

      ArrayList al=new ArrayList();

      Rectangle columnRC=new Rectangle(0,0,0,0);

      foreach(ColumnHeader c in this.Columns)

      {

        columnRC.Size=new Size(c.Width,1);

        al.Add(columnRC);

        columnRC.Location=new Point(columnRC.Left+columnRC.Width,0);

      }

        

      //Draw the item according to it's state

 

      SolidBrush sb=new SolidBrush(Color.White);

      Pen p=new Pen(Color.White,-1);

 

      //draw the background...

      if((dis.itemState & (int)ODSDefs.ODS_SELECTED)!=0)

      {

        //item is selected

        sb.Color=this.SelectedBackColor;

        g.FillRectangle(sb,rcItem);

      }

      else

      {

        if(_repeaterLook)

        {

          if((dis.itemID & 1)==0)

            sb.Color=this.EvenLineBackColor;

          else

            sb.Color=this.OddLineBackColor;

        }

        else

          sb.Color=this.BackColor;

        g.FillRectangle(sb,rcItem);

      }

 

      //Draw the Foreground.

 

      if((dis.itemState & (int)ODSDefs.ODS_SELECTED)!=0)

        sb.Color=this.SelectedForeColor;

      else

      {

        if(RepeaterLook)

          if((dis.itemID & 1)==0)

            sb.Color=this.EvenLineForeColor;

          else

            sb.Color=this.OddLineForeColor;

        else

          sb.Color=this.ForeColor;

      }

 

      //standard offset of item text from left border

      int itemOffset=5;

 

      //Draw the icon centered vertically

      if(this.SmallImageList!=null)

      {

        if(i.ImageIndex>-1)

          g.DrawImage(SmallImageList.Images[i.ImageIndex],

            new Rectangle(rcItem.Left,

              rcItem.Top+rcItem.Height/2-SmallImageList.ImageSize.Height/2,

              SmallImageList.ImageSize.Width,

              SmallImageList.ImageSize.Width),

            0,0,SmallImageList.ImageSize.Width,SmallImageList.ImageSize.Height,

            GraphicsUnit.Pixel);

 

        itemOffset+=SmallImageList.ImageSize.Width;

      }

 

      //Draw the checkboc centered vertically

      if(this.CheckBoxes)

      {

        //Draw a CheckBox

        ControlPaint.DrawCheckBox(g,

          rcItem.Left+1+itemOffset,

          rcItem.Top+rcItem.Height/2-SystemInformation.MenuCheckSize.Height/2,

          SystemInformation.MenuCheckSize.Width,

          SystemInformation.MenuCheckSize.Height,

          i.Checked ? ButtonState.Checked : ButtonState.Normal);

        itemOffset+=SystemInformation.MenuCheckSize.Width;

      }

 

      RectangleF subrcItem=new RectangleF(0,0,0,0);

      // then the item

      if(al.Count>0)

        subrcItem=new RectangleF(((Rectangle)al[0]).Left+itemOffset-hScroll,rcItem.Top,((Rectangle)al[0]).Width-itemOffset,rcItem.Height);

      else

        subrcItem=new RectangleF(rcItem.Left+itemOffset-hScroll, rcItem.Top, rcItem.Width-itemOffset, rcItem.Height);

 

      //We'll use ellipsis character trimming if a header is too small for the text

      StringFormat sf=(StringFormat)StringFormat.GenericTypographic.Clone();

      sf.Trimming|=StringTrimming.EllipsisCharacter;

      sf.FormatFlags|=StringFormatFlags.FitBlackBox;

 

      if(subrcItem.Width>0)

        g.DrawString(i.Text,Font,sb,subrcItem,sf);

 

      // now the sub-items if any

 

      for(int n=1; n<i.SubItems.Count; n++)

      {

        //Only draw a sub-item with a header

        if(n<al.Count)

        {

          Rectangle headerRect=(Rectangle)al[n];

          subrcItem=new Rectangle(headerRect.Left-hScroll+5,rcItem.Top,headerRect.Width,rcItem.Bottom-1);

          g.DrawString(i.SubItems[n].Text,Font,sb,subrcItem,sf);

        }

      }

 

      //clean up the StringFormat

      sf.Dispose();

 

      //clean up the graphics object

      g.Dispose();

 

      //clean up brushes and pens.

      sb.Dispose();

 

      p.Dispose();

 

      //We processed the message

      m.Result=(IntPtr)1;

        

    }

 

    #endregion

 

 

Return to the article.

Copyright © Bob Powell 2000-.  All rights reserved.