MFC Step-by-Step Guide

Tutorial 4

Application Defined Events


GOAL: Experience event-driven application with a Timer and Mouse events
PREREQUISITES: Tutorial 3

Add A Timer
  1. Add a Text Label to echo the Timer
  2. Add an output Control Variable to the Text Label, with
    1. Access to private
    2. Category to Value
    3. Variable Name to "m_TimerEcho"
    4. Variable Type to CString
  3. Create Service Routine as "WM_TIMER" and click "<Add> OnTimer"
  4. In 'TutorialDlg.h', create a private variable int m_Seconds;
  5. In 'TutorialDlg.cpp', initialize the timer inside the OnInitDialog() function:
    BOOL CTutorialDlg::OnInitDialog(){
       ...
       m_Seconds = 0;
       SetTimer(0, 1000, NULL);
    }
  6. Now inside the function code block for OnTimer(...), add the following code:
    void CTutorialDlg::OnTimer(UINT nIDEvent){
       m_Seconds++;
       m_TimerEcho.Format("%d: Seconds have passed", m_Seconds);
       UpdateData(FALSE);
    }
Create Mouse Echo Text
  1. Add a Text Label to echo the Mouse position
  2. Add an output Control Variable to the Text Label, with
    1. Access to private
    2. Category to Value
    3. Variable Name to "m_MouseEcho"
    4. Variable Type to CString
Left-Mouse-Button Event
  1. Create Service Routine as "WM_LBUTTONDOWN" and click "<Add> OnLButtonDown"
  2. Inside 'TutorialDlg.cpp' under the OnLButtonDown(...), add
    void CTutorialDlg::OnLButtonDown(UINT nFlags, CPoint point){
       ...
       m_MouseEcho.Format("Left mouse down at %d,%d", point.x, point.y);
       UpdateData(FALSE);
    }
Right-Mouse-Button Event
  1. Create Service Routine as "WM_RBUTTONDOWN" and click "<Add> OnRButtonDown"
  2. Inside 'TutorialDlg.cpp' under the OnRButtonDown(...), add
    void CTutorialDlg::OnRButtonDown(UINT nFlags, CPoint point){
       ...
       m_MouseEcho.Format("Right mouse down at %d,%d", point.x, point.y);
       UpdateData(FALSE);
    }
Mouse Move Event
  1. Create Service Routine as "WM_MOUSEMOVE" and click "<Add> OnMouseMove"
  2. Inside 'TutorialDlg.cpp' under the OnMouseMove(...), add
    void CTutorialDlg::OnMouseMove(UINT nFlags, CPoint point){
       ...
       m_MouseEcho.Format("Mouse Move at %d,%d", point.x, point.y);
       UpdateData(fFALSE);
    }


» Written by William Frankhouser (wjf2@washington.edu)
» Advised by Kelvin Sung (ksung@washington.edu) as part of the project sponsored by the National Science Foundation under Grant No. 0442420. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
» Produced in the "Essential Concepts for Building Interactive Computer Graphics Applications", A.K. Peters, Ltd.