www.pudn.com > Observerwangdxh.zip > PgmCtrl.cpp
// PgmCtrl.cpp : implementation of the CProgramControl class
//
#include "stdafx.h"
#include "PgmCtrl.h"
// Predefined constants
#define ID_PROGRAM_LIST 0x20001
#define ID_EFFORT_LEVEL_EDIT 0x20002
#define ID_EFFORT_LEVEL_SPIN 0x20003
/////////////////////////////////////////////////////////////////////////////
// CTimeMonitor message handlers
BEGIN_MESSAGE_MAP(CProgramControl, CCardioMonitor)
//{{AFX_MSG_MAP(CProgramControl)
ON_WM_PAINT()
//}}AFX_MSG_MAP
ON_CBN_SELCHANGE( ID_PROGRAM_LIST, OnProgramChange )
ON_NOTIFY(UDN_DELTAPOS, ID_EFFORT_LEVEL_SPIN, OnDeltaposEffortLevel)
END_MESSAGE_MAP()
CProgramControl::CProgramControl( CCardioSubject * pCardioSubject ) : CCardioMonitor( pCardioSubject )
{
}
CProgramControl::~CProgramControl()
{
}
BOOL CProgramControl::Update( CSubject * pSubject )
{
// Step 1 - Make sure the right subject updates the heartrate meter
if( m_pCardioSubject != pSubject )
{
return FALSE;
}
return TRUE;
}
BOOL CProgramControl::Create( const RECT & rRect, CWnd * pParentWnd, UINT nID )
{
// Step 1 - Adjust Rectangle
CRect WndRect = CRect( rRect.left, rRect.top, rRect.left + 190, rRect.top + 120 );
CRect ClientRect;
// Step 2 - Create window
CWnd::Create( NULL, NULL, WS_VISIBLE | WS_BORDER | WS_CHILD | WS_TABSTOP,
WndRect, pParentWnd, nID );
// Step 3 - Create program list combo
GetClientRect( ClientRect );
CRect ComboRect = CRect( ClientRect.left + 70, ClientRect.top + 40, ClientRect.right - 10, ClientRect.bottom );
m_ProgramList.Create( CBS_AUTOHSCROLL | CBS_DROPDOWNLIST |
WS_CHILD | WS_VISIBLE | WS_VSCROLL, ComboRect, ( CWnd * ) this, ID_PROGRAM_LIST );
// Step 4 - Create buddy window for effort level spin control
CRect EditRect = CRect( ClientRect.left + 70, ClientRect.top + 80, ClientRect.left + 110, ClientRect.top + 105 );
m_EffortEdit.Create( WS_CHILD | WS_TABSTOP | WS_VISIBLE |
WS_BORDER, EditRect, ( CWnd * ) this, ID_EFFORT_LEVEL_EDIT );
m_EffortEdit.SetReadOnly( TRUE );
// Step 5 - Create effort level spin control
CRect SpinRect = CRect( ClientRect.left + 110, ClientRect.top + 80, ClientRect.left + 120, ClientRect.top + 105 );
return m_EffortLevel.Create( WS_CHILD | WS_TABSTOP | WS_VISIBLE |
UDS_ARROWKEYS | UDS_SETBUDDYINT | UDS_AUTOBUDDY | UDS_NOTHOUSANDS,
SpinRect, ( CWnd * ) this, ID_EFFORT_LEVEL_SPIN );
}
void CProgramControl::OnPaint()
{
CRect ClientRect;
CRect DrawRect;
CRect TitleRect;
CString csTitle = "Program Control";
CString csText1 = "Program";
CString csText2 = "Level";
CPaintDC dc(this);
// Step 1 - Set background color, mode and text color
GetClientRect( ClientRect );
dc.FillSolidRect( ClientRect, RGB( 128, 64, 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 title 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 + 10, ClientRect.top + 43, ClientRect.right, ClientRect.bottom );
dc.DrawText( csText1, DrawRect, DT_LEFT | DT_WORDBREAK );
DrawRect = CRect( ClientRect.left + 10, ClientRect.top + 80, ClientRect.right, ClientRect.bottom );
dc.DrawText( csText2, DrawRect, DT_LEFT | DT_WORDBREAK );
}
CComboBox & CProgramControl::GetProgramCombo()
{
// Step 1 - Return reference to program list combo
return m_ProgramList;
}
void CProgramControl::OnProgramChange()
{
CString csProgram;
// Step 1 - Get the newly selected program and update the cardio subject
m_ProgramList.GetLBText( m_ProgramList.GetCurSel(), csProgram );
m_pCardioSubject->SetProgram( csProgram );
}
void CProgramControl::SetEffortLevelRange( INT nMinLevel, INT nMaxLevel, INT nCurrentLevel )
{
// Step 1 - Set effort level range
ASSERT( nCurrentLevel >= nMinLevel && nCurrentLevel <= nMaxLevel );
m_EffortLevel.SetRange( nMinLevel, nMaxLevel );
m_EffortLevel.SetPos( nCurrentLevel );
}
void CProgramControl::OnDeltaposEffortLevel(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
// Step 1 - Calculate the effort level
INT nMinLevel = 0;
INT nMaxLevel = 0;
INT nLevel = pNMUpDown->iPos + pNMUpDown->iDelta;
m_EffortLevel.GetRange( nMinLevel, nMaxLevel );
if( nLevel < nMinLevel )
{
nLevel = nMinLevel;
}
if( nLevel > nMaxLevel )
{
nLevel = nMaxLevel;
}
// Step 3 - Set effort level in the cardio subject
m_pCardioSubject->SetLevel( nLevel );
*pResult = 0;
}