www.pudn.com > CTableDemo.rar > Counter.cpp


// Counter.cpp : implementation file 
// 
 
#include "stdafx.h"  
#include "Counter.h" 
#include "Odom.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#define BLACK                     (COLORREF) (0x00000000) 
#define MAGENTA                (COLORREF) (0x00FF00FF) 
 
///////////////////////////////////////////////////////////////////////////// 
// CCounter 
 
CCounter::CCounter() 
{ 
	m_bInitialize = TRUE; 
	m_bAllowRotation = TRUE; 
	m_LastDigit = 0; 
} 
 
CCounter::~CCounter() 
{ 
} 
 
 
BEGIN_MESSAGE_MAP(CCounter, CStatic) 
	//{{AFX_MSG_MAP(CCounter) 
	ON_WM_CREATE() 
	ON_WM_PAINT() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCounter message handlers 
 
int CCounter::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CStatic::OnCreate(lpCreateStruct) == -1) 
		return -1; 
 
	//	Load 10 image arrays 
	BOOL success; 
	for (int i = 0; i < NUM_IMAGE_ARRAYS; i ++) 
	{ 
		success = m_ImgArray [i].Create (IDB_ZERO_TO_ONE + i,16,8, MAGENTA); 
		ASSERT (success); 
	} 
	m_LastDigit = 0; 
	return 0; 
} 
 
void CCounter::OnPaint() 
{ 
	CPaintDC dc(this); // device context for painting 
	int           counter; 
	int			  i; 
	POINT	pt;		 
	 
	pt.x = 0;	pt.y = 0; 
 
	if (m_bAllowRotation) 
	{ 
		//  start each counter from zero every time 
		switch (m_RotationStyle) 
		{ 
		case STYLE_ROTATE_FROM_ZERO: 
			//  First time thru -- set the counter to zero 
			if (m_bInitialize) 
				m_ImgArray [0].Draw (&dc, 0, pt, ILD_NORMAL); 
			else 
			{ 
				// draw first image 
				m_ImgArray [0].Draw (&dc, 0, pt, ILD_NORMAL); 
				for (int i = 0; i < m_DigitVal; i ++) 
					RunImageBmp (i, &dc, pt); // run through the image array 
			} 
 
			m_LastDigit = m_DigitVal; // save for next time through 
			break; 
		// continue scrolling from existing number 
		case STYLE_ROTATE_WRAPAROUND: 
			// start each counter from current num and wrap around 
			m_ImgArray [0].Draw (&dc, 0, pt, ILD_NORMAL); 
			 
			//	just in case 
			if (m_DigitVal < 0) 
				break; 
			// make sure last digit is not negative 
			if (m_LastDigit < 0) 
				m_LastDigit = 0; 
			 
			// there was no change; draw last digit and exit 
			if (m_DigitVal == m_LastDigit) 
			{ 
				m_ImgArray [m_DigitVal].Draw (&dc, 0, pt, ILD_NORMAL); 
				break; 
			} 
 
			// Run as far as it can go.  If the new digit is less than the current 
			//	digit, then the loop will not be entered and counter will be zero. 
			counter = 0; 
			for (i = m_LastDigit; i < m_DigitVal; i ++) 
			{ 
				RunImageBmp (i, &dc, pt); 
				counter ++; 
			} 
			 
			// wrap around if necessary 
			if (counter == 0)  // new digit is less than old digit 
			{ 
				//	run up to zero 
				for (i = m_LastDigit; i < NUM_IMAGE_ARRAYS; i ++) 
					RunImageBmp (i, &dc, pt); 
				//	then run up to new digit 
				for (i = 0; i < m_DigitVal; i ++) 
					RunImageBmp (i, &dc, pt); 
			} 
			 
			m_LastDigit = m_DigitVal; // save for next time through 
			break; 
		} 
		 
		m_bInitialize = FALSE; 
	} 
	else // this is here in case the window is repainted because it was hidden 
	{ 
		if (m_LastDigit >= 0)  // redraw the current digit 
			m_ImgArray [m_LastDigit].Draw (&dc, 0, pt, ILD_NORMAL); 
		else  // draw a zero 
			m_ImgArray [0].Draw (&dc, 0, pt, ILD_NORMAL); 
	} 
 
	m_bAllowRotation = FALSE; 
	// Do not call CStatic::OnPaint() for painting messages 
} 
 
LRESULT CCounter::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)  
{ 
	switch (message) 
	{ 
	case WM_ADVANCE_COUNTER: 
		m_bAllowRotation = TRUE; 
		m_DigitVal = (int) wParam;  // the window now knows what integer to show 
		Invalidate (TRUE); // force a repaint because thats where the rotation takes place 
		break; 
	} 
 
	return CStatic::WindowProc(message, wParam, lParam); 
} 
 
void CCounter::RunImageBmp (int i, CPaintDC *pDC, POINT  pt) 
{ 
	//	loops through a CImageList array - 
	//	in this case, 8 images. 
	for (int j = 0; j < NUM_ARRAY_IMAGES; j ++) 
	{ 
		m_ImgArray [i].Draw (pDC, j, pt, ILD_NORMAL); 
		if (m_TimeInterval)	// if a delay is set, do it 
			Sleep (m_TimeInterval); 
	} 
}