Click here to Skip to main content
15,888,527 members
Articles / Programming Languages / Objective C

CMultimediaTimer - A Periodic Timer Class

Rate me:
Please Sign up or sign in to vote.
4.13/5 (7 votes)
27 Mar 20021 min read 71.5K   1.7K   33  
CMultimediaTimer implements a periodic timer using the Windows Multimedia Timer API
// MMTimerTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MMTimerTest.h"
#include "MMTimerTestDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMMTimerTestDlg dialog

CMMTimerTestDlg::CMMTimerTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMMTimerTestDlg::IDD, pParent),
		m_Callback1(m_Slider1),m_Timer1(m_Callback1),
		m_Callback2(m_Slider2),m_Timer2(m_Callback2),
		m_Callback3(m_Slider3),m_Timer3(m_Callback3),
		m_Callback4(m_Slider4),m_Timer4(m_Callback4),
		m_Callback5(m_Slider5),m_Timer5(m_Callback5),
		m_Callback6(m_Slider6),m_Timer6(m_Callback6)
{
	//{{AFX_DATA_INIT(CMMTimerTestDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMMTimerTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMMTimerTestDlg)
	DDX_Control(pDX, IDC_SLIDER1, m_Slider1);
	DDX_Control(pDX, IDC_SLIDER2, m_Slider2);
	DDX_Control(pDX, IDC_SLIDER3, m_Slider3);
	DDX_Control(pDX, IDC_SLIDER4, m_Slider4);
	DDX_Control(pDX, IDC_SLIDER5, m_Slider5);
	DDX_Control(pDX, IDC_SLIDER6, m_Slider6);
	DDX_Control(pDX, IDC_BUTTON1, m_Button1);
	DDX_Control(pDX, IDC_BUTTON2, m_Button2);
	DDX_Control(pDX, IDC_BUTTON3, m_Button3);
	DDX_Control(pDX, IDC_BUTTON4, m_Button4);
	DDX_Control(pDX, IDC_BUTTON5, m_Button5);
	DDX_Control(pDX, IDC_BUTTON6, m_Button6);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMMTimerTestDlg, CDialog)
	//{{AFX_MSG_MAP(CMMTimerTestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMMTimerTestDlg message handlers

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

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CMMTimerTestDlg::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();
	}
}

HCURSOR CMMTimerTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CMMTimerTestDlg::HandleButton(CButton& button,CMultimediaTimer& timer)
{
	if(timer.Active())
		timer.Stop();
	else
		timer.Start(100,5);//Run at 10Hz

	if(timer.Active())
		button.SetWindowText(_T("Stop"));
	else
		button.SetWindowText(_T("Start"));
}


void CMMTimerTestDlg::OnButton1() 
{	
	HandleButton(m_Button1,m_Timer1);	
}

void CMMTimerTestDlg::OnButton2() 
{	
	HandleButton(m_Button2,m_Timer2);	
}

void CMMTimerTestDlg::OnButton3() 
{	
	HandleButton(m_Button3,m_Timer3);	
}

void CMMTimerTestDlg::OnButton4() 
{	
	HandleButton(m_Button4,m_Timer4);	
}

void CMMTimerTestDlg::OnButton5() 
{	
	HandleButton(m_Button5,m_Timer5);	
}

void CMMTimerTestDlg::OnButton6() 
{	
	HandleButton(m_Button6,m_Timer6);	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
Australia Australia
John Curtis runs Fatlab Software Pty. Ltd. which provides software services to companies in Australia and is currently developing a range of productivity apps.
He has 20+ years experience writing C++ software and can write Java, C#, Javascript when needed!
Specialities include IOT device code, Realtime control, Embedded Linux, Bare bones....

Loves playing with Arduino and Raspberry Pi based solutions for small scale problems.

Comments and Discussions