www.gusucode.com > 基于VC编程界面编程高级应用技术源码程序 > VC界面编程高级应用技术/code/3/MyProgBar/MyProgBarDlg.cpp

    // MyProgBarDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyProgBar.h"
#include "MyProgBarDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	afx_msg void OnTimer(UINT nIDEvent);
	virtual BOOL OnInitDialog();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyProgBarDlg dialog

CMyProgBarDlg::CMyProgBarDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyProgBarDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyProgBarDlg)
		// 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 CMyProgBarDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyProgBarDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

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

/////////////////////////////////////////////////////////////////////////////
// CMyProgBarDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	//创建自定义进度条
	MyProgBar.Create(WS_CHILD|WS_VISIBLE,CRect(10,20,220,42),this,ID_MYPROGRESSBAR);
	//设置增量
	MyProgBar.SetStep(1);
	//设置进度条范围
	MyProgBar.SetRange(0,100);
	//设置进度条当前进度
	MyProgBar.SetPos(1);
	//初始化进度
	percent=0;
	this->SetTimer(ID_MYPROGRESSBAR,100,NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyProgBarDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

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

void CAboutDlg::OnTimer(UINT nIDEvent) 
{
	
}

void CMyProgBarDlg::OnTimer(UINT nIDEvent) 
{
percent++;
	if(percent>100)
		percent=0;
	//设置进度
	MyProgBar.SetPos(percent);
	//根据percent的奇偶性设置动画效果
	if(percent%2)
		MyProgBar.SetBmp(IDB_OPEN);
	else
		MyProgBar.SetBmp(IDB_CLOSE);
	CString title;
	title.Format("%d%%",percent);
	//动态设置进度条标题
	MyProgBar.SetBarCaption(title);
	CDialog::OnTimer(nIDEvent);	
	CDialog::OnTimer(nIDEvent);	
	CDialog::OnTimer(nIDEvent);
}

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	//隐藏对话框 
	ShowWindow(SW_HIDE); 
	CRect dlgRect; 
	GetClientRect(&dlgRect); 
	CPoint centerPoint; 
	centerPoint.x=dlgRect.Width()/2; 
	centerPoint.y=dlgRect.Height()/2; 
	//得到对话框的中点坐标 
	CRgn testrgn; 
	this->ShowWindow(SW_HIDE);
	//得到边框宽度
	int m=GetSystemMetrics(SM_CYSIZEFRAME);
	//以下代码实现对话框的动态弹出 
	for (int i=10;i<dlgRect.Width()/2+2*m;i+=1) 
	{ 
		//由内向外建立不同大小的区域
		testrgn.CreateRectRgn(centerPoint.x-i,
			centerPoint.y-i,centerPoint.x+i,centerPoint.y+i); 
		//设置窗口的区域
		SetWindowRgn((HRGN) testrgn,TRUE); 
		//居中显示窗口
		ShowWindow(SW_SHOW); 
		CenterWindow(); 
		testrgn.DeleteObject(); 
		//稍做延时,以呈现动态效果
		Sleep(5);
	} 
	return TRUE; 
	
}