Click here to Skip to main content
15,881,812 members
Articles / Web Development / HTML

Using the IE 5 built-in progress dialog

Rate me:
Please Sign up or sign in to vote.
4.95/5 (8 votes)
6 Feb 2000 210.8K   1.8K   64  
A wrapper class for the progress dialog provided by IE 5.
// IProgDlgDlg.cpp : implementation file
//

#include "stdafx.h"
#include "IProgDlg.h"
#include "IProgDlgDlg.h"
#include "shprogresswnd.h"

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

/////////////////////////////////////////////////////////////////////////////
// CIProgDlgDlg dialog

CIProgDlgDlg::CIProgDlgDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIProgDlgDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIProgDlgDlg)
	m_bShowMinimize = FALSE;
	m_bShowProgressBar = FALSE;
	m_bShowTimeLeft = FALSE;
	m_bModalDlg = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CIProgDlgDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIProgDlgDlg)
	DDX_Check(pDX, IDC_MINIMIZE, m_bShowMinimize);
	DDX_Check(pDX, IDC_PROGRESSBAR, m_bShowProgressBar);
	DDX_Check(pDX, IDC_SHOWTIME, m_bShowTimeLeft);
	DDX_Check(pDX, IDC_MODAL, m_bModalDlg);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CIProgDlgDlg, CDialog)
	//{{AFX_MSG_MAP(CIProgDlgDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_GO, OnGo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIProgDlgDlg message handlers

BOOL CIProgDlgDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CIProgDlgDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CIProgDlgDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CIProgDlgDlg::OnGo() 
{
CSHProgressWnd dlg;
const DWORD    dwMax = 700;
TCHAR          szMsg[256];
HRESULT        hr;

    UpdateData();

    if ( !dlg.IsValid() )
        {
        AfxMessageBox ( _T("Couldn't create CSHProgressWnd object.  CSHProgressWnd requires IE 5 to be installed."), MB_ICONERROR );
        return;
        }

    dlg.SetTitle ( _T("Progress Dlg Title") );
    dlg.SetAnimation ( IDR_PROGRESS );
    dlg.SetCancelMessage ( _T("Cancel Message") );
    dlg.SetLineText ( 1, _T("Unzipping some phantom files...") );

    dlg.SetAllowMinimize ( m_bShowMinimize );
    dlg.SetCalculateTime ( m_bShowTimeLeft );
    dlg.SetShowProgressBar ( m_bShowProgressBar );

    hr = m_bModalDlg ? dlg.ShowModal(this) : dlg.ShowModeless(this);

    if ( FAILED(hr) )
        {
        CString s;

        s.Format ( _T("Couldn't show the progress window.  Error returned was %lu"),
                  (DWORD) hr );

        AfxMessageBox ( s, MB_ICONERROR );
        
        return;
        }

    dlg.UpdateProgress ( 0, dwMax );
    dlg.ResetTimer();

    for ( DWORD dw = 0;
          dw < dwMax  &&  !dlg.HasUserCanceled();
          dw += 8 + rand()%10 )
        {
        Sleep(150);

        if ( dw > dwMax )
            dw = dwMax;                 // just in case we go over the max value

        wsprintf ( szMsg, _T("Progress = %lu/%lu"), dw, dwMax );

        dlg.SetLineText ( 2, szMsg );
        dlg.UpdateProgress ( dw );
        }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) VMware
United States United States
Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

Mike was a VC MVP from 2005 to 2009.

Comments and Discussions