DOWRD ExStyle = ListCtrl.GetExtendedStyle()
ListCtrl.SetExtendedStyle(ExStyle|LVS_EX_FULLROWSELECT);//这个扩展风格只能与LVS_REPORT 风格一起作用

解决方案 »

  1.   

    点亮整行
    继承CListCtrl
    例子// ListCtEx.h : interface of the CListCtrlEx class
    //
    #if !defined(ListCtrl_Ex)
    #define ListCtrl_Exclass CListCtrlEx : public CListCtrl
    {
    // Construction
    public:
    CListCtrlEx();// Attributes
    protected:
    BOOL m_bFullRowSel;public:
    BOOL SetFullRowSel(BOOL bFillRowSel);
    BOOL GetFullRowSel(); BOOL m_bClientWidthSel;// Overrides
    protected:
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);// Implementation
    public:
    virtual ~CListCtrlEx();
    #ifdef _DEBUG
    virtual void Dump(CDumpContext& dc) const;
    #endifprotected:
    static LPCTSTR MakeShortString(CDC* pDC, LPCTSTR lpszLong, int nColumnLen, int nOffset);
    void RepaintSelectedItems();// Implementation - client area width
    int m_cxClient;// Implementation - state icon width
    int m_cxStateImageOffset;// Implementation - list view colors
    COLORREF m_clrText;
    COLORREF m_clrTextBk;
    COLORREF m_clrBkgnd;};/////////////////////////////////////////////////////////////////////////////
    #endif
    // ListCtEx.cpp : implementation of the CListCtrlEx class
    //#include "ListCtEx.h"                                       /////////////////////////////////////////////////////////////////////////////
    // CListCtrlEx/////////////////////////////////////////////////////////////////////////////
    // CListCtrlEx construction/destructionCListCtrlEx::CListCtrlEx()
    {
    m_bFullRowSel = FALSE;
    m_bClientWidthSel = TRUE; m_cxClient = 0;
    m_cxStateImageOffset = 0; m_clrText = ::GetSysColor(COLOR_WINDOWTEXT);
    m_clrTextBk = ::GetSysColor(COLOR_WINDOW);
    m_clrBkgnd = ::GetSysColor(COLOR_WINDOW);
    }CListCtrlEx::~CListCtrlEx()
    {
    }BOOL CListCtrlEx::SetFullRowSel(BOOL bFullRowSel)
    {
    // no painting during change
    LockWindowUpdate(); m_bFullRowSel = bFullRowSel; BOOL bRet; if (m_bFullRowSel)
    bRet = ModifyStyle(0L, LVS_OWNERDRAWFIXED);
    else
    bRet = ModifyStyle(LVS_OWNERDRAWFIXED, 0L); // repaint window if we are not changing view type
    if (bRet && (GetStyle() & LVS_TYPEMASK) == LVS_REPORT)
    Invalidate(); // repaint changes
    UnlockWindowUpdate(); return(bRet);
    }BOOL CListCtrlEx::GetFullRowSel()
    {
    return(m_bFullRowSel);
    }/////////////////////////////////////////////////////////////////////////////
    // CListCtrlEx drawing// offsets for first and other columns
    #define OFFSET_FIRST    2
    #define OFFSET_OTHER    6void CListCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
    CListCtrl& ListCtrl=*this;
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    CRect rcItem(lpDrawItemStruct->rcItem);
    UINT uiFlags = ILD_TRANSPARENT;
    CImageList* pImageList;
    int nItem = lpDrawItemStruct->itemID;
    BOOL bFocus = (GetFocus() == this);
    COLORREF clrTextSave, clrBkSave;
    COLORREF clrImage = m_clrBkgnd;
    static _TCHAR szBuff[MAX_PATH];
    LPCTSTR pszText;// 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
    ListCtrl.GetItem(&lvi); BOOL bSelected = (bFocus || (GetStyle() & LVS_SHOWSELALWAYS)) && lvi.state & LVIS_SELECTED;
    bSelected = bSelected || (lvi.state & LVIS_DROPHILITED);// set colors if item is selected CRect rcAllLabels;
    ListCtrl.GetItemRect(nItem, rcAllLabels, LVIR_BOUNDS); CRect rcLabel;
    ListCtrl.GetItemRect(nItem, rcLabel, LVIR_LABEL); rcAllLabels.left = rcLabel.left;
    if (m_bClientWidthSel && rcAllLabels.right<m_cxClient)
    rcAllLabels.right = m_cxClient; 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(m_clrTextBk));// set color and mask for the icon if (lvi.state & LVIS_CUT)
    {
    clrImage = m_clrBkgnd;
    uiFlags |= ILD_BLEND50;
    }
    else if (bSelected)
    {
    clrImage = ::GetSysColor(COLOR_HIGHLIGHT);
    uiFlags |= ILD_BLEND50;
    }// draw state icon UINT nStateImageMask = lvi.state & LVIS_STATEIMAGEMASK;
    if (nStateImageMask)
    {
    int nImage = (nStateImageMask>>12) - 1;
    pImageList = ListCtrl.GetImageList(LVSIL_STATE);
    if (pImageList)
    {
    pImageList->Draw(pDC, nImage,
    CPoint(rcItem.left, rcItem.top), ILD_TRANSPARENT);
    }
    }// draw normal and overlay icon CRect rcIcon;
    ListCtrl.GetItemRect(nItem, rcIcon, LVIR_ICON); pImageList = ListCtrl.GetImageList(LVSIL_SMALL);
    if (pImageList)
    {
    UINT nOvlImageMask=lvi.state & LVIS_OVERLAYMASK;
    if (rcItem.left<rcItem.right-1)
    {
    ImageList_DrawEx(pImageList->m_hImageList, lvi.iImage,
    pDC->m_hDC,rcIcon.left,rcIcon.top, 16, 16,
    m_clrBkgnd, clrImage, uiFlags | nOvlImageMask);
    }
    }// draw item label ListCtrl.GetItemRect(nItem, rcItem, LVIR_LABEL);
    rcItem.right -= m_cxStateImageOffset; 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; ListCtrl.GetColumn(nColumn, &lvc); nColumn++)
    {
    rcItem.left = rcItem.right;
    rcItem.right += lvc.cx; int nRetLen = ListCtrl.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 && bFocus)
    pDC->DrawFocusRect(rcAllLabels);// set original colors if item was selected if (bSelected)
    {
    pDC->SetTextColor(clrTextSave);
    pDC->SetBkColor(clrBkSave);
    }
    }LPCTSTR CListCtrlEx::MakeShortString(CDC* pDC, LPCTSTR lpszLong, int nColumnLen, int nOffset)
    {
    static const _TCHAR szThreeDots[] = _T("..."); int nStringLen = lstrlen(lpszLong); if(nStringLen == 0 ||
    (pDC->GetTextExtent(lpszLong, nStringLen).cx + nOffset) <= nColumnLen)
    {
    return(lpszLong);
    } static _TCHAR szShort[MAX_PATH]; lstrcpy(szShort,lpszLong);
    int nAddLen = pDC->GetTextExtent(szThreeDots,sizeof(szThreeDots)).cx; for(int i = nStringLen-1; i > 0; i--)
    {
    szShort[i] = 0;
    if((pDC->GetTextExtent(szShort, i).cx + nOffset + nAddLen)
    <= nColumnLen)
    {
    break;
    }
    } lstrcat(szShort, szThreeDots);
    return(szShort);
    }void CListCtrlEx::RepaintSelectedItems()
    {
    CListCtrl& ListCtrl = *this;
    CRect rcItem, rcLabel;// invalidate focused item so it can repaint properly int nItem = ListCtrl.GetNextItem(-1, LVNI_FOCUSED); if(nItem != -1)
    {
    ListCtrl.GetItemRect(nItem, rcItem, LVIR_BOUNDS);
    ListCtrl.GetItemRect(nItem, rcLabel, LVIR_LABEL);
    rcItem.left = rcLabel.left; InvalidateRect(rcItem, FALSE);
    }// if selected items should not be preserved, invalidate them if(!(GetStyle() & LVS_SHOWSELALWAYS))
    {
    for(nItem = ListCtrl.GetNextItem(-1, LVNI_SELECTED);
    nItem != -1; nItem = ListCtrl.GetNextItem(nItem, LVNI_SELECTED))
    {
    ListCtrl.GetItemRect(nItem, rcItem, LVIR_BOUNDS);
    ListCtrl.GetItemRect(nItem, rcLabel, LVIR_LABEL);
    rcItem.left = rcLabel.left; InvalidateRect(rcItem, FALSE);
    }
    }// update changes UpdateWindow();
    }/////////////////////////////////////////////////////////////////////////////
    // CListCtrlEx diagnostics#ifdef _DEBUGvoid CListCtrlEx::Dump(CDumpContext& dc) const
    {
    CListCtrl::Dump(dc); dc << "m_bFullRowSel = " << (UINT)m_bFullRowSel;
    dc << "\n";
    dc << "m_cxStateImageOffset = " << m_cxStateImageOffset;
    dc << "\n";
    }#endif //_DEBUG