⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 progressex.cpp

📁 一个学生考试成绩管理的半成品
💻 CPP
字号:
// ProgressEx.cpp : 实现文件
//

#include "stdafx.h"
#include "ScoreGather.h"

#ifndef _MEMDC_H_
class CMemDC : public CDC
{
public:
    CMemDC(CDC* pDC) : CDC()
    {
        ASSERT(pDC != NULL);

        m_pDC = pDC;
        m_pOldBitmap = NULL;
        m_bMemDC = !pDC->IsPrinting();
              
        if (m_bMemDC)    // Create a Memory DC
        {
            pDC->GetClipBox(&m_rect);
			CreateCompatibleDC(pDC);
            m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
            m_pOldBitmap = SelectObject(&m_bitmap);
            SetWindowOrg(m_rect.left, m_rect.top);
        }
        else        // Make a copy of the relevent parts of the current DC for printing
        {
            m_bPrinting = pDC->m_bPrinting;
            m_hDC       = pDC->m_hDC;
            m_hAttribDC = pDC->m_hAttribDC;
        }
    }
 // Destructor copies the contents of the mem DC to the original DC
    ~CMemDC()
    {
        if (m_bMemDC) 
        {    
            // Copy the offscreen bitmap onto the screen.
            m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
                          this, m_rect.left, m_rect.top, SRCCOPY);

            //Swap back the original bitmap.
            SelectObject(m_pOldBitmap);
         } else
		{
            m_hDC = m_hAttribDC = NULL;
        }
    }

    // Allow usage as a pointer
    CMemDC* operator->() {return this;}
 // Allow usage as a pointer
    operator CMemDC*() {return this;}

private:
    CBitmap  m_bitmap;      // Offscreen bitmap
    CBitmap* m_pOldBitmap;  // bitmap originally found in CMemDC
    CDC*     m_pDC;         // Saves CDC passed in constructor
    CRect    m_rect;        // Rectangle of drawing area.
    BOOL     m_bMemDC;      // TRUE if CDC really is a Memory DC.
};

#endif
// CProgressEx

IMPLEMENT_DYNAMIC(CProgressEx, CProgressCtrl)
CProgressEx::CProgressEx()
{
	m_nPos            = 0;
    m_nStepSize        = 1;
    m_nMax            = 100;
    m_nMin            = 0;
    m_bShowText        = TRUE;
    m_strText.Empty();
    m_colFore        = RGB(38,255,168);
    m_colBk            = RGB(0xE6,0xE6,0xFA);
    m_colTextFore    = RGB(0,0,255);
    m_colTextBk        = RGB(0,0,255);

    m_nBarWidth = -1;
}

CProgressEx::~CProgressEx()
{
}


BEGIN_MESSAGE_MAP(CProgressEx, CProgressCtrl)
	ON_WM_ERASEBKGND()
    ON_WM_PAINT()
    ON_WM_SIZE()
END_MESSAGE_MAP()



// CProgressEx 消息处理程序

LRESULT CProgressEx::OnSetText(UINT, LPCTSTR szText)
{
    LRESULT result = Default();

    if ( (!szText && m_strText.GetLength()) || (szText && (m_strText != szText))   )
    {
        m_strText = szText;
        Invalidate();
    }
    return result;
}

LRESULT CProgressEx::OnGetText(UINT cchTextMax, LPTSTR szText)
{
    if (!_tcsncpy(szText, m_strText, cchTextMax))
        return 0;
    else 
        return min(cchTextMax, (UINT) m_strText.GetLength());
}

BOOL CProgressEx::OnEraseBkgnd(CDC* pDC) 
{    
	return TRUE;
}

void CProgressEx::OnSize(UINT nType, int cx, int cy) 
{
    CProgressCtrl::OnSize(nType, cx, cy);
    m_nBarWidth    = -1;   // Force update if SetPos called
}

void CProgressEx::OnPaint() 
{
    if (m_nMin >= m_nMax) 
        return;

    CRect LeftRect, RightRect, ClientRect;
    GetClientRect(ClientRect);

    double Fraction = (double)(m_nPos - m_nMin) / ((double)(m_nMax - m_nMin));

    CPaintDC PaintDC(this); // device context for painting
    CMemDC dc(&PaintDC);
    //CPaintDC dc(this);    // device context for painting (if not double buffering)

    LeftRect = RightRect = ClientRect;

    LeftRect.right = LeftRect.left + (int)((LeftRect.right - LeftRect.left)*Fraction);
    dc.FillSolidRect(LeftRect, m_colFore);

    RightRect.left = LeftRect.right;
    dc.FillSolidRect(RightRect, m_colBk);

    if (m_bShowText)
    {
        CString str;
        if (m_strText.GetLength())
            str = m_strText;
        else
            str.Format("%d%%", (int)(Fraction*100.0));

        dc.SetBkMode(TRANSPARENT);

        CRgn rgn;
        rgn.CreateRectRgn(LeftRect.left, LeftRect.top, LeftRect.right, LeftRect.bottom);
        dc.SelectClipRgn(&rgn);
        dc.SetTextColor(m_colTextBk);

        dc.DrawText(str, ClientRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

        rgn.DeleteObject();
        rgn.CreateRectRgn(RightRect.left, RightRect.top, RightRect.right, RightRect.bottom);
        dc.SelectClipRgn(&rgn);
        dc.SetTextColor(m_colTextFore);

        dc.DrawText(str, ClientRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    }
}

void CProgressEx::SetForeColour(COLORREF col)
{
    m_colFore = col;
}

void CProgressEx::SetBkColour(COLORREF col)
{
    m_colBk = col;
}

void CProgressEx::SetTextForeColour(COLORREF col)
{
    m_colTextFore = col;
}

void CProgressEx::SetTextBkColour(COLORREF col)
{
    m_colTextBk = col;
}

COLORREF CProgressEx::GetForeColour()
{
    return m_colFore;
}

COLORREF CProgressEx::GetBkColour()
{
    return m_colBk;
}

COLORREF CProgressEx::GetTextForeColour()
{
    return m_colTextFore;
}

COLORREF CProgressEx::GetTextBkColour()
{
    return m_colTextBk;
}
/////////////////////////////////////////////////////////////////////////////
// CProgressEx message handlers

void CProgressEx::SetShowText(BOOL bShow)
{ 
    if (::IsWindow(m_hWnd) && m_bShowText != bShow)
        Invalidate();

    m_bShowText = bShow;
}


void CProgressEx::SetRange(int nLower, int nUpper)
{
    m_nMax = nUpper;
    m_nMin = nLower;
}

int CProgressEx::SetPos(int nPos) 
{    
    if (!::IsWindow(m_hWnd))
        return -1;

    int nOldPos = m_nPos;
    m_nPos = nPos;

    CRect rect;
    GetClientRect(rect);

    double Fraction = (double)(m_nPos - m_nMin) / ((double)(m_nMax - m_nMin));
    int nBarWidth = (int) (Fraction * rect.Width());

    if (nBarWidth != m_nBarWidth)
    {
        m_nBarWidth = nBarWidth;
        RedrawWindow();
    }

    return nOldPos;
}

int CProgressEx::StepIt() 
{    
   return SetPos(m_nPos + m_nStepSize);
}

int CProgressEx::OffsetPos(int nPos)
{
    return SetPos(m_nPos + nPos);
}

int CProgressEx::SetStep(int nStep)
{
    int nOldStep = nStep;
    m_nStepSize = nStep;
    return nOldStep;
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -