In Depth Banner
Skip Navigation Links

Select your preferred language

WndProc for the CustomListViewEx class.

The ProcessNotify method.

    protected int ProcessNotify(ref Message m)

    {

      //get the header

      NMHDR hdr=(NMHDR)m.GetLParam(typeof(NMHDR));

      NMLVCUSTOMDRAW cdhdr;

      int retval=(int)CDRFDefs.CDRF_DODEFAULT;

 

      int itemOffset=5;

      if(this.View==View.Details)

      {

        //do a little bit of preparation

        if(this.CheckBoxes)

          itemOffset+=SystemInformation.MenuCheckSize.Width;

        if(this.SmallImageList!=null)

          itemOffset+=this.SmallImageList.ImageSize.Width;

      }

      //Prepare the Graphics

      Graphics g=null;

      //Discover which notify message has been sent.

      if(hdr.code==(int)NMDefs.NM_CUSTOMDRAW)

      {

        //This is a custom draw notification. Get the custom draw header

        NMCUSTOMDRAW nmcd=(NMCUSTOMDRAW)m.GetLParam(typeof(NMCUSTOMDRAW));

        switch(nmcd.dwDrawStage)

        {

          // This is received when the control wants to ascertain if

          // any custom drawing should be done.

          case (int)CDDSDefs.CDDS_PREPAINT:

            //This is to ask if we want to do any painting at-all

            QueryCustomDrawEventArgs e=new QueryCustomDrawEventArgs(this.View);

            //fire the event

            OnQueryCustomDraw(e);

            //if the control is to be custom drawn, e.Inform will be true.

            if(e.Inform)

            {

              //only if in detail view

              if(View==View.Details)

              {

                //and only if specified in the event arguments

                if(e.Subitems)

                  retval = (int)CDRFDefs.CDRF_NOTIFYSUBITEMDRAW; //draw the subitems

                break;

              }

              retval = (int)CDRFDefs.CDRF_NOTIFYITEMDRAW; //otherwise draw the top-level items

            }

            else

              retval = (int)CDRFDefs.CDRF_DODEFAULT; //don't do any custom drawing

            break;

          // Received when custom drawing has been opted for. Each top-level item is exposed in turn

          case (int)CDDSDefs.CDDS_ITEMPREPAINT:

            cdhdr=(NMLVCUSTOMDRAW)m.GetLParam(typeof(NMLVCUSTOMDRAW));

            g=Graphics.FromHdc(cdhdr.nmcd.hdc);

            ListViewItemPaintEventArgs ippa=new ListViewItemPaintEventArgs(g,this.Items[cdhdr.nmcd.dwItemSpec],this.GetItemRect(cdhdr.nmcd.dwItemSpec,ItemBoundsPortion.Entire),0,false,this.View,itemOffset);

            this.OnPaintItem(ippa);

            if(ippa.DoDefault)

            {

              retval=(int)CDRFDefs.CDRF_DODEFAULT;

              break;

            }

            if(ippa.DrawSubItems)

            {

              retval=(int)CDRFDefs.CDRF_NOTIFYSUBITEMDRAW;

              break;

            }

            //We assume drawing was accomplished by the handler

            retval=(int)CDRFDefs.CDRF_SKIPDEFAULT;

            break;

          // This is received for each and every subitem when in

          case (int)(CDDSDefs.CDDS_SUBITEM | CDDSDefs.CDDS_ITEMPREPAINT):

            cdhdr=(NMLVCUSTOMDRAW)m.GetLParam(typeof(NMLVCUSTOMDRAW));

            g=Graphics.FromHdc(cdhdr.nmcd.hdc);

            ippa=new ListViewItemPaintEventArgs(g,

              this.Items[cdhdr.nmcd.dwItemSpec],

              this.GetSubItemRect(cdhdr.nmcd.dwItemSpec,

                cdhdr.iSubItem,

                ItemBoundsPortion.Entire),

              cdhdr.iSubItem,

              true,

              this.View,

              cdhdr.iSubItem==0 ? itemOffset : 0);

            this.OnPaintItem(ippa);

            if(ippa.DoDefault)

            {

              retval=(int)CDRFDefs.CDRF_DODEFAULT;

              break;

            }

            //We assume drawing was accomplished by the handler

            retval=(int)CDRFDefs.CDRF_SKIPDEFAULT;

            break

        }

      }

 

      if(g!=null)

        g.Dispose();

 

      return retval;

    }

 

Return to the article.

Copyright © Bob Powell 2003-2009. All rights reserved