zoukankan      html  css  js  c++  java
  • 自绘CProgressCtrl进度条控件,支持自定义显示文本和进程百分比信息

    [cpp] view plain copy
     
    1. // CXProgressCtrl 头文件  
    2.   
    3. #pragma once  
    4.   
    5. // CXProgressCtrl  
    6.   
    7. class CXProgressCtrl : public CProgressCtrl  
    8. {  
    9.     DECLARE_DYNAMIC(CXProgressCtrl)  
    10.   
    11. public:  
    12.     CXProgressCtrl();  
    13.     virtual ~CXProgressCtrl();  
    14.   
    15.     typedef enum _ALIGN_TEXT_  
    16.     {  
    17.         ALIGN_LEFT = 0x00,  
    18.         ALIGN_CENTER,  
    19.         ALIGN_RIGHT  
    20.     }ALIGN_TEXT;  
    21.   
    22. public:  
    23.     inline void SetBarColor(COLORREF clr)  
    24.     {  
    25.         m_clrBarColor = clr;  
    26.     }  
    27.   
    28.     inline COLORREF GetBarColor()  
    29.     {  
    30.         return m_clrBarColor;  
    31.     }  
    32.   
    33.     inline void SetBarBkColor(COLORREF clr)  
    34.     {  
    35.         m_clrBarBkColor = clr;  
    36.     }  
    37.   
    38.     inline COLORREF GetBarBkColor()  
    39.     {  
    40.         return m_clrBarBkColor;  
    41.     }  
    42.   
    43.     inline void SetTextColor(COLORREF clr)  
    44.     {  
    45.         m_clrTextColor = clr;  
    46.     }  
    47.   
    48.     inline COLORREF GetTextColor()  
    49.     {  
    50.         return m_clrTextColor;  
    51.     }  
    52.   
    53.     inline void SetTextBkColor(COLORREF clr)  
    54.     {  
    55.         m_clrTextBkColor = clr;  
    56.     }  
    57.   
    58.     inline COLORREF GetTextBkColor()  
    59.     {  
    60.         return m_clrTextBkColor;  
    61.     }  
    62.   
    63.     inline void SetTextAlign(ALIGN_TEXT AlignText)  
    64.     {  
    65.         m_AlignText = AlignText;  
    66.     }  
    67.   
    68.     inline ALIGN_TEXT GetTextAlign()  
    69.     {  
    70.         return m_AlignText;  
    71.     }  
    72.   
    73.     inline void ShowPercent(BOOL bShowPercent = TRUE)  
    74.     {  
    75.         m_bShowPercent = bShowPercent;  
    76.     }  
    77.   
    78.     inline BOOL IsShowPercent()  
    79.     {  
    80.         return m_bShowPercent;  
    81.     }  
    82.   
    83. protected:  
    84.     COLORREF m_clrBarColor;  
    85.     COLORREF m_clrBarBkColor;  
    86.     COLORREF m_clrTextColor;  
    87.     COLORREF m_clrTextBkColor;  
    88.     ALIGN_TEXT m_AlignText;  
    89.     BOOL m_bShowPercent;  
    90.     CFont m_font; // Only for vertical style  
    91.   
    92. protected:  
    93.     afx_msg BOOL OnEraseBkgnd(CDC* pDC);  
    94.     afx_msg void OnPaint();  
    95.   
    96.     DECLARE_MESSAGE_MAP()  
    97. };  
    [cpp] view plain copy
     
    1. // CXProgressCtrl .CPP文件  
    2. // XProgressCtrl.cpp : implementation file  
    3. //  
    4.   
    5. #include "stdafx.h"  
    6. #include "Demo.h"  
    7. #include "XProgressCtrl.h"  
    8.   
    9. // CXProgressCtrl  
    10.   
    11. IMPLEMENT_DYNAMIC(CXProgressCtrl, CProgressCtrl)  
    12.   
    13. CXProgressCtrl::CXProgressCtrl() : m_clrTextColor(RGB(0, 0, 0)),  
    14. m_clrTextBkColor(RGB(255, 255, 255)),  
    15. m_clrBarColor(RGB(128, 128, 255)),  
    16. m_clrBarBkColor(RGB(235, 235, 235)),  
    17. m_AlignText(ALIGN_CENTER),   
    18. m_bShowPercent(TRUE)  
    19. {  
    20.       
    21. }  
    22.   
    23. CXProgressCtrl::~CXProgressCtrl()  
    24. {  
    25.     if(m_font.GetSafeHandle())  
    26.     {  
    27.         m_font.DeleteObject();  
    28.     }  
    29. }  
    30.   
    31.   
    32. BEGIN_MESSAGE_MAP(CXProgressCtrl, CProgressCtrl)  
    33.     ON_WM_ERASEBKGND()  
    34.     ON_WM_PAINT()  
    35. END_MESSAGE_MAP()  
    36.   
    37. // CXProgressCtrl message handlers  
    38. BOOL CXProgressCtrl::OnEraseBkgnd(CDC *pDC)  
    39. {  
    40.     return TRUE;  
    41. }  
    42.   
    43. void CXProgressCtrl::OnPaint()  
    44. {  
    45.     CPaintDC paintDC(this);  
    46.   
    47.     int nMin = 0;  
    48.     int nMax = 0;  
    49.   
    50.     GetRange(nMin, nMax);  
    51.     ASSERT(nMin <= nMax);  
    52.   
    53.     int nPos = GetPos();  
    54.     ASSERT(nPos >= nMin && nPos <= nMax);  
    55.   
    56.     DWORD dwStyle = GetStyle();  
    57.   
    58.     BOOL bVertical = FALSE;  
    59.     if(dwStyle & PBS_VERTICAL)  
    60.     {  
    61.         bVertical = TRUE;  
    62.     }  
    63.   
    64.     CDC dc;  
    65.     dc.CreateCompatibleDC(&paintDC);  
    66.     ASSERT(dc.GetSafeHdc());  
    67.   
    68.     CRect rect;  
    69.     GetClientRect(&rect);  
    70.   
    71.     CBitmap bmp;  
    72.     bmp.CreateCompatibleBitmap(&paintDC, rect.Width(), rect.Height());  
    73.     ASSERT(bmp.GetSafeHandle());  
    74.   
    75.     CBitmap* pOldBitmap = (CBitmap*)dc.SelectObject(&bmp);  
    76.   
    77.     CFont* pOldFont = NULL;  
    78.     CWnd* pParent = GetParent();      
    79.     ASSERT(pParent);  
    80.   
    81.     CFont* pFont = pParent->GetFont();  
    82.     ASSERT(pFont);  
    83.     if(bVertical)  
    84.     {  
    85.         if(NULL == m_font.GetSafeHandle())  
    86.         {  
    87.             LOGFONT lf = {0};  
    88.             pFont->GetLogFont(&lf);  
    89.             lf.lfEscapement = 900;  
    90.             m_font.CreateFontIndirect(&lf);  
    91.         }  
    92.         ASSERT(m_font.GetSafeHandle());  
    93.         pOldFont = (CFont*)dc.SelectObject(&m_font);              
    94.     }  
    95.     else  
    96.     {  
    97.         pOldFont = (CFont*)dc.SelectObject(pFont);  
    98.     }  
    99.   
    100.     double dPercent = (double)(nPos - nMin) / ((double)(nMax - nMin));  
    101.   
    102.     dc.DrawEdge(rect, EDGE_SUNKEN, BF_RECT | BF_FLAT);  
    103.   
    104.     CRect rc(rect);  
    105.     rc.DeflateRect(CSize(2, 2));  
    106.     dc.FillSolidRect(&rc, m_clrBarBkColor);  
    107.   
    108.     CString strText(_T(""));  
    109.     GetWindowText(strText);  
    110.   
    111.     if(m_bShowPercent)  
    112.     {  
    113.         strText.AppendFormat(_T("%d%% "), static_cast<int>((dPercent * 100.0) + 0.5));  
    114.     }  
    115.   
    116.     dc.SetBkMode(TRANSPARENT);  
    117.     dc.SetTextColor(m_clrTextColor);  
    118.   
    119.     CPoint pt(0, 0);  
    120.     CSize size = dc.GetOutputTextExtent(strText);  
    121.   
    122.     if(!bVertical)  
    123.     {  
    124.         switch(m_AlignText)  
    125.         {  
    126.         case ALIGN_LEFT:  
    127.             pt.x = rc.left;  
    128.             break;  
    129.   
    130.         case ALIGN_RIGHT:  
    131.             pt.x = rc.right - size.cx;  
    132.             break;  
    133.   
    134.         case ALIGN_CENTER:  
    135.         default:  
    136.             pt.x = rc.left + (rc.Width() - size.cx) / 2;  
    137.             break;  
    138.         }  
    139.         pt.y = rc.top + (rc.Height() - size.cy) / 2;  
    140.   
    141.         CRect rcPos(rc);  
    142.   
    143.         rcPos.right = rcPos.left + (int)(dPercent * rcPos.Width());  
    144.         dc.FillSolidRect(rcPos, m_clrBarColor);  
    145.   
    146.         dc.SetTextColor(m_clrTextColor);  
    147.         dc.ExtTextOut(pt.x, pt.y, ETO_OPAQUE, rcPos, strText, NULL);  
    148.   
    149.         dc.SetTextColor(m_clrTextBkColor);  
    150.         dc.ExtTextOut(pt.x, pt.y, ETO_CLIPPED, &rcPos, strText, NULL);  
    151.     }  
    152.     else  
    153.     {  
    154.         switch(m_AlignText)  
    155.         {  
    156.         case ALIGN_LEFT:  
    157.             pt.y = rc.bottom;  
    158.             break;  
    159.   
    160.         case ALIGN_RIGHT:  
    161.             pt.y = rc.top + size.cx;  
    162.             break;  
    163.   
    164.         case ALIGN_CENTER:  
    165.         default:  
    166.             pt.y = rc.bottom - (rc.Height() - size.cx) / 2;  
    167.             break;  
    168.         }  
    169.         pt.x = rc.left + (rc.Width() - size.cy) / 2;  
    170.   
    171.         CRect rcPos(rc);  
    172.   
    173.         rcPos.top = rcPos.bottom - (int)(dPercent * rcPos.Height());  
    174.         dc.FillSolidRect(rcPos, m_clrBarColor);  
    175.   
    176.         dc.SetTextColor(m_clrTextColor);  
    177.         dc.ExtTextOut(pt.x, pt.y, ETO_OPAQUE, rcPos, strText, NULL);  
    178.   
    179.         dc.SetTextColor(m_clrTextBkColor);  
    180.         dc.ExtTextOut(pt.x, pt.y, ETO_CLIPPED, &rcPos, strText, NULL);  
    181.     }  
    182.       
    183.     paintDC.BitBlt(rect.left, rect.top ,rect.Width(), rect.Height(), &dc, 0, 0, SRCCOPY);  
    184.   
    185.     dc.SelectObject(pOldFont);  
    186.     dc.SelectObject(pOldBitmap);  
    187.     bmp.DeleteObject();  
    188.   
    189.     dc.DeleteDC();  
    190. }  

    效果图如下所示:

    http://blog.csdn.net/visualeleven/article/details/6165007

  • 相关阅读:
    POJ3094 UVALive3594 HDU2734 ZOJ2812 Quicksum【进制】
    UVALive5583 UVA562 Dividing coins
    POJ1979 HDU1312 Red and Black【DFS】
    POJ1979 HDU1312 Red and Black【DFS】
    POJ2386 Lake Counting【DFS】
    POJ2386 Lake Counting【DFS】
    HDU4394 Digital Square
    HDU4394 Digital Square
    UVA213 UVALive5152 Message Decoding
    UVA213 UVALive5152 Message Decoding
  • 原文地址:https://www.cnblogs.com/findumars/p/6005679.html
Copyright © 2011-2022 走看看