ProgressCtrlST位图进度条控件类

来源:互联网 发布:朗读软件手机版 编辑:程序博客网 时间:2024/06/05 15:05


翻译来源codeproject:https://www.codeproject.com/Articles/2840/CProgressCtrlST

请自行到codeproject下载,最后有我写的案例及改善后的class

位图进度条控制

  • 下载演示项目 - 237 Kb
  • 下载源 - 5 Kb

样品图像

SoftechSoftware主页


一、介绍

CProgressCtrlST是从MFC CProgressCtrl派生的类。使用这个类,您的应用程序可以使用位图进行条,如许多现代安装程序和游戏中所见!

CProgressCtrlST功能:

  • 使用方便
  • 标准CProgressCtrl方法
  • 支持256+颜色位图
  • 支持负范围
  • 支持垂直进度控制
  • 包含完整的源代码!
  • 现有应用中无需成本实现

二、如何在您的应用程序中集成CProgressCtrlST

1.在您的项目中包括以下文件:

  • ProgressCtrlST.h
  • ProgressCtrlST.cpp
您还需要一个位图来绘制进度条。例如包括在您的项目中Tile.bmp,并将其 称为IDB_TILE使用对话框编辑器创建一个称为进度控件,例如 IDC_TILED

2.然后为此进度控件创建一个成员变量:

CProgressCtrlST m_progressTiled;

3.现在将进度控件附加到CProgressCtrlST对于基于对话的应用程序,在 OnInitDialog中

// Call the base-class methodCDialog::OnInitDialog();// Create the IDC_TILED progress controlm_progressTiled.SubclassDlgItem(IDC_TILED, this);
或在您的DoDataExchange中(一般采用此方法):
// Call the base methodCDialog::DoDataExchange(pDX);// Create the IDC_TILED progress controlDDX_Control(pDX, IDC_TILED, m_progressTiled);
控件将具有与基本MFC类CProgressCtrl中相同的默认范围和起始位置 可以使用与基类相同的方法来修改这些值,例如,SetRangeSetPos此时OffsetPos不支持。

默认情况下,控件将像正常的进度条一样绘制自己。可以分配位图来获取进度条,而不是正常的标准块!

该位图将被平铺以绘制进度条的必要部分; 它不会以任何方式收缩或扩大。

4.为进度条分配位图:

// Assign a bitmapm_progressTiled.SetBitmap(IDB_TILE);
您的进度控制现在是CProgressCtrlST

三、类方法

SetBitmap
设置用于绘制进度条的位图。

// Parameters://     [IN]   nBitmap//            Resource ID of the bitmap to use as background.//            Pass NULL to remove any previous bitmap.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     PROGRESSST_OK//        Function executed successfully.//     PROGRESSST_INVALIDRESOURCE//        The resource specified cannot be found or loaded.//DWORD SetBitmap(int nBitmap, BOOL bRepaint = TRUE)
SetBitmap
设置用于绘制进度条的位图。
// Parameters://     [IN]   hBitmap//            Handle to the bitmap to use as background.//            Pass NULL to remove any previous bitmap.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     PROGRESSST_OK//        Function executed successfully.//     PROGRESSST_INVALIDRESOURCE//        The resource specified cannot be found or loaded.//DWORD SetBitmap(HBITMAP hBitmap, BOOL bRepaint = TRUE)
SetRange
设置进度条控件的范围的上限和下限,并重绘条以反映新的范围。
// Parameters://     [IN]   nLower//            Specifies the lower limit of the range (default is zero).//     [IN]   nUpper//            Specifies the upper limit of the range (default is 100).//void SetRange(int nLower, int nUpper)
SetStep
指定进度条控件的步进增量。
步进增量是对StepIt的调用增加进度条的当前位置的量。
// Parameters://     [IN]   nStep//            New step increment.//// Return value://     The previous step increment.//int SetStep(int nStep)
SetPos
设置进度条控件由nPos指定的当前位置,并重新绘制栏以反映新位置。
进度条控制的位置不是屏幕上的物理位置,而是位于SetRange中指示的上下范围之间。
// Parameters://     [IN]   nPos//            New position of the progress bar control.//// Return value://     The previous position of the progress bar control.//int SetPos(int nPos)
StepIt
进展由步长增量进度栏控件的当前位置和重绘棒,以反映新的位置。
步增量由SetStep方法设置。
// Return value://     The previous position of the progress bar control.//int StepIt()
OnDrawText
每次重绘进度条时都会调用此函数。
这是一个虚拟函数,让派生类进行自定义绘图。
默认实现什么都不做。
// Parameters://     [IN]   pDC//            Pointer to the device context.//     [IN]   nPercentage//            Current percentage of the progress bar.//     [IN]   rcCtrl//            A CRect object that indicates the dimensions of the entire control.//     [IN]   rcProgress//            A CRect object that indicates the dimensions of the currently displayed bar.//     [IN]   bVertical//            TRUE if the progress is vertical, otherwise FALSE.//virtual void OnDrawText(CDC* pDC, int nPercentage, CRect rcCtrl, CRect rcProgress, BOOL bVertical)
GetVersionI
将类版本作为一个简短的值返回。
// Return value://     Class version. Divide by 10 to get actual version.//static short GetVersionI()

GetVersionC

返回类版本作为字符串值。

// Return value://     Pointer to a null-terminated string containig the class version.//static LPCTSTR GetVersionC()

四、案例

关于讨论区提到的内存泄漏问题,需要卸载相应对象。案例ProgressCtrlST类已修改...

OnPaint() 
{
..

CRect rcFullCtrl;

GetClientRect(rcFullCtrl);
OnDrawText(&dc, nPercentage, rcFullCtrl, rcCtrl, bVertical);

// need this lines...
DeleteDC(hdcMem);
DeleteDC(hdcTemp);
DeleteObject(hbmpTemp);

}

src+vs2015案例:http://download.csdn.net/detail/greless/9869264