现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

VC中在listctrl中嵌入进度条

2012-08-13 03:15 工业·编程 ⁄ 共 2410字 ⁄ 字号 暂无评论

void CMainDialog::DrawText(int nItem, int nSubItem, CDC *pDC,  COLORREF crText,  COLORREF crBkgnd, CRect &rect)
{
    ASSERT(pDC);
    pDC->FillSolidRect(&rect, crBkgnd);

    int nProcess = 50;//m_list.GetItemData(nItem);
    CRect procRect = rect;
    pDC->Rectangle(procRect);

    procRect.left += 1;
    procRect.bottom -= 1;
    procRect.top += 1;
    procRect.right = procRect.left + rect.Width() * nProcess / 100;
    CBrush brush(RGB(126,184,231));
    pDC->FillRect(&procRect, &brush);

    CString str;
    str.Format(_T("%d%%"), nProcess);

    if (!str.IsEmpty())
    {
        UINT nFormat = DT_VCENTER | DT_SINGLELINE | DT_CENTER;

        pDC->SetBkMode(TRANSPARENT);
        pDC->SetTextColor(crText);
        pDC->SetBkColor(crBkgnd);
        pDC->DrawText(str, &rect, nFormat);
    }
}
//添加消息响应函数
void CMainDialog::OnNMCustomdrawList(NMHDR *pNMHDR, LRESULT *pResult)
{
//    LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
    // TODO: 在此添加控件通知处理程序代码
    NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

    // Take the default processing unless we set this to something else below.
    *pResult = CDRF_DODEFAULT;

    // First thing - check the draw stage. If it's the control's prepaint
    // stage, then tell Windows we want messages for every item.

    if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)
    {
        *pResult = CDRF_NOTIFYITEMDRAW;
    }
    else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
    {
        // This is the notification message for an item.  We'll request
        // notifications before each subitem's prepaint stage.

        *pResult = CDRF_NOTIFYSUBITEMDRAW;
    }
    else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))
    {
        // This is the prepaint stage for a subitem. Here's where we set the
        // item's text and background colors. Our return value will tell
        // Windows to draw the subitem itself, but it will use the new colors
        // we set here.

        int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);
        int nSubItem = pLVCD->iSubItem;

        if(nSubItem != 4)  //这里我只重绘第4列
            return;

        COLORREF crText  = ::GetSysColor(COLOR_WINDOWTEXT);
        COLORREF crBkgnd = ::GetSysColor(COLOR_WINDOWTEXT);

        CDC* pDC = CDC::FromHandle(pLVCD->nmcd.hdc);
        CRect rect;
        m_list.GetSubItemRect(nItem, nSubItem, LVIR_BOUNDS, rect);
        if (m_list.GetItemState(nItem, LVIS_SELECTED))
            DrawText(nItem, nSubItem, pDC, ::GetSysColor(COLOR_HIGHLIGHT),
            ::GetSysColor(COLOR_HIGHLIGHT), rect);
        else
            DrawText(nItem, nSubItem, pDC, crText, crBkgnd, rect);

        *pResult = CDRF_SKIPDEFAULT; // We've painted everything.
    }
}

给我留言

留言无头像?