The Wayback Machine - https://web.archive.org/web/20090214012000/http://www.codeguru.com:80/cpp/cpp/cpp_mfc/parsing/article.php/c4091/

CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> C++ >> C++ & MFC >> Parsing


Implementing CustomDraw into Your Project
Rating:

Sakthivel Sugumaran (view profile)
September 9, 2002

Environment: MFC

Here are the steps you need to perform to use this utility:

  1. Create the MFC project with view as ListView.
  2. Apply styles to the view .cpp file.
  3.   BOOL CMyListView::PreCreateWindow(CREATESTRUCT& cs)
      {
        //adding styles
        cs.style |= LVS_REPORT;
        return CListView::PreCreateWindow(cs);
      }
    
  4. Add columns to the view .cpp file.
  5.   void CMyListView::OnInitialUpdate()
      {
        CListView::OnInitialUpdate();
    
        CListCtrl& ListCtrl = GetListCtrl();
    
        //inserting 5 cols
        ListCtrl.InsertColumn(0, "Col-1", LVCFMT_LEFT, 75);
        ListCtrl.InsertColumn(1, "Col-2", LVCFMT_LEFT, 100);
        ListCtrl.InsertColumn(2, "Col-3", LVCFMT_LEFT, 100);
        ListCtrl.InsertColumn(3, "Col-4", LVCFMT_LEFT, 100);
        ListCtrl.InsertColumn(4, "Col-5", LVCFMT_LEFT, 100);
    
        //adding 10 items
        CString item;
        for(int i=0;i<10;i++)
        {
          item.Format("Item-%d",i);
          ListCtrl.InsertItem(i,item);
        }
      }
    
  6. Add the following line to the message map area of the view header(.h) file.
  7.     afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT*
                                  pResult);
    
  8. Add the following line to the message map area of the view .cpp file.
  9.     ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
    
  10. Add the OnCustomDraw function.
  11.         void CMyListView::OnCustomDraw(NMHDR* pNMHDR,
                                           LRESULT* pResult)
            {
             *pResult = 0;
    
               COLORREF dwColourTxt = 0;
               COLORREF dwColourBk = RGB(255, 255, 255);
    
               LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
    
               switch(lplvcd->nmcd.dwDrawStage)
               {
                  Case CDDS_PREPAINT:
                  {
                     *pResult = CDRF_NOTIFYITEMDRAW;
                     return;
                  }
    
                  // Modify item text and or background
                  Case CDDS_ITEMPREPAINT:
                  {
                     if(lplvcd->nmcd.dwItemSpec == 0)
                     {
                        dwColourTxt = RGB(255, 0, 0);     // Red
                     }
                     else if(lplvcd->nmcd.dwItemSpec == 1)
                     {
                        dwColourTxt = RGB(0, 255, 0);     // Green
                     }
                     Else
                     {
                        dwColourTxt = RGB(0, 0, 255);     // Blue
                     }
                     lplvcd->clrText = dwColourTxt;
                     // If you want the sub items the same as
                     // the item, set *pResult to CDRF_NEWFONT
                     *pResult = CDRF_NOTIFYSUBITEMDRAW;
                     return;
                  }
    
                  // Modify sub item text and or background
                  case CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEM:
                  {
                     // Ignore the first sub item; it is the item
                     // handled above
                     if(lplvcd->iSubItem != 0)
                     {
                        // First sub item
                        if(lplvcd->iSubItem == 1)
                        {
                           dwColourTxt = RGB(0, 127, 127);
                                         // Pale blue
                           dwColourBk = RGB(255, 255, 255);
                                         // White
                        }
                        // All sub items after the first
                        Else
                        {
                           dwColourTxt = RGB(255, 255, 255);
                                       // White
                           if(lplvcd->nmcd.dwItemSpec == 0)
                           {
                              dwColourBk = RGB(255, 0, 102);
                                       // Red
                           }
                           else if(lplvcd->nmcd.dwItemSpec == 1)
                           {
                              dwColourBk = RGB(204, 255, 153);
                                       // Green
                           }
                           Else
                           {
                              dwColourBk = RGB(0, 102, 204);
                                       // Blue
                           }
                        }
                        lplvcd->clrText = dwColourTxt;
                        lplvcd->clrTextBk = dwColourBk;
                     }
                     *pResult = CDRF_NEWFONT;
                     return;
                  }
               }
            }
    

Downloads

Download source - 19 Kb

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Already existing article - Legacy CodeGuru (09/10/2002)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers