• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

Select whole line in CListCtrl report view?

Does anyone have a class derived from CListCtrl that will select the entire line in report view rather than just the item?
0
duncan2
Asked:
duncan2
1 Solution
 
Tommy HuiCommented:
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);
      UINT uiFlags = ILD_TRANSPARENT;
      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
      GetItem(&lvi);

      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)));
      }
      else
            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)
                  continue;

            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;
                        break;
                  case LVCFMT_CENTER:
                        nJustify = DT_CENTER;
                        break;
                  default:
                        break;
                  }
            }

            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)
            pDC->DrawFocusRect(rcAllLabels);

// set original colors if item was selected

      if (bSelected)
      {
        pDC->SetTextColor(clrTextSave);
            pDC->SetBkColor(clrBkSave);
      }
}

0
 
yifCommented:
test
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now