www.gusucode.com > 类似Windows拷贝文件一样的进度条VC实现-源码程序 > 类似Windows拷贝文件一样的进度条VC实现-源码程序/code/IconProgressAppDlg.cpp

    // IconProgressAppDlg.cpp : implementation file
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "IconProgressApp.h"
#include "IconProgressAppDlg.h"
#include "math.h"

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

/////////////////////////////////////////////////////////////////////////////
// CIconProgressAppDlg dialog

CIconProgressAppDlg::CIconProgressAppDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIconProgressAppDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIconProgressAppDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CIconProgressAppDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIconProgressAppDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CIconProgressAppDlg, CDialog)
	//{{AFX_MSG_MAP(CIconProgressAppDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIconProgressAppDlg message handlers

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

	icon_progress1.Attach(IDC_ICON_PROGRESS1, this, IDI_ICON2, 18, 4);
	icon_progress1.SetRange(0, 100);
	icon_progress1.SetPos(0);

	icon_progress2.Attach(IDC_ICON_PROGRESS2, this, IDI_ICON3, 4, RGB(255, 128, 128));
	icon_progress2.SetRange(-100, 200);
	icon_progress2.SetIconChange(IDI_ICON4, 0);
	icon_progress2.SetIconChange(IDI_ICON5, 100);
	icon_progress2.SetPos(-100);

	icon_progress3.Attach(IDC_ICON_PROGRESS3, this, IDI_ICON1, 8, RGB(0, 96, 96));
	icon_progress3.SetRange(100, 200);
	icon_progress3.SetPos(100);

	icon_progress4.Attach(IDC_ICON_PROGRESS4, this, IDI_ICON6, 24);
	icon_progress4.SetRange(0, 100);
	icon_progress4.SetPos(0);

	SetTimer(0, 50, 0);

	// 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
	
	return TRUE;
}

// 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 CIconProgressAppDlg::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 CIconProgressAppDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CIconProgressAppDlg::OnTimer(UINT nIDEvent) 
{
	static int position1 = 0;
	static int position2 = -100;
	static double index  = 0;
	int position3;

	switch (nIDEvent)
	{
		case 0:
		{
			position1 += 1;
			icon_progress1.SetPos(position1);
			icon_progress4.SetPos(100 - position1 + 20);
			position1 %= 100;

			position2 += 5;
			icon_progress2.SetPos(position2);
			if (position2 == 200)
			{
				position2 = -100;
			}

			index++;
			position3  = (int)(100.0 * sin(index * (3.1415927 / 50)));
			if (position3 == 99)
			{
				position3 = 100;
			}
			position3 += 100;
			icon_progress3.SetPos(position3);
			if (index == 50)
			{
				index = 0;
			}
			break;
		}
	}
	
	CDialog::OnTimer(nIDEvent);
}