65.9K
CodeProject 正在变化。 阅读更多。
Home

CSkinProgress

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.97/5 (97投票s)

2003年5月25日

Zlib

9分钟阅读

viewsIcon

558750

downloadIcon

14130

状态栏中的位图进度条,带可选的完成状态消息。

Dual Preview - 01-DualPreview.png - 600 x 252 - 53049 bytes

引言

最初是基于 Chris Maunder 在 CProgressBar 上的工作开发的。这个 CSkinProgress 类旨在用于需要进行一些计算的任何地方。它具有以下特点:

  • 位图进度条(默认,来自资源 - 只需少量代码修改 - 或来自外部源)
  • 进度条自动调整大小
  • 文本自动调整大小
  • 进度条平移
  • 可选的完成状态消息(百分比或计时)
  • 以及更多...

所有功能都可以在运行时更改

  • 根据您的心情或需求实时切换不同的位图(正常模式,警报条等)
  • 更改文本以显示正在计算的进程的不同状态
  • 如果提供更高的精度,则通过重新采样更改进度条的范围
  • 以及更多...

现在是功能解释...

特点

1. 模式

CSkinProgress 允许在状态栏或对话框中创建进度条。这取决于您将使用的构造函数以及您将提供给 CSkinProgress 的参数。

状态栏

Status Bar - 02-StatusBar.png - 14868 bytes - 600 x 365

CSkinProgress
(
  LPCTSTR  i_poStrMessage,
  int      i_nUpper       = 100,
  int      i_nProgress    = cSPT_PERCENT,
  int      i_nPane        = 0,
  int      i_nSize        = 200,
  CBitmap* i_poBitmap     = NULL
#ifdef dCSP_SLIDERBAR_METHOD
 ,BOOL     i_bReverse     = false
#endif // dCSP_SLIDERBAR_METHOD

);

使用此构造函数,您可以创建带有文本、完成状态、选择窗格、位图以及进度条是否从末尾显示的进度条。

对话框

Dialog - 03-Dialog.png - 14139 bytes - 600 x 367

#ifdef dCSP_DIALOG_PROGRESS
CSkinProgress
(
  CWnd*    i_poWndProgress,
  int      i_nUpper       = 100, 
  CBitmap* i_poBitmap     = NULL,
#ifdef dCSP_SLIDERBAR_METHOD
  BOOL     i_bReverse     = false,
#endif // dCSP_SLIDERBAR_METHOD

  CWnd*    i_poWndMessage = NULL,
  LPCTSTR  i_poStrMessage = NULL,
  int      i_nProgress    = cSPT_NONE
);
#endif // dCSP_DIALOG_PROGRESS

如果激活了 dCSP_SLIDERBAR_METHOD 定义,您可以使用此构造函数在任何带有父级(重要)的 CWnd 对象中创建对话框中的进度条。您无需提供窗格号,但需要提供一个用于创建进度条的 CWnd 锚点,以及另一个用于显示文本的 CWnd

2. 大小调整

您可以为进度条提供一个大小。此大小在进度条位于状态栏在窗格0中时使用,从而与文本共享。您可以选择3种文本共享模式

正数大小

Positive size - 04-PosSize.png - 11841 bytes - 600 x 120

如果给定大小为正,则进度条的大小为 FIXED。因此,如果空间不足,文本将换行。

零大小

Zero size - 05-ZeroSize.png - 11613 bytes - 600 x 120

如果给定大小为零,则进度条的大小适合文本留下的剩余空间。因此,如果空间不足,文本永远不会换行。这只可能在完成消息占用太多空间时发生。

负数大小

Negative size - 06-NegSize.png - 12516 bytes - 600 x 120

如果给定大小为负,则进度条的大小适合文本留下的剩余空间(如果空间不足),否则使用给定大小。

3. 文本

您可以提供任何您想要的文本(注意:不要太长 - 最多128个字符)。

Text one - 07-TextOne.png - 13150 bytes - 600 x 80

Text two - 08-TextTwo.png - 10867 bytes - 600 x 80

4. 窗格

您可以在状态栏中创建任意数量的窗格,只需遵循以下规则即可...

在字符串表中的 ID_INDICATORS 部分添加一个ID,否则文本将出现在窗格中。文本越长,窗格的宽度就越重要!

String table - 09-StringTable.png - 40318 bytes - 600 x 181

现在将此用户窗格ID添加到“MainFrm.cpp”文件开头的“indicators”数组中。第一个条目是窗格0。

Main Frm - 10-MainFrm.png - 35606 bytes - 600 x 160

然后,选择您将用于显示进度条的窗格号。例如,用户窗格是文本所在的窗格0

Pane 0 - 11-PaneZero.png - 9964 bytes - 600 x 80

Pane 1 - 12-PaneOne.png - 10062 bytes - 600 x 80

您甚至可以使用系统窗格,例如NUM-LOCK指示器所在的窗格3

Pane 3 - 13-PaneThree.png - 9925 bytes - 600 x 80

5. 完成消息

您可以要求 CSkinProgress 在文本末尾添加一条消息,以提供当前的完成状态。进度条提供图形信息,但有时获得更多信息很有用。一个 enum 列表具有递增的值,每个值都有不同的含义。以下是不同类型的消息:

  • cSPT_NONE:不添加任何消息,速度很快。

    No message - 14-ProgressNone.png - 10153 bytes - 600 x 80 

  • cSPT_PERCENT:添加一个简单的文本百分比完成报告,比NONE慢一点

    Percent message - 15-ProgressPercent.png - 10154 bytes - 600 x 80

  • cSPT_TIMED:添加一个功能齐全的消息,提供开始日期/时间、当前完成百分比、剩余百分比、剩余时间以及预计结束日期/时间 - 相当慢

    Timed message - 16-ProgressTimed.png - 10335 bytes - 600 x 80 

  • cSPT_AUTOSIZE:根据窗格0中的剩余空间自动选择PERCENT和TIMED之间最佳消息 - 最慢。

    Auto-sized message - 17-ProgressAutoSize.png - 11119 bytes - 600 x 80 

  • NEGATIVE:取消括号(新功能

    Percent message minus - 18-ProgressPercentMinus.png - 6929 bytes - 600 x 80

    您现在可以通过提供负数进度条来删除括号,就像您可以通过为窗格0中的进度条提供负数大小来阻止文本换行一样!

6. 位图

您可以选择默认的嵌入式位图(与 IDB_BMP_AQUA 相同)或使用资源位图(需要更改编译条件并修改 CSkinProgress::SetBitmap(...) 中的代码)

Default bitmap - 19-BitmapDefault.png - 10328 bytes - 600 x 80

您还可以向 CSkinProgress 提供位图

User bitmap - 20-BitmapLuna.png - 9698 bytes - 600 x 80

7. 反向显示

未选中时,进度条从左侧(或垂直进度条的顶部)开始。

Normal display - 21-Normal.png - 10626 bytes - 600 x 80

出于某些原因,您可以选择从进度条的末尾显示它。这对于对话框中的垂直条特别有用,它从底部开始,就像温度计一样。

Reverse display - 22-Reverse.png - 10780 bytes - 600 x 80

代码

构造函数

状态栏

CSkinProgress::CSkinProgress
( // Default constructor

)

使用“Work in progress”和下面给出的默认构造函数值,在状态栏中创建一个默认进度条。

CSkinProgress::CSkinProgress
( // Default constructor with parameters for status bar usage

  LPCTSTR     i_poStrMessage, // Text to display

  int         i_nUpper,       // = 100          : Default range from 0 to 

                              //                  i_nUpper

  int         i_nProgress,    // = cSPT_PERCENT : Message type to add to the 

                              // text

  int         i_nPane,        // = 0            : Pane number in which 

                              // display the progress bar

  int         i_nSize,        // = 200          : Size of the progress bar if 

                              //                  in pane 0

  CBitmap*    i_poBitmap      // = NULL         : Pointer to a user bitmap

#ifdef dCSP_SLIDERBAR_METHOD
 ,BOOL        i_bReverse      // = false        : Reverse display of the 

                              // progress bar

#endif // dCSP_SLIDERBAR_METHOD

)

警告:此构造函数自原始版本 1.00/1.12dev 以来已更改!

最简单的状态栏构造函数是

CSkinProgress::CSkinProgress
( // Default constructor with parameters for status bar usage

  LPCTSTR     i_poStrMessage, // Text to display

)

对话框

#ifdef dCSP_DIALOG_PROGRESS
CSkinProgress::CSkinProgress
( // Default constructor with parameters for dialog usage

  CWnd*       i_poWndProgress,  // Pointer to the anchor CWnd to use for the 

                                // progress bar

  int         i_nUpper,         // = 100,      : Default range from 0 to 

                                // i_nUpper

  CBitmap*    i_poBitmap,       // = NULL      : Pointer to a user bitmap

#ifdef dCSP_SLIDERBAR_METHOD
  BOOL        i_bReverse,       // = false,    : Reverse display of the 

                                //               progress bar

#endif // dCSP_SLIDERBAR_METHOD

  CWnd*       i_poWndMessage,   // = NULL,     : Pointer to the anchor CWnd

                                //               to use for the text pane

  LPCTSTR     i_poStrMessage,   // = NULL      : Text to display,

  int         i_nProgress       // = cSPT_NONE : Message type to add to the

                                //               text

)
#endif // dCSP_DIALOG_PROGRESS

要使用对话框构造函数,请确保已设置 `dCSP_DIALOG_PROGRESS` 定义。您可以绕过文本窗格声明,因为我已将 `i_bReverse` 参数放在函数参数列表的中间。请注意您的编码,您也可以自行使用 `dCSP_SLIDERBAR_METHOD` 定义,以便为您的代码提供条件编译功能。

最简单的状态栏构造函数是

CSkinProgress::CSkinProgress
( // Default constructor with parameters for status bar usage

  CWnd*       i_poWndProgress,  // Pointer to the anchor CWnd to use for the 

                                // progress bar

)

析构函数

CSkinProgress::~CSkinProgress
( // Destructor

)

位图处理

BOOL CSkinProgress::SetBitmap
( // Change of progress bar image

  CBitmap* i_poBitmap           // = NULL : Pointer to an existing bitmap

#ifndef dCSP_TIMED_REDRAW
 ,BOOL     i_bDisplay           // = true : Display the changes

#endif // dCSP_TIMED_REDRAW

)

以下两个附加函数由 CSkinProgress::SetBitmap(...) 使用,以便复制/转换 TRUE/COLOR 位图图片(尚不支持映射颜色位图)。它们可以用于您自己的目的

BOOL CSkinProgress::CopyBitmap
( // Copy a bitmap

  CBitmap* o_poBitmap,          // Pointer to an EXISTING but NOT INITIALIZED 

                                // bitmap

  CBitmap* i_poBitmap           // Pointer to the source bitmap

)
BOOL CSkinProgress::ConvBitmap
( // Convert a bitmap to a specified device context

  CBitmap*    o_poBitmap,       // Pointer to an EXISTING but NOT INITIALIZED 

                                // bitmap

  CBitmap*    i_poBitmap,       // Pointer to the source bitmap

  CDC*        i_poDC            // = NULL : Pointer to the DC to use for the

                                // conversion, if NULL use the current DC

)

进度接口

BOOL CSkinProgress::SetRange
( // Set the new range

  int  i_nLower,                // Minimum limit

  int  i_nUpper,                // Maximum limit

  int  i_nStep,                 // = 1     : Step increment

  BOOL i_bResamble              // = false : Resample the current position 

                                // from the new Lower and Upper values

#ifndef dCSP_TIMED_REDRAW
 ,BOOL i_bDisplay               // = true  : Display the changes

#endif // dCSP_TIMED_REDRAW

)
int CSkinProgress::SetPos
( // Set <M_NRIGHT> value

  int  i_nPos                   // Set a new current position

#ifndef dCSP_TIMED_REDRAW
 ,BOOL i_bDisplay               // = true : Display the changes

#endif // dCSP_TIMED_REDRAW

)
int CSkinProgress::OffsetPos
( // Forward of <NOFFSET> value

  int  i_nOffset                // Add the offset to the current position 

                                // (can be negative)

#ifndef dCSP_TIMED_REDRAW
 ,BOOL i_bDisplay               // = true : Display the changes

#endif // dCSP_TIMED_REDRAW

)
int CSkinProgress::SetStep
( // Set <M_NSTEP> value

  int i_nStep                   // Set the step increment

)             
int CSkinProgress::StepIt
( // Forward of <M_NSTEP> value

#ifndef dCSP_TIMED_REDRAW
  BOOL i_bDisplay               // = true : Display the changes

#endif // dCSP_TIMED_REDRAW

) 

扩展进度接口

BOOL CSkinProgress::SetSize
( // Set size of the progress bar in pane 0 

  int  i_nSize                  // Set the size of the progress bar

#ifndef dCSP_TIMED_REDRAW
 ,BOOL i_bDisplay               // = true : Display the changes

#endif // dCSP_TIMED_REDRAW

)
int CSkinProgress::GetSize
( // Get width or height of the progress bar in pixel, mostly used in 

  // CSkinSlider

)
int CSkinProgress::GetPos
( // Get <M_NRIGHT> value

  BOOL i_bPercent               // = false

)
int CSkinProgress::GetStep
( // Get <M_NSTEP> value

)
int CSkinProgress::GetLower
( // Get <M_NLOWER> value

)
int CSkinProgress::GetUpper
( // Get <M_NUPPER> value

)
#ifdef dCSP_SLIDERBAR_METHOD
int CSkinProgress::SetStart
( // Set <M_NLEFT> value

  int  i_nStart                 // Set a new start position

#ifndef dCSP_TIMED_REDRAW
 ,BOOL i_bDisplay               // = true : Display the changes

#endif // dCSP_TIMED_REDRAW

)
#endif // dCSP_SLIDERBAR_METHOD

#ifdef dCSP_SLIDERBAR_METHOD
int CSkinProgress::GetStart
( // Get <M_NLEFT> value

  BOOL i_bPercent               // = false

)
#endif // dCSP_SLIDERBAR_METHOD

void CSkinProgress::Reset
( // Reset the progress bar

#ifndef dCSP_TIMED_REDRAW
  BOOL i_bDisplay               // = true : Display the changes

#endif // dCSP_TIMED_REDRAW

)

文本接口

BOOL CSkinProgress::SetText
( // Set the new text

  LPCTSTR i_poStrMessage        // New text to display      

#ifndef dCSP_TIMED_REDRAW
 ,BOOL    i_bDisplay            // = true : Display the changes

#endif // dCSP_TIMED_REDRAW

)
BOOL CSkinProgress::SetProgress
( // Set <M_NPROGRESSTEXT> value

  int  i_nProgress              // Set progress text

#ifndef dCSP_TIMED_REDRAW
 ,BOOL i_bDisplay               // = true : Display the changes

#endif // dCSP_TIMED_REDRAW

)
int CSkinProgress::GetProgress
( // Get <M_NPROGRESSTEXT> value

)

更新进程

BOOL CSkinProgress::RefreshPanes
( // Resize the text pane and the progress bar

) 

编译源代码

通过条件编译管理 CSkinProgress 功能!以下是每个编译定义的解释:

  • dCSP_DIALOG_PROGRESS:激活在对话框中使用 CSkinProgress
  • dCSP_TIMED_REDRAW:当设置/取消注释时,不在每个事件时重绘/刷新文本/进度条,而是每秒只刷新50次(足以保证准确性)。在某些循环中,CSkinProgress::StepIt() 每秒可以轻松调用超过50次!
  • dCSP_VERTICAL_BAR:激活完整的垂直条计算。不用于状态栏,但可以在设置 dCSP_DIALOG_PROGRESS 时使用
  • dCSP_SLIDERBAR_METHOD:将进度条转换为完整的滑块条。允许条反向显示,因此如果要使用 CSkinProgress 的“反向”功能,请取消注释此定义

特定的定义用于非常实验性的目的,请谨慎使用它们

  • dCSP_RESOURCE_BITMAP:使用资源位图作为默认位图。资源位图的当前 ID 名称是 IDB_AQUABAR,但您可以通过修改 CSkinProgress::SetBitmap(...) 中的资源位图名称来更改此名称
  • dCSP_CREATE_BITMAP_FILE:CSkinProgress::SetBitmap(...) 结束时,在 C 盘根目录创建源文件!用于更改默认嵌入式位图:创建一个 CSkinProgress 并提供用户位图,您将获得转换后的位图到源文件中!
  • dCSP_DISPLAY_STRETCH:显示用于创建图像列表的拉伸图片。除了文档或验证 CSkinProgress::SetBitmap(...) 函数之外,此功能几乎没有用处

进度条位图

现在,为了完成对 CSkinProgress 图形引擎的简短介绍,我们来概述一下用于创建进度条图片的方法。一个位图被提供给 CSkinProgress::SetBitmap(...)...

Photon bar - 23-Photon.png - 389 bytes - 180 x 32

此位图分为9个部分...

Bitmap cutted into pieces - 24-PhotonCutted.png - 557 bytes - 180 x 32

从左到右,以下是含义和关联的 ID 定义

  • cSPB_START:进度条背景的开始
  • cSPB_BEFORE:条形图开始前的元素,可以是高光
  • cSPB_LEFT:条形的左侧部分,其开头
  • cSPB_CENTER:进度条的中心部分,可解释为抓取结
  • cSPB_BAR:条形主体
  • cSPB_RIGHT:条形的右侧部分,其末尾
  • cSPB_AFTER:条形末尾后的元素,可以是阴影
  • cSPB_BACKGROUND:进度条背景
  • cSPB_END:进度条背景的结束

要创建您自己的位图皮肤,请制作任何具有如上所述9个部分的位图。位图的高度并不重要(顺便说一句不要太大 - 当然不超过32像素),宽度应为9的倍数(重要),因此:宽度 % 9 = 0!看看奖励/隐藏位图,了解 CSkinProgress 如何让您制作有趣的东西 ;)

关键元素的相对位置是根据进度条的范围及其当前位置计算的。然后,一旦将这些值作为百分比获取,就计算这些关键元素的绝对位置。现在只需按照定义的顺序将它们放置在进度条图像上,以便某些元素可以隐藏其他元素,就像在 Paint Shop Pro 中的图层一样。

蓝色列是元素的精确位置。
红色列是基于元素宽度的基准列。

Pseudo layers - 25-PhotonParts.png - 37743 bytes - 600 x 450

这三个/四个字母是 `CSkinProgress::OnPaint()` 函数中注释的关键,方便您轻松找到它们...

版本历史

修订版 1.02:2003 年 7 月 12 日 14:01:53 (Kochise)

基准:CSkinProgress 1.32dev
    提取文件:无错误
    解析文件:无错误
    差异引擎:无错误
    修改.public.constructor:CSkinProgress
    触碰.protected.function:CreateCommon
    修改.protected.function:ProgressInStatusBar
    修改.protected.function:ProgressInDialog
    修改.public.function:SetRange
    触碰.public.function:SetPos
    修改.public.function:GetPos
    触碰.public.function:SetStart
    修改.public.function:GetStart
    修改.public.function:Reset
    添加.public.function:SetProgress
    添加.public.function:GetProgress
    修改.public.function:RefreshPanes
    触碰.protected.function:CompactText
    修改.protected.function:GetTimed
    修改.protected.function:UpdateProgress
    修改.protected.function:OnPaint
    编译项目文件:无错误
    打包文件:无错误

修订版 1.01:2003 年 7 月 6 日 22:08:37 (Kochise)

基准:CSkinProgress 1.28 Devel
    提取文件:无错误
    解析文件:无错误
    差异引擎:无错误
    修改.public.constructor:CSkinProgress
    添加.public.constructor:CSkinProgress
    修改.public.function:SetBitmap
    修改.public.function:ConvBitmap
    修改.public.function:SetRange
    修改.public.function:SetPos
    修改.public.function:OffsetPos
    修改.public.function:StepIt
    修改.public.function:SetSize
    添加.public.function:GetSize
    添加.public.function:GetPos
    添加.public.function:GetStep
    添加.public.function:GetLower
    添加.public.function:GetUpper
    添加.public.function:SetStart
    添加.public.function:GetStart
    添加.public.function:Reset
    修改.public.function:SetText
    变异.function:protected.ResizeTextPane->public.RefreshPanes
    修改.public.function:RefreshPanes
    变异.function:public.Clear->protected.Clear
    添加.protected.function:GetTargetRect
    变异.protected.function:Create->CreateCommon
    修改.protected.function:CreateCommon
    添加.protected.function:ProgressInStatusBar
    添加.protected.function:ProgressInDialog
    修改.protected.function:CompactText
    添加.protected.function:UpdateProgress
    修改.protected.function:OnPaint
    编译项目文件:无错误
    打包文件:无错误

修订版 1.00:2003 年 5 月 25 日 13:25:12 (Kochise)

第一个存档版本并发布到 CodeProject。
基准:CSkinProgress 1.12 Devel
    添加.public.constructor:CSkinProgress
    添加.public.constructor:CSkinProgress
    添加.public.function:SetRange
    添加.public.function:SetText
    添加.public.function:SetSize
    添加.public.function:SetBitmap
    添加.public.function:CopyBitmap
    添加.public.function:ConvBitmap
    添加.public.function:SetPos
    添加.public.function:OffsetPos
    添加.public.function:SetStep
    添加.public.function:StepIt
    添加.public.function:Clear
    添加.public.destructor:~CSkinProgress
    添加.protected.function:Create
    添加.protected.function:GetStatusBar
    添加.protected.function:ResizeTextPane
    添加.protected.function:CompactText
    添加.protected.function:GetTimed
    添加.protected.function:OnEraseBkgnd
    添加.protected.function:OnPaint
    添加.protected.function:OnSizing
    添加.protected.function:OnSize
    添加.protected.function:OnTimer
    编译项目文件:无错误
    打包文件:无错误
© . All rights reserved.