Click here to Skip to main content
15,884,353 members
Articles / Desktop Programming / MFC
Article

A Popup Progress Window

Rate me:
Please Sign up or sign in to vote.
4.92/5 (56 votes)
21 Apr 2002CPOL1 min read 307.6K   11.1K   154   59
A popup window containing a progress control and cancel button - no resource file needed

Sample Image

Introduction

There are many occasions where it's nice to have a popup window that shows the progress of a lengthy operation. Incorporating a dialog resource with a progress control and cancel button, then linking up the control messages for every project you wish to have the progress window can get monotonous and messy.

The class CProgressWnd is a simple drop in window that contains a progress control, a cancel button and a text area for messages. The text area can display 4 lines of text as default, although this can be changed using CProgressWnd::SetWindowSize() (below)

Construction

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

Construction is either via the constructor or a two-step process using the constructor and the Create function. pParent is the parent of the progress window,strTitle is the window caption title. bSmooth will only be effective if you have the header files and commctrl32.dll from IE 3.0 or above (no problems for MS VC 5.0). It specifies whether the progress bar will be smooth or chunky.

Operations

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.

The PeekAndPump function allows messages to be pumped during long operations. The first parameter allows the window to be cancelled by pressing the ESC key.

You can also make the window modal by creating the window and calling GoModal(). This will disable the main window, and re-enable the main window when this window is destroyed. See the demo app for example code.

The window will also store and restore its position to and from the registry between incantations.

To use the window, just do something like:

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;
    }
}

or it can be done two stage as:

CProgressWnd wndProgress;

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

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

History

  • 13 Apr 2002 - added SaveSettings call to OnCancel. Updated project for VC.NET.
  • 22 Apr 2002 - minor mods by Luke Gabello

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
QuestionAnother improved class based on this one Pin
mrsilver2-Jun-17 12:41
mrsilver2-Jun-17 12:41 
This ones could run a task and allow the user to cancel it or retry if a timeout reaches and the task is not finished.


Implementation

// ProgressWnd.cpp : implementation file
//
// Written by Chris Maunder (chrismaunder@codeguru.com)
// Copyright 1998-2002
//
// CProgressWnd is a drop-in popup progress window for use in
// programs that a time consuming. Check out the header file
// or the accompanying HTML doc file for details.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed by any means PROVIDING it is not sold for
// profit without the authors written consent, and providing that this
// notice and the authors name is included. If the source code in 
// this file is used in any commercial application then an email to
// the me would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to your
// computer, causes your pet cat to fall ill, increases baldness or
// makes you car start emitting strange noises when you start it up.
//
// Expect bugs.
// 
// Please use and enjoy. Please let me know of any bugs/mods/improvements 
// that you have found/implemented and I will fix/incorporate them into this
// file. 
//
// Updated May 18 1998 - added PeekAndPump function to allow modal operation,
//                       with optional "Cancel on ESC" (Michael <mbh-ep@post5.tele.dk>)
//         Nov 27 1998 - Removed modal stuff from PeekAndPump
//         Dec 18 1998 - added WS_EX_TOPMOST to the creation flag
//         Apr 14 2002 - Added SaveCurrentSettings to OnCancel (Geert Delmeiren)
//         Apr 22 2002 - Minor mods by Luke Gabello
//         Jun 03 2017 - Added derived class to process a task within a time out by Manuel Jiménez

#include "stdafx.h"
#include "ProgressWnd.h"
#include <thread> 
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


#define IDC_CANCEL   10
#define IDC_TEXT     11
#define IDC_PROGRESS 12
#define IDC_RETRY	 13
LPCTSTR szSection = _T("Settings");   
LPCTSTR szEntryX  = _T("X");
LPCTSTR szEntryY  = _T("Y");

/////////////////////////////////////////////////////////////////////////////
// CProgressWnd

CProgressWnd::CProgressWnd()
{
    CommonConstruct();
}

CProgressWnd::CProgressWnd(CWnd* pParent, LPCTSTR pszTitle, BOOL bSmooth /* = FALSE */, BOOL bEnableRetry /*= FALSE*/)
{
    CommonConstruct();
    m_strTitle = pszTitle;
	m_bEnableRetry = bEnableRetry;
    Create(pParent, pszTitle, bSmooth);
}

void CProgressWnd::CommonConstruct()
{

    m_wRenenableWnd  = NULL;

    m_nNumTextLines  = 4;
    m_nPrevPos       = 0;
    m_nPrevPercent   = 0;
    m_nStep          = 1;
    m_nMinValue      = 0;
    m_nMaxValue      = 100;

    m_strTitle       = _T("Progress");
    m_strCancelLabel = _T(" Cancel ");
	m_strRetryLabel	 = _T(" Retry ");
    m_bCancelled     = FALSE;
    m_bModal         = FALSE;
	m_bEnableRetry = FALSE;
    m_bPersistantPosition = TRUE;   // saves and restores position automatically
}

CProgressWnd::~CProgressWnd()
{
    DestroyWindow();
}

BOOL CProgressWnd::Create(CWnd* pParent, LPCTSTR pszTitle, BOOL bSmooth /* = FALSE */)
{
    BOOL bSuccess;

    // Register window class
    CString csClassName = AfxRegisterWndClass(CS_OWNDC|CS_HREDRAW|CS_VREDRAW,
                                              ::LoadCursor(NULL, IDC_APPSTARTING),
                                              CBrush(::GetSysColor(COLOR_BTNFACE)));

    // Get the system window message font for use in the cancel button and text area
    NONCLIENTMETRICS ncm;
    ncm.cbSize = sizeof(NONCLIENTMETRICS);
    VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
    m_font.CreateFontIndirect(&(ncm.lfMessageFont)); 

    // If no parent supplied then try and get a pointer to it anyway
    if (!pParent)
        pParent = AfxGetMainWnd();

    // Create popup window
    bSuccess = CreateEx(WS_EX_DLGMODALFRAME|WS_EX_TOPMOST, // Extended style
                        csClassName,                       // Classname
                        pszTitle,                          // Title
                        WS_POPUP|WS_BORDER|WS_CAPTION,     // style
                        0,0,                               // position - updated soon.
                        390,130,                           // Size - updated soon
                        pParent->GetSafeHwnd(),            // handle to parent
                        0,                                 // No menu
                        NULL);    
    if (!bSuccess) 
		return FALSE;

    // Now create the controls
    CRect TempRect(0,0,10,10);

    bSuccess = m_Text.Create(_T(""), WS_CHILD|WS_VISIBLE|SS_NOPREFIX|SS_LEFTNOWORDWRAP,
                             TempRect, this, IDC_TEXT);
    if (!bSuccess)
		return FALSE;

    DWORD dwProgressStyle = WS_CHILD|WS_VISIBLE;
#ifdef PBS_SMOOTH    
    if (bSmooth)
       dwProgressStyle |= PBS_SMOOTH;
#endif
    bSuccess = m_wndProgress.Create(dwProgressStyle, TempRect, this, IDC_PROGRESS);
    if (!bSuccess) 
		return FALSE;
	
    bSuccess = m_CancelButton.Create(m_strCancelLabel, 
                                     WS_CHILD|WS_VISIBLE|WS_TABSTOP| BS_PUSHBUTTON, 
                                     TempRect, this, IDC_CANCEL);
	if (!bSuccess)
		return FALSE;

	if (m_bEnableRetry)
	{
		bSuccess = m_RetryButton.Create(m_strRetryLabel,
			WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
			TempRect, this, IDC_RETRY);
		if (bSuccess)
			m_RetryButton.SetFont(&m_font, TRUE);
	}

    m_CancelButton.SetFont(&m_font, TRUE);	

    m_Text.SetFont(&m_font, TRUE);

    // Resize the whole thing according to the number of text lines, desired window
    // width and current font.
    SetWindowSize(m_nNumTextLines, 390);

    // Center and show window
    if (m_bPersistantPosition)
        GetPreviousSettings();
    else
        CenterWindow();

    Show();

    return TRUE;
}

BOOL CProgressWnd::GoModal(LPCTSTR pszTitle /*=_T("Progress")"*/, BOOL bSmooth /*=FALSE*/)
{
    CWnd *pMainWnd = AfxGetMainWnd();

    if (!::IsWindow(m_hWnd) && !Create(pMainWnd, pszTitle, bSmooth))
        return FALSE;

    // Walk up the window chain to find the main parent wnd and disable it. 
    CWnd * wnd = this;
    do {
        CWnd * parent = wnd->GetParent();

        // if we have no parent (ie. the main window)
        // or if our parent is disabled, 
        // then this is the window that we will want to remember for reenabling
        if (!parent || !parent->IsWindowEnabled()) {
            m_wRenenableWnd = wnd;
            m_wRenenableWnd->EnableWindow(false);
            break;
        }
        wnd = parent;
    } while (1);

    // Re-enable this window
    EnableWindow(TRUE);

    m_bModal = TRUE;

    return TRUE;
}
    
void CProgressWnd::SetWindowSize(int nNumTextLines, int nWindowWidth /*=390*/)
{
    int nMargin = 10;
    CSize EdgeSize(::GetSystemMetrics(SM_CXEDGE), ::GetSystemMetrics(SM_CYEDGE));

    CRect TextRect, CancelRect, ProgressRect, RetryRect;
    CSize CancelSize;

    // Set up a default size for the text area in case things go wrong
    TextRect.SetRect(nMargin,nMargin, nWindowWidth-2*nMargin, 100+2*nMargin);

    // Get DrawText to tell us how tall the text area will be (while we're at
    // it, we'll see how big the word "Cancel" is)
    CDC* pDC = GetDC();
    if (pDC)
	{
        CFont* pOldFont = pDC->SelectObject(&m_font);
        CString str = _T("M");
        for (int i = 0; i < nNumTextLines-1; i++)
			str += _T("\nM");
        pDC->DrawText(str, TextRect, DT_CALCRECT|DT_NOCLIP|DT_NOPREFIX);
        TextRect.right = TextRect.left + nWindowWidth;
        CancelSize = pDC->GetTextExtent(m_strCancelLabel + _T("  ")) +
                                             CSize(EdgeSize.cx*4, EdgeSize.cy*3);
        pDC->SelectObject(pOldFont);
        ReleaseDC(pDC);
    }
    
    // Work out how big (and where) the cancel button should be
    CancelRect.SetRect(TextRect.right-CancelSize.cx, TextRect.bottom+nMargin, 
                       TextRect.right, TextRect.bottom+nMargin + CancelSize.cy);

	RetryRect = CancelRect;
	RetryRect.OffsetRect(CSize(0, -22));

    // Work out how big (and where) the progress control should be
    ProgressRect.SetRect(TextRect.left, CancelRect.top + EdgeSize.cy, 
                         CancelRect.left-nMargin, CancelRect.bottom - EdgeSize.cy);


    // Resize the main window to fit the controls
    CSize ClientSize(nMargin + TextRect.Width() + nMargin,
                     nMargin + TextRect.Height() + nMargin + CancelRect.Height() + nMargin);

    CRect WndRect, ClientRect;
    GetWindowRect(WndRect); GetClientRect(ClientRect);
    WndRect.right = WndRect.left + WndRect.Width()-ClientRect.Width()+ClientSize.cx;
    WndRect.bottom = WndRect.top + WndRect.Height()-ClientRect.Height()+ClientSize.cy;
    MoveWindow(WndRect);

    // Now reposition the controls...
    m_wndProgress.MoveWindow(ProgressRect);
    m_CancelButton.MoveWindow(CancelRect);
	if (m_bEnableRetry)
	{
		// AT THE BEGINNING THE RETRY BUTTON IS DISABLED
		m_RetryButton.EnableWindow(FALSE);
		m_RetryButton.MoveWindow(RetryRect);
		TextRect.right -= RetryRect.Width() + 2;
	}
    m_Text.MoveWindow(TextRect);
}

void CProgressWnd::Clear() 
{ 
    SetText(_T(""));
    SetPos(0);
    m_bCancelled = FALSE; 
    m_nPrevPos = 0;

    if (::IsWindow(GetSafeHwnd()))
        UpdateWindow();
}

void CProgressWnd::Hide()  
{ 
    if (!::IsWindow(GetSafeHwnd())) 
        return;

    if (IsWindowVisible())
    {
        ShowWindow(SW_HIDE);
        ModifyStyle(WS_VISIBLE, 0);
    }
}

void CProgressWnd::Show()  
{ 
    if (!::IsWindow(GetSafeHwnd()))
        return;

    if (!IsWindowVisible())
    {
        ModifyStyle(0, WS_VISIBLE);
        ShowWindow(SW_SHOW);
        RedrawWindow(NULL,NULL,RDW_ERASE|RDW_FRAME|RDW_INVALIDATE|RDW_UPDATENOW);
    }
}

void CProgressWnd::SetRange(int nLower, int nUpper, int nStep /* = 1 */)    
{
    if (!::IsWindow(GetSafeHwnd())) 
        return;

    // To take advantage of the Extended Range Values we use the PBM_SETRANGE32
    // message intead of calling CProgressCtrl::SetRange directly. If this is
    // being compiled under something less than VC 5.0, the necessary defines
    // may not be available.
#ifdef PBM_SETRANGE32
    ASSERT(-0x7FFFFFFF <= nLower && nLower <= 0x7FFFFFFF);
    ASSERT(-0x7FFFFFFF <= nUpper && nUpper <= 0x7FFFFFFF);
    m_wndProgress.SendMessage(PBM_SETRANGE32, (WPARAM) nLower, (LPARAM) nUpper);
#else
    ASSERT(0 <= nLower && nLower <= 65535);
    ASSERT(0 <= nUpper && nUpper <= 65535);
    m_wndProgress.SetRange(nLower, nUpper);
#endif

    m_nMaxValue = nUpper;
    m_nMinValue = nLower;
    m_nStep     = nStep;

    m_wndProgress.SetStep(nStep);
}

int CProgressWnd::OffsetPos(int nPos)
{ 
    if (!::IsWindow(GetSafeHwnd())) 
        return m_nPrevPos;

    Show();

    return SetPos(m_nPrevPos + nPos);  
}

int CProgressWnd::StepIt()                
{
    if (!::IsWindow(GetSafeHwnd())) 
        return m_nPrevPos;

    Show();

    return SetPos(m_nPrevPos + m_nStep); 
}

int CProgressWnd::SetStep(int nStep)
{
    int nOldStep = m_nStep;
    m_nStep = nStep;
    if (!::IsWindow(GetSafeHwnd())) 
        return nOldStep;

    return m_wndProgress.SetStep(nStep); 
}

int CProgressWnd::SetPos(int nPos)                    
{
#ifdef PBM_SETRANGE32
    ASSERT(-0x7FFFFFFF <= nPos && nPos <= 0x7FFFFFFF);
#else
    ASSERT(0 <= nPos && nPos <= 65535);
#endif

    if (!::IsWindow(GetSafeHwnd())) 
        return m_nPrevPos;

    Show();

    CString strTitle;
    int nPercentage;
    
    m_nPrevPos = nPos;

    if (m_nMaxValue > m_nMinValue)
        nPercentage = (int) (((nPos - m_nMinValue)*100.0)/(m_nMaxValue - m_nMinValue) + 0.5);
    else
        nPercentage = 0;

    if (nPercentage != m_nPrevPercent) 
    {
        m_nPrevPercent = nPercentage;
        strTitle.Format(_T("%s [%d%%]"),m_strTitle,nPercentage);
        SetWindowText(strTitle);
    }
    return m_wndProgress.SetPos(nPos);        
}

void CProgressWnd::SetText(LPCTSTR fmt, ...)
{
    if (!::IsWindow(GetSafeHwnd())) 
        return;

    va_list args;

    va_start(args, fmt);
    _vstprintf(buffer, sizeof(buffer), fmt, args);
    va_end(args);

    m_Text.SetWindowText(buffer);
}

BEGIN_MESSAGE_MAP(CProgressWnd, CWnd)
    //{{AFX_MSG_MAP(CProgressWnd)
    ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
    ON_BN_CLICKED(IDC_CANCEL, OnCancel)
	ON_BN_CLICKED(IDC_RETRY, OnRetry)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CProgressWnd message handlers

BOOL CProgressWnd::OnEraseBkgnd(CDC* pDC) 
{
    // Fill background with Catchment background colour
    CBrush backBrush(GetSysColor(COLOR_BTNFACE));
    CBrush* pOldBrush = pDC->SelectObject(&backBrush);
    CRect rect;
    pDC->GetClipBox(&rect);     // Erase the area needed
    pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
    pDC->SelectObject(pOldBrush);

    return TRUE;
}
void CProgressWnd::OnRetry()
{
}
void CProgressWnd::OnCancel() 
{
    if (m_bPersistantPosition)
        SaveCurrentSettings();

    m_bCancelled = TRUE;
    Hide();

    if (m_bModal)
        PostMessage(WM_CLOSE);

    CWnd *pWnd = AfxGetMainWnd();
    if (pWnd && ::IsWindow(pWnd->m_hWnd))
        pWnd->SetForegroundWindow();
}


BOOL CProgressWnd::DestroyWindow() 
{
    if (m_bPersistantPosition)
        SaveCurrentSettings();

    if (m_bModal)
    {
        m_bModal = FALSE;
        CWnd *pMainWnd = AfxGetMainWnd();

        if (m_wRenenableWnd)
            m_wRenenableWnd->EnableWindow(TRUE);
    }
	
    return CWnd::DestroyWindow();
}

// Message pumping function that can either be used to pump messages during
// long operations. This version will only pass messages to this window (and
// all child windows). (Thanks to Michael <mbh-ep@post5.tele.dk> for this)
void CProgressWnd::PeekAndPump(BOOL bCancelOnESCkey /*= TRUE*/)
{
    if (m_bModal && ::GetFocus() != m_hWnd)
        SetFocus();

    MSG msg;
    while (!m_bCancelled && ::PeekMessage(&msg, NULL,0,0,PM_NOREMOVE)) 
    {
        if (bCancelOnESCkey && (msg.message == WM_CHAR) && (msg.wParam == VK_ESCAPE))
            OnCancel();

        // Cancel button disabled if modal, so we fake it.
        if (m_bModal && (msg.message == WM_LBUTTONUP))
        {
            CRect rect;
            m_CancelButton.GetWindowRect(rect);
            if (rect.PtInRect(msg.pt))
                OnCancel();
			m_RetryButton.GetWindowRect(rect);
			if (rect.PtInRect(msg.pt))
				OnRetry();
		}
  
        if (!AfxGetApp()->PumpMessage()) 
        {
            ::PostQuitMessage(0);
            return;
        } 
    }
}

// Retores the previous window size from the registry
void CProgressWnd::GetPreviousSettings()
{
    int x = AfxGetApp()->GetProfileInt(szSection, szEntryX, -1);
    int y = AfxGetApp()->GetProfileInt(szSection, szEntryY, -1);

    if (x >= 0 && x < GetSystemMetrics(SM_CXSCREEN) &&
        y >= 0 && y < GetSystemMetrics(SM_CYSCREEN))
    {
        SetWindowPos(NULL, x,y, 0,0, SWP_NOSIZE|SWP_NOZORDER);
    }
    else
        CenterWindow();
}

// Saves the current window position registry
void CProgressWnd::SaveCurrentSettings()
{   
    if (!IsWindow(m_hWnd))
        return;

    CRect rect;
    GetWindowRect(rect);

    AfxGetApp()->WriteProfileInt(szSection, szEntryX, rect.left);
    AfxGetApp()->WriteProfileInt(szSection, szEntryY, rect.top);
}
//--------------------------------------------------------------------
CProgressFuncWnd::CProgressFuncWnd()
{
	CommonConstruct();
	m_bTimeOut = FALSE;
	m_startTime = 0;
	m_RemainingTime = 0;
	bWaitingForUserAction = FALSE;
};
CProgressFuncWnd::CProgressFuncWnd(CWnd* pParent, LPCTSTR pszTitle, CustomProcessFunc myfunc, DWORD dwTimeOut, BOOL bSmooth , BOOL bEnableRetry /*= TRUE*/)
{
	CommonConstruct();
	bWaitingForUserAction = FALSE;
	m_bEnableRetry = bEnableRetry;
	m_dwTimeOut = dwTimeOut;
	m_strTitle = pszTitle;
	m_myfunc = myfunc;
	m_nWindowTimer = NULL;
	Create(pParent, pszTitle, bSmooth);	
	
}
void CProgressFuncWnd::Show()
{
	CProgressWnd::Show();
	if (!::IsWindow(GetSafeHwnd()))
		return;

	if (IsWindowVisible())
	{
		SetRange(0, m_dwTimeOut);
	}
}

BEGIN_MESSAGE_MAP(CProgressFuncWnd, CProgressWnd)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_RETRY, CProgressFuncWnd::OnRetry)
END_MESSAGE_MAP()

void CProgressFuncWnd::OnTimeOut()
{
	if (m_bPersistantPosition)
		SaveCurrentSettings();

	m_bTimeOut = TRUE;
	Hide();

	if (m_bModal)
		PostMessage(WM_CLOSE);

	CWnd *pWnd = AfxGetMainWnd();
	if (pWnd && ::IsWindow(pWnd->m_hWnd))
		pWnd->SetForegroundWindow();
}
void CProgressFuncWnd::LaunchProcess()
{
	std::this_thread::sleep_for(std::chrono::milliseconds(30));
	m_startTime = GetTickCount();
	// launch the function asynchronously :)
	// and store m_taskResult will be our sync result (std::future) cool :)
	m_taskResult = std::async(m_myfunc,this, m_dwTimeOut/*&CProgressFuncWnd::DoProcess, this*/);
	
}
void CProgressFuncWnd::OnRetry()
{
	m_Text.SetWindowText(buffer);
	m_RetryButton.EnableWindow(FALSE);
	bWaitingForUserAction = FALSE;
	RedrawWindow();
	LaunchProcess();
}
INT_PTR CProgressFuncWnd::WaitForEnd(DWORD &milis)
{	
	CString strTitle;
	std::future_status status;

	// cancel is always enabled so always watch it
	if (Cancelled())
	{
		return IDCANCEL;
	}
	// if NOT waiting for user action (retry press)	
	if (!bWaitingForUserAction)
	{
		// then count time of process and act accordingly 

		// get current millis since process started
		milis = GetTickCount() - m_startTime;
		// check if async process returned any value
		status = m_taskResult.wait_for(std::chrono::milliseconds(0));
		if (status == std::future_status::ready)
		{
			// yes , async process has ended
			// return success if process returned TRUE
			if (m_taskResult.get() == TRUE)
			{
				// redraw window 
				RedrawWindow();
				// change caption to notify all went
				strTitle.Format(_T("%s - Operation Completed!"), m_strTitle);
				SetWindowText(strTitle);
				return IDOK;
			}
			// return process aborted by itself
			return IDABORT;
		}

		// Time out reached
		if (milis >= m_dwTimeOut)
		{
			// is retry option enabled?
			if (!m_bEnableRetry)
			{
				// call function to exit 
				OnTimeOut();
				// call function to exit 
				//OnTimeOut();
				// and return time out error code
				return IDTIMEOUT;
			}
			else
			{
				// offer option to retry the operation
				// reset vars
				milis = 0;
				SetPos(0);
				m_nPrevPos = 0;
				// signal time out for process
				m_bTimeOut = TRUE;
				strTitle.Format(_T("%s - Operation Failed!"), m_strTitle);
				SetWindowText(strTitle);
				m_Text.SetWindowText(_T("Operation failed, click 'Retry' to try again or 'Cancel' to exit."));
				bWaitingForUserAction = TRUE;
				m_RetryButton.EnableWindow(TRUE);
				return IDRETRY;
			}
		}
	}
	
	// dispatch window pending messages
	PeekAndPump();

	// update caption if in process
	if (!bWaitingForUserAction)
	{
		// calculate remaininig miliseconds from total time out and current milis
		m_RemainingTime = m_dwTimeOut - (DWORD)milis;
		// update window caption with remaining time
		strTitle.Format(_T("%s [%d%%] Remaining Time: %0.02f seconds"), m_strTitle, m_nPrevPercent, (double)((double)m_RemainingTime / 1000.0f));
		SetWindowText(strTitle);
	}

	return IDRETRY;
}



Interface

#pragma once
// ProgressWnd.h : header file
//
// Written by Chris Maunder (chrismaunder@codeguru.com)
// Copyright 1998.
//
// CProgressWnd is a drop-in popup progress window for use in
// programs that a time consuming. Check out the accompanying HTML 
// doc file for details.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed by any means PROVIDING it is not sold for
// profit without the authors written consent, and providing that this
// notice and the authors name is included. If the source code in 
// this file is used in any commercial application then an email to 
// me would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to your
// computer, causes your pet cat to fall ill, increases baldness or
// makes you car start emitting strange noises when you start it up.
//
// Expect bugs.
// 
// Please use and enjoy. Please let me know of any bugs/mods/improvements 
// that you have found/implemented and I will fix/incorporate them into this
// file. 

#ifndef _INCLUDE_PROGRESSWND_H
#define _INCLUDE_PROGRESSWND_H

#include <future>
#include <functional>
#include <algorithm>

class CProgressWnd;
class CProgressFuncWnd;


/////////////////////////////////////////////////////////////////////////////
// CProgressWnd window

class CProgressWnd : public CWnd
{
// Construction/Destruction
public:
    CProgressWnd();
    CProgressWnd(CWnd* pParent, LPCTSTR pszTitle, BOOL bSmooth = FALSE, BOOL bEnableRetry=FALSE);
    virtual ~CProgressWnd();

    BOOL Create(CWnd* pParent, LPCTSTR pszTitle, BOOL bSmooth = FALSE);
    BOOL GoModal(LPCTSTR pszTitle =_T("Progress"), BOOL bSmooth = FALSE);

protected:
    void CommonConstruct();
	TCHAR buffer[512];
// Operations
public:
    void SetRange(int nLower, int nUpper, int nStep = 1);
                                                    // Set range and step size
    int OffsetPos(int nPos);                        // Same as CProgressCtrl
    int StepIt();                                   //    "
    int SetStep(int nStep);                         //    "
    virtual int SetPos(int nPos);                           //    "

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

    void Clear();                                   // Clear text, reset bar
    void Hide();                                    // Hide window
    virtual void Show();                            // Show window

    BOOL Cancelled() { return m_bCancelled; }       // Was "Cancel" hit?

	void SetWindowSize(int nNumTextLines, int nWindowWidth = 390);

    void PeekAndPump(BOOL bCancelOnESCkey = TRUE);  // Message pumping for modal operation   
    
// Implementation
protected:
    void GetPreviousSettings();
    void SaveCurrentSettings();

protected:
    CWnd * m_wRenenableWnd;
	BOOL m_bEnableRetry;
    BOOL m_bCancelled;
    BOOL m_bModal;
    BOOL m_bPersistantPosition;
    int  m_nPrevPos, m_nPrevPercent;
    int  m_nStep;
    int  m_nMaxValue, m_nMinValue;
    int  m_nNumTextLines;

    CStatic       m_Text;
    CProgressCtrl m_wndProgress;
    CButton       m_CancelButton;
	CButton       m_RetryButton;
    CString       m_strTitle,
                  m_strCancelLabel,
				  m_strRetryLabel;
    CFont         m_font;


// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CProgressWnd)
	public:
	virtual BOOL DestroyWindow();
	//}}AFX_VIRTUAL

// Generated message map functions
protected:
    //{{AFX_MSG(CProgressWnd)
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	//}}AFX_MSG
    afx_msg void OnCancel();
	virtual afx_msg void OnRetry();
    DECLARE_MESSAGE_MAP()
};

typedef std::function<BOOL(CProgressFuncWnd *pWnd, DWORD dwTimeOut)> CustomProcessFunc;

class CProgressFuncWnd : public CProgressWnd
{
protected:
	BOOL bWaitingForUserAction;
	DWORD m_startTime;
	std::future<BOOL> m_taskResult;
	BOOL m_bTimeOut;
	UINT_PTR m_nWindowTimer;
	DWORD m_dwTimeOut;
	DWORD m_RemainingTime;
	CustomProcessFunc m_myfunc;
	void OnTimeOut();
public:
	CProgressFuncWnd();
	virtual void Show();
	void LaunchProcess();
	virtual afx_msg void OnRetry();
	INT_PTR WaitForEnd(DWORD &milis);
	CProgressFuncWnd(CWnd* pParent, LPCTSTR pszTitle, CustomProcessFunc myfunc,DWORD dwTimeOut,BOOL bSmooth, BOOL bEnableRetry = TRUE);
	BOOL TimeOut() { return (m_bTimeOut && !m_bEnableRetry); }				// Was "timeout" hit?
	BOOL WaitingUserRetry() {return bWaitingForUserAction; };
	DECLARE_MESSAGE_MAP()
};


#endif
/////////////////////////////////////////////////////////////////////////////


Usage example:

void COnLineDevices::OnBnClickedOk()
{
//	MessageBoxTimeout(this->GetSafeHwnd(),_T("Sending command to device..."),_T("ISM - Comunicando con dispositivo"), MB_SETFOREGROUND |MB_SYSTEMMODAL|MB_ICONINFORMATION, 10, 5000);
	CProgressFuncWnd wndProgress(this, _T("Connecting to device"), std::bind(&COnLineDevices::MyCustomFunc,this, std::placeholders::_1, std::placeholders::_2),5000,FALSE,TRUE);

	// wndProgress.GoModal(); // Call this if you want a modal window
	wndProgress.SetText(_T("Sending data to %s..."),_T("Device 01"));
	wndProgress.GoModal();
	wndProgress.LaunchProcess();
	DWORD milis = 0;
	INT_PTR res = IDRETRY;
	// Wait for async task to end, each calls recovers the miliseconds elapsed synce the process is running
	// do not modify this value
	while ((res=wndProgress.WaitForEnd(milis))==IDRETRY)
	{
		
	}
	
	switch (res)
	{
		case IDCANCEL:
			AfxMessageBox(_T("User aborted operation"), MB_ICONSTOP);
		break;
		case IDTIMEOUT:
			AfxMessageBox(_T("Time out reached for operation!"), MB_ICONEXCLAMATION);
		break;
		case IDOK:
			AfxMessageBox(_T("Operation completed on time!"),MB_ICONINFORMATION);
		break;
		case IDABORT:
			AfxMessageBox(_T("Operation TERMINATED ERRONEUSLY!"), MB_ICONSTOP);
		break;
	}
	CDialogEx::OnOK();
}


Custom function running on another thread, as we use std::function we could bind any function to this class easily without reimplementing different dialogs for each one.


BOOL COnLineDevices::MyCustomFunc(CProgressFuncWnd *pWnd,DWORD dwTimeOut)
{
	
	if (!pWnd->WaitingUserRetry())
	{
		for (int i = 0; i < dwTimeOut; i++)
		{
			std::this_thread::sleep_for(std::chrono::milliseconds(100));

			// should we cancel or exit due to time out?
			if (pWnd->TimeOut() || pWnd->Cancelled() || pWnd->WaitingUserRetry())
			{
				return FALSE;
			}
			pWnd->StepIt();
		}
	}
	else
	{
		return TRUE;
	}
	return TRUE;
}

QuestionHow Do I close the ProgressWindow ? Pin
sunil_rbc13-Oct-15 2:07
professionalsunil_rbc13-Oct-15 2:07 
Questionquestion Pin
kcpnevergiveup15-Apr-13 16:21
kcpnevergiveup15-Apr-13 16:21 
AnswerRe: question Pin
kcpnevergiveup15-Apr-13 17:01
kcpnevergiveup15-Apr-13 17:01 
GeneralThis is really cool.... Pin
Rangarajan Varadan8-Apr-09 6:52
Rangarajan Varadan8-Apr-09 6:52 
Generalthanks Pin
Dirk Higbee2-Sep-08 3:17
Dirk Higbee2-Sep-08 3:17 
QuestionProgress bar frozen Pin
Solange200831-Aug-08 12:05
Solange200831-Aug-08 12:05 
QuestionCrash in CProgressWnd::Create() in VS2008 Pin
oldNic15-Aug-08 7:28
oldNic15-Aug-08 7:28 
AnswerRe: Crash in CProgressWnd::Create() in VS2008 Pin
oldNic18-Aug-08 10:41
oldNic18-Aug-08 10:41 
GeneralRe: Crash in CProgressWnd::Create() in VS2008 Pin
arokicki5-Mar-09 7:57
arokicki5-Mar-09 7:57 
AnswerRe: Crash in CProgressWnd::Create() in VS2008 Pin
Ards6-Jun-11 18:02
Ards6-Jun-11 18:02 
Generalerror C2504: 'CWnd' : base class undefined Pin
Luke DeStevens9-Oct-07 10:29
Luke DeStevens9-Oct-07 10:29 
GeneralRe: error C2504: 'CWnd' : base class undefined Pin
jaymz12-May-08 1:43
jaymz12-May-08 1:43 
QuestionHow get rid of flickering? Pin
Walxer28-Aug-07 13:04
Walxer28-Aug-07 13:04 
GeneralThank you Chris : I no more need two threads Pin
Ahmed Charfeddine24-Aug-07 1:28
Ahmed Charfeddine24-Aug-07 1:28 
QuestionAccessing Progress Window? Pin
WVP10-Aug-07 5:48
WVP10-Aug-07 5:48 
AnswerRe: Accessing Progress Window? Pin
Ahmed Charfeddine24-Aug-07 2:21
Ahmed Charfeddine24-Aug-07 2:21 
GeneralGood! Pin
napo@burgasnet.com26-Jun-07 3:28
napo@burgasnet.com26-Jun-07 3:28 
GeneralThe same for .NET Pin
_slav_4-Feb-07 3:04
_slav_4-Feb-07 3:04 
GeneralCool ! Pin
eigen30-Sep-06 18:03
eigen30-Sep-06 18:03 
Generalproblem used in worker thread Pin
how jack24-Feb-06 9:48
how jack24-Feb-06 9:48 
GeneralRe: problem used in worker thread Pin
Kevin Done9-Nov-11 21:21
Kevin Done9-Nov-11 21:21 
GeneralCrash on MouseOver Pin
Staati25-Jan-06 4:48
Staati25-Jan-06 4:48 
GeneralRe: Crash on MouseOver Pin
Staati26-Jan-06 5:37
Staati26-Jan-06 5:37 
GeneralAlways crashes for me :-( Pin
EthannCastell4-Oct-05 0:49
EthannCastell4-Oct-05 0:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.