MFC 自绘进度条

http://my.oschina.net/u/131904/blog/55101


从 CProgress类派生出 CTextProgress

CTextProgress::CTextProgress()
{
    m_crProgress = RGB(0,255,0);
    m_crBlank = RGB(255,255,255);
    m_crText = RGB(0,25,255);
}

void CTextProgress::OnPaint() 
{
    //CPaintDC dc(this); // device context for painting
    // TODO: Add your message handler code here
    PAINTSTRUCT ps;
    CDC *pDC = BeginPaint(&ps);
    int nPos = GetPos();
    int nMin,nMax;
    GetRange(nMin,nMax);
    CString csPos;
    csPos.Format("%d%%",nPos*100/(nMax-nMin));
    
    CRect clientRC;
    GetClientRect(clientRC);
    CSize sztext = pDC->GetTextExtent(csPos); //获取字符串的高度和宽度
    int nX = (clientRC.Width() - sztext.cx) /2; //计算中心位置
    int nY = (clientRC.Height() -sztext.cy) /2;
    pDC->SetBkMode(TRANSPARENT);
    
    
    //获取单位刻度
    double dFraction = (double) clientRC.Width()/(nMax-nMin);
    int nLeft = nPos * dFraction; //计算左边距
    CRect leftRC = clientRC;
    leftRC.right = nLeft;
    CRect rightRC = clientRC;
    rightRC.left = nLeft;
    
    pDC->FillRect(leftRC,&CBrush(m_crProgress));
    pDC->FillRect(rightRC,&CBrush(m_crBlank));
    pDC->SetTextColor(m_crText);
    pDC->TextOut(nX,nY,csPos);
    ReleaseDC(pDC);
    EndPaint(&ps);
    // Do not call CProgressCtrl::OnPaint() for painting messages
}


你可能感兴趣的:(MFC 自绘进度条)