www.pudn.com > Observerwangdxh.zip > CalMon.cpp
// CalMon.cpp : implementation of the CCalorieMonitor class // #include "stdafx.h" #include "CalMon.h" #include///////////////////////////////////////////////////////////////////////////// // CCalorieMonitor message handlers BEGIN_MESSAGE_MAP(CCalorieMonitor, CCardioMonitor) //{{AFX_MSG_MAP(CCalorieMonitor) ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() CCalorieMonitor::CCalorieMonitor( CCardioSubject * pCardioSubject ) : CCardioMonitor( pCardioSubject ) { // Step 1 - Get calories per minute and initialize total calories m_nCaloriesPerMinute = m_pCardioSubject->GetCaloriesPerMinute(); m_dbTotalCalories = 0.0; } CCalorieMonitor::~CCalorieMonitor() { } BOOL CCalorieMonitor::Update( CSubject * pSubject ) { // Step 1 - Make sure the right subject updates the time monitor if( m_pCardioSubject != pSubject ) { return FALSE; } // Step 2 - Get calories per minute and calculate total calories m_nCaloriesPerMinute = m_pCardioSubject->GetCaloriesPerMinute(); // Step 3 - Update the window, if the calorie monitor is started if( m_bStarted == TRUE ) { m_dbTotalCalories += ( double ) m_nCaloriesPerMinute / 60.00; Invalidate(); } return TRUE; } BOOL CCalorieMonitor::Create( const RECT & rRect, CWnd * pParentWnd, UINT nID ) { // Step 1 - Adjust Rectangle CRect WndRect = CRect( rRect.left, rRect.top, rRect.left + 140, rRect.top + 120 ); // Step 2 - Create window return CWnd::Create( NULL, NULL, WS_VISIBLE | WS_BORDER | WS_CHILD | WS_TABSTOP, WndRect, pParentWnd, nID ); } void CCalorieMonitor::OnPaint() { CRect ClientRect; CRect DrawRect; CRect TitleRect; CString csTitle = "Calorie Monitor"; CString csText1 = "Calories / Minute"; CString csText2 = "Total Calories"; CString csCaloriesPerMinute = itoa( m_nCaloriesPerMinute, " ", 10 ); CString csTotalCalories = itoa( ( INT ) floor( m_dbTotalCalories ), " ", 10 ); CPaintDC dc(this); // Step 1 - Set background color, mode and text color GetClientRect( ClientRect ); dc.FillSolidRect( ClientRect, RGB( 0, 0, 0 ) ); TitleRect = CRect( ClientRect.left, ClientRect.top, ClientRect.right, ClientRect.top + 30 ); dc.FillSolidRect( TitleRect, RGB( 0, 128, 128 ) ); dc.SetBkMode( TRANSPARENT ); dc.SetTextColor( RGB( 255, 255, 255 ) ); // Step 2 - Set the text DrawRect = CRect( ClientRect.left, ClientRect.top + 5, ClientRect.right, ClientRect.top + 45 ); dc.DrawText( csTitle, DrawRect, DT_CENTER | DT_WORDBREAK ); DrawRect = CRect( ClientRect.left + 15, ClientRect.top + 35, ClientRect.left + 80, ClientRect.top + 75 ); dc.DrawText( csText1, DrawRect, DT_LEFT | DT_WORDBREAK ); DrawRect = CRect( ClientRect.left + 85, ClientRect.top + 40, ClientRect.left + 135, ClientRect.top + 60 ); dc.DrawText( csCaloriesPerMinute, DrawRect, DT_LEFT | DT_WORDBREAK ); DrawRect = CRect( ClientRect.left + 15, ClientRect.top + 75, ClientRect.left + 80, ClientRect.top + 115 ); dc.DrawText( csText2, DrawRect, DT_LEFT | DT_WORDBREAK ); DrawRect = CRect( ClientRect.left + 85, ClientRect.top + 80, ClientRect.left + 135, ClientRect.top + 105 ); dc.DrawText( csTotalCalories, DrawRect, DT_LEFT | DT_WORDBREAK ); }