Select whole line in CListCtrl report view?

Posted on 1998-11-21
Medium Priority
Last Modified: 2013-11-20
Does anyone have a class derived from CListCtrl that will select the entire line in report view rather than just the item?
Question by:duncan2
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Accepted Solution

Tommy Hui earned 1200 total points
ID: 1325117
Make sure the list ctrl is ownerdrawn and override its DrawItem member function:

void CPublishList::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
      CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
      CRect rcItem(lpDrawItemStruct->rcItem);
      int nItem = lpDrawItemStruct->itemID;
      static _TCHAR szBuff[MAX_PATH];
      LPCTSTR pszText;
      COLORREF clrTextSave, clrBkSave;
      CRect rcLabel;
      CRect rcAllLabels;

// get item data

      LV_ITEM lvi;
      lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
      lvi.iItem = nItem;
      lvi.iSubItem = 0;
      lvi.pszText = szBuff;
      lvi.cchTextMax = sizeof(szBuff);
      lvi.stateMask = 0xFFFF;            // get all state flags

      GetItemRect(nItem, rcAllLabels, LVIR_BOUNDS);
      GetItemRect(nItem, rcItem, LVIR_LABEL);

      BOOL bSelected = lvi.state & LVIS_SELECTED;
      bSelected = bSelected || (lvi.state & LVIS_DROPHILITED);

      if (bSelected)
            clrTextSave = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
            clrBkSave = pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));

            pDC->FillRect(rcAllLabels, &CBrush(::GetSysColor(COLOR_HIGHLIGHT)));
            pDC->FillRect(rcAllLabels, &CBrush(RGB(255,255,255)));

// draw item label

      pszText = MakeShortString(pDC, szBuff,
                        rcItem.right-rcItem.left, 2*OFFSET_FIRST);

      rcLabel = rcItem;
      rcLabel.left += OFFSET_FIRST;
      rcLabel.right -= OFFSET_FIRST;

      pDC->DrawText(pszText,-1,rcLabel,DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER);

// draw labels for extra columns

      LV_COLUMN lvc;
      lvc.mask = LVCF_FMT | LVCF_WIDTH;

      for(int nColumn = 1; GetColumn(nColumn, &lvc); nColumn++)
            rcItem.left = rcItem.right;
            rcItem.right += lvc.cx;

            int nRetLen = GetItemText(nItem, nColumn,
                                    szBuff, sizeof(szBuff));
            if (nRetLen == 0)

            pszText = MakeShortString(pDC, szBuff,
                  rcItem.right - rcItem.left, 2*OFFSET_OTHER);

            UINT nJustify = DT_LEFT;

            if(pszText == szBuff)
                  switch(lvc.fmt & LVCFMT_JUSTIFYMASK)
                  case LVCFMT_RIGHT:
                        nJustify = DT_RIGHT;
                  case LVCFMT_CENTER:
                        nJustify = DT_CENTER;

            rcLabel = rcItem;
            rcLabel.left += OFFSET_OTHER;
            rcLabel.right -= OFFSET_OTHER;

            pDC->DrawText(pszText, -1, rcLabel,
                  nJustify | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER);

// draw focus rectangle if item has focus

      if (lvi.state & LVIS_FOCUSED)

// set original colors if item was selected

      if (bSelected)


Expert Comment

ID: 1325119

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question