首页 > 代码库 > 自绘listCtrl控件选中该行高亮(模拟windows)

自绘listCtrl控件选中该行高亮(模拟windows)

CListCtrl的派生类CMyListCtrl的DrawItem()函数里添加代码

  CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    if (lpDrawItemStruct->itemState && LVIS_SELECTED)
    {
        CPen PenLine(PS_SOLID, 1, RGB(0, 0, 0));
        CPen *OldPenLine = pDC->SelectObject(&PenLine);
        rcItem.right -= 2;
        rcItem.bottom -= 2;
        pDC->MoveTo(rcItem.left, rcItem.top);
        pDC->LineTo(rcItem.right, rcItem.top);
        pDC->MoveTo(rcItem.left, rcItem.top);
        pDC->LineTo(rcItem.left, rcItem.bottom);
        pDC->MoveTo(rcItem.left, rcItem.bottom);
        pDC->LineTo(rcItem.right, rcItem.bottom);
        pDC->MoveTo(rcItem.right, rcItem.top);
        pDC->LineTo(rcItem.right, rcItem.bottom);
        pDC->SelectObject(OldPenLine);
        rcItem.DeflateRect(2, 2, 2, 2);
        COLORREF m_color = RGB(71, 173, 255);

        for (int i = rcItem.Height() / 2; i>0; i--)
        {
            CPen pen(PS_SOLID, 1, m_color);
            CPen *OldPen = pDC->SelectObject(&pen);
            pDC->MoveTo(rcItem.left, rcItem.top + i);
            pDC->LineTo(rcItem.right, rcItem.top + i);
            pDC->MoveTo(rcItem.left, rcItem.bottom - i);
            pDC->LineTo(rcItem.right, rcItem.bottom - i);
            pDC->SelectObject(OldPen);
        }
        pDC->SetTextColor(RGB(255, 255, 255));
    }
    else
    {
        pDC->SetTextColor(RGB(0, 0, 0));
        CBrush brush;
        brush.CreateSolidBrush(RGB(255, 255, 255));
        pDC->FillRect(rcItem, &brush);
    }
    CString strText = GetItemText(nRow, 0);
    CRect rcSubItem;
    GetSubItemRect(nRow, 0, LVIR_LABEL, rcSubItem);
    pDC->DrawText(strText, rcSubItem, DT_CENTER);

PenLine是在框内画线,模仿被选中的样子

图元的线型
PS_SOLID 实线
PS_DASH 虚线
PS_DOT 点线
PS_DASHDOT 点化线
PS_DASHDOTDOT 双点化线

 

自绘listCtrl控件选中该行高亮(模拟windows)