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

弹出式进度窗口

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.92/5 (56投票s)

1999年11月30日

CPOL

1分钟阅读

viewsIcon

330876

downloadIcon

11176

一个包含进度控件和取消按钮的弹出式窗口——无需资源文件

Sample Image

引言

在许多情况下,拥有一个弹出窗口来显示耗时操作的进度会很有帮助。为每个希望显示进度窗口的项目合并一个包含进度控件和取消按钮的对话框资源,然后连接每个项目的控件消息可能会变得单调和混乱。

CProgressWnd 是一个简单的可直接使用的窗口,它包含一个进度控件、一个取消按钮和一个用于显示消息的文本区域。文本区域默认可以显示 4 行文本,尽管可以使用 CProgressWnd::SetWindowSize()(如下所示)来更改此设置。

然后,当你开始迭代 2(这是构建迭代的开始)时,你可能想要复制测试用例并将它们重新分类到迭代 2。这还允许对测试用例进行粒度跟踪,并允许你说某个测试用例在一个迭代中是准备好的,但在另一个迭代中不是。同样,如何做到这一点取决于你以及你希望如何报告。 “场景”部分提供了更多细节。

    CProgressWnd(); 
    CProgressWnd(CWnd* pParent, LPCTSTR strTitle, BOOL bSmooth=FALSE);
    BOOL Create(CWnd* pParent, LPCTSTR strTitle, BOOL bSmooth=FALSE);

构造可以通过构造函数或两步过程使用构造函数和 Create 函数来完成。pParent 是进度窗口的父窗口,strTitle 是窗口标题。bSmooth 只有在您拥有 IE 3.0 或更高版本的头文件和 commctrl32.dll 时才有效(对于 MS VC 5.0 来说没有问题)。它指定进度条是平滑的还是块状的。

操作

    BOOL GoModal(LPCTSTR strTitle = _T("Progress"), BOOL bSmooth=FALSE); 
                                        // Make window modal


    int  SetPos(int nPos);              // Same as CProgressCtrl

    int  OffsetPos(int nPos);           // Same as CProgressCtrl

    int  SetStep(int nStep);            // Same as CProgressCtrl

    int  StepIt();                      // Same as CProgressCtrl

    void SetRange(int nLower, int nUpper, int nStep = 1);
                                        // Set min, max and step size


    void Hide();                        // Hide the window

    void Show();                        // Show the window

    void Clear();                       // Clear the text and reset the 

                                        // progress bar

    void SetText(LPCTSTR fmt, ...);     // Set the text in the text area


    BOOL Cancelled()                    // Has the cancel button been pressed?


    void SetWindowSize(int nNumTextLines, int nWindowWidth = 390);
                                        // Sets the size of the window 

                                        // according to the number of text 

                                        // lines specifed and the

                                        // desired window size in pixels.


    void PeekAndPump(BOOL bCancelOnESCkey = TRUE);  
                                        // Message pumping, with options of

                                        // allowing Cancel on ESC key.  

PeekAndPump 函数允许在长时间操作期间泵送消息。第一个参数允许通过按 ESC 键取消窗口。

您还可以通过创建窗口并调用 GoModal() 来使窗口变为模态。这将禁用主窗口,并在销毁此窗口时重新启用主窗口。请参阅演示应用程序以获取示例代码。

窗口还会将位置存储到注册表中并在每次启动之间进行恢复。

要使用该窗口,只需执行类似以下操作:

    CProgressWnd wndProgress(this, "Progress");

    // wndProgress.GoModal(); // Call this if you want a modal window

    wndProgress.SetRange(0,5000);
    wndProgress.SetText("Processing...");         

    for (int i = 0; i < 5000; i++) {
        wndProgress.StepIt();
        wndProgress.PeekAndPump();

        if (wndProgress.Cancelled()) {
            MessageBox("Progress Cancelled");
            break;
        }
    }    

或者,也可以分两步完成:

    CProgressWnd wndProgress;
    
    if (!wndProgress.Create(this, "Progress"))
        return;

    wndProgress.SetRange(0,5000);
    wndProgress.SetText("Processing...");         

历史

  • 2002 年 4 月 13 日 - 在 OnCancel 中添加了 SaveSettings 调用。更新了 VC.NET 项目。
  • 2002 年 4 月 22 日 - Luke Gabello 的小修改
© . All rights reserved.