Activate the Timer operation
Return to Main

Resume :

  • To create a new class.
  • To add a member variable.
  • Activate the Timer operation, to add 3 member functions.
  • Details ...

    1. Manage the IDD_BIOGRAPHY dialog, create a new class.
    2. On the View menu, click ClassWizard. Click ClassWizard's Add Class button, and on the menu click New.
    3. In the New Class dialog box, add the following data:
      Class type:
      Class name:
      File name:
      Base class:
      Dialod ID:
      MFC Class
      CBiography
      Biography
      CDialog
      IDD_BIOGRAPHY
    4. Click Ok

      ClassWizard create the files - Biography.h and Biography.cpp after you’ve created the class. Examine these Code files.

      -
      New, Biography.h file:

      #if !defined(AFX_BIOGRAPHY_H__3265EB26_1EAB_11D7_BFCA_FBE81CB16F4A__INCLUDED_)
      #define AFX_BIOGRAPHY_H__3265EB26_1EAB_11D7_BFCA_FBE81CB16F4A__INCLUDED_

      #if _MSC_VER > 1000
      #pragma once
      #endif // _MSC_VER > 1000
      // Biography.h : header file
      //

      /////////////////////////////////////////////////////////////////////////////
      // CBiography dialog

      class CBiography : public CDialog
      {
      // Construction
      public:
      CBiography(CWnd* pParent = NULL); // standard constructor

      // Dialog Data
      //{{AFX_DATA(CBiography)
      enum { IDD = IDD_BIOGRAPHY };
      // NOTE: the ClassWizard will add data members here
      //}}AFX_DATA


      // Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CBiography)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
      //}}AFX_VIRTUAL

      // Implementation
      protected:

      // Generated message map functions
      //{{AFX_MSG(CBiography)
      // NOTE: the ClassWizard will add member functions here
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
      };

      //{{AFX_INSERT_LOCATION}}
      // Microsoft Visual C++ will insert additional declarations immediately before the previous line.

      #endif // !defined(AFX_BIOGRAPHY_H__3265EB26_1EAB_11D7_BFCA_FBE81CB16F4A__INCLUDED_)


      - New, Biography.cpp file:

      // Biography.cpp : implementation file
      //

      #include "stdafx.h"
      #include "TestVC0.h"
      #include "Biography.h"

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

      /////////////////////////////////////////////////////////////////////////////
      // CBiography dialog


      CBiography::CBiography(CWnd* pParent /*=NULL*/)
      : CDialog(CBiography::IDD, pParent)
      {
      //{{AFX_DATA_INIT(CBiography)
      // NOTE: the ClassWizard will add member initialization here
      //}}AFX_DATA_INIT
      }


      void CBiography::DoDataExchange(CDataExchange* pDX)
      {
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CBiography)
      // NOTE: the ClassWizard will add DDX and DDV calls here
      //}}AFX_DATA_MAP
      }

      BEGIN_MESSAGE_MAP(CBiography, CDialog)
      //{{AFX_MSG_MAP(CBiography)
      // NOTE: the ClassWizard will add message map macros here
      //}}AFX_MSG_MAP
      END_MESSAGE_MAP()

      /////////////////////////////////////////////////////////////////////////////
      // CBiography message handlers


    5. Add a member Varaiable.

      1. On the View menu, click ClassWizard.
        The MFC ClassWizard dialog box appears, click the Member Variables tab.

        Note:
        This tab, contains a list box displaying the mapping between controls and member variables.
      2. In the Class name box, select the class CBiography.
      3. Select the name of the Control ID you want to add a variable - IDC_ STATICBITMAP.
      4. Click Add Variable, the Add Member Variable dialog box appears.
      5. In the Member variable name text box, type the name of the variable - m_bitmap.
      6. In the Category drop-down list, select the appropriate type of variable - Control.
      7. In the Variable type drop-down list, select the appropriate class that defines the
        variable's data type - CStatic.
      8. Click OK.

        Note:
        The member name and type you specified now appear in the Control IDs list.

      ClassWizard makes changes to Biography.h and Biography.cpp files after you’ve mapped the controls to a member variable. Examine these changes.

      - Biography.h
      file:
      - the new Text Code is red

      #if !defined(AFX_BIOGRAPHY_H__3265EB26_1EAB_11D7_BFCA_FBE81CB16F4A__INCLUDED_)
      #define AFX_BIOGRAPHY_H__3265EB26_1EAB_11D7_BFCA_FBE81CB16F4A__INCLUDED_

      #if _MSC_VER > 1000
      #pragma once
      #endif // _MSC_VER > 1000
      // Biography.h : header file
      //

      /////////////////////////////////////////////////////////////////////////////
      // CBiography dialog

      class CBiography : public CDialog
      {
      // Construction
      public:
      CBiography(CWnd* pParent = NULL); // standard constructor

      // Dialog Data
      //{{AFX_DATA(CBiography)
      enum { IDD = IDD_BIOGRAPHY };
      CStatic
      m_bitmap;
      //}}AFX_DATA

      // Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CBiography)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
      //}}AFX_VIRTUAL

      // Implementation
      protected:

      // Generated message map functions
      //{{AFX_MSG(CBiography)
      // NOTE: the ClassWizard will add member functions here
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
      };

      //{{AFX_INSERT_LOCATION}}
      // Microsoft Visual C++ will insert additional declarations immediately before the previous line.

      #endif // !defined(AFX_BIOGRAPHY_H__3265EB26_1EAB_11D7_BFCA_FBE81CB16F4A__INCLUDED_)


      - Biography.cpp file: - the new Text Code is red

      // Biography.cpp : implementation file
      //

      #include "stdafx.h"
      #include "TestVC0.h"
      #include "Biography.h"

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

      /////////////////////////////////////////////////////////////////////////////
      // CBiography dialog


      CBiography::CBiography(CWnd* pParent /*=NULL*/)
      : CDialog(CBiography::IDD, pParent)
      {
      //{{AFX_DATA_INIT(CBiography)
      // NOTE: the ClassWizard will add member initialization here
      //}}AFX_DATA_INIT
      }


      void CBiography::DoDataExchange(CDataExchange* pDX)
      {
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CBiography)
      DDX_Control(pDX, IDC_STATICBITMAP, m_bitmap);
      //}}AFX_DATA_MAP
      }

      BEGIN_MESSAGE_MAP(CBiography, CDialog)
      //{{AFX_MSG_MAP(CBiography)
      // NOTE: the ClassWizard will add message map macros here
      //}}AFX_MSG_MAP
      END_MESSAGE_MAP()

      /////////////////////////////////////////////////////////////////////////////
      // CBiography message handlers


    6. Activate the Timer opreation ...
      1. ClassWizard, Add 3 member functions
      2. On the View menu, click ClassWizard.
        The MFC ClassWizard dialog box appears, click the Message Maps tab.
      3. In the Class name box, select the class CBiography.
      4. In the Object IDs list, select the CBiography.
      5. For WM_INITDIALOG, WM_DESTROY and WM_TIMER functions:
        1. WM_INITDIALOG - set to a dialog box before the dialog box is displayed.
          • In the Messages list, select the WM_INITDIALOG.
          • Click Add Function.
          • The Add Member Function dialog box appears, click OK.
            To accept the default Member function name -
            OnInitDialog display(or rename it) and then click OK.
            The new item message -
            OnInitDialog...... ON_WM_INITDIALOG appearing in the Member functions list.
        2. WM_DESTROY - indicates window is about to be destroyed.
          • In the Messages list, select the WM_DESTROY.
          • Click Add Function.
          • The Add Member Function dialog box appears, click OK.
            To accept the default Member function name - OnDestroy
            g display(or rename it) and then click OK.
            The new item message - OnDestroy
            ...... ON_WM_DESTROY appearing in the Member functions list.
        3. WM_TIMER - indicates timeout interval for a timer has elapsed.
          • In the Messages list, select the WM_TIMER.
          • Click Add Function.
          • The Add Member Function dialog box appears, click OK.
            To accept the default Member function name - OnTimer
            display(or rename it) and then click OK.
            The new item message - OnTimer
            ...... ON_WM_TIMER appearing in the Member functions list.

        ClassWizard makes changes to Biography.h and Biography.cpp files after you’ve added the member functions. Examine these changes ...

        - Biography.h
        file:
        - the new Text Code is red

        #if !defined(AFX_BIOGRAPHY_H__3265EB26_1EAB_11D7_BFCA_FBE81CB16F4A__INCLUDED_)
        #define AFX_BIOGRAPHY_H__3265EB26_1EAB_11D7_BFCA_FBE81CB16F4A__INCLUDED_

        #if _MSC_VER > 1000
        #pragma once
        #endif // _MSC_VER > 1000
        // Biography.h : header file
        //

        /////////////////////////////////////////////////////////////////////////////
        // CBiography dialog

        class CBiography : public CDialog
        {
        // Construction
        public:
        CBiography(CWnd* pParent = NULL); // standard constructor

        // Dialog Data
        //{{AFX_DATA(CBiography)
        enum { IDD = IDD_BIOGRAPHY };
        CStatic m_bitmap;
        //}}AFX_DATA

        // Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CBiography)
        protected:
        virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
        //}}AFX_VIRTUAL

        // Implementation
        protected:

        // Generated message map functions
        //{{AFX_MSG(CBiography)
        virtual BOOL OnInitDialog();
        afx_msg void
        OnDestroy();
        afx_msg void
        OnTimer(UINT nIDEvent);
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
        };

        //{{AFX_INSERT_LOCATION}}
        // Microsoft Visual C++ will insert additional declarations immediately before the previous line.

        #endif // !defined(AFX_BIOGRAPHY_H__3265EB26_1EAB_11D7_BFCA_FBE81CB16F4A__INCLUDED_)


        - Biography.cpp file: - the new Text Code is red

        // Biography.cpp : implementation file
        //

        #include "stdafx.h"
        #include "TestVC0.h"
        #include "Biography.h"

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

        /////////////////////////////////////////////////////////////////////////////
        // CBiography dialog


        CBiography::CBiography(CWnd* pParent /*=NULL*/)
        : CDialog(CBiography::IDD, pParent)
        {
        //{{AFX_DATA_INIT(CBiography)
        // NOTE: the ClassWizard will add member initialization here
        //}}AFX_DATA_INIT
        }


        void CBiography::DoDataExchange(CDataExchange* pDX)
        {
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CBiography)
        DDX_Control(pDX, IDC_STATICBITMAP, m_bitmap);
        //}}AFX_DATA_MAP
        }

        BEGIN_MESSAGE_MAP(CBiography, CDialog)
        //{{AFX_MSG_MAP(CBiography)
        // NOTE: the ClassWizard will add message map macros here
        //}}AFX_MSG_MAP
        END_MESSAGE_MAP()

        /////////////////////////////////////////////////////////////////////////////
        // CBiography message handlers
        BOOL CBiography::OnInitDialog()
        {
        CDialog::OnInitDialog();
        // TODO: Add extra initialization here

        return TRUE; // return TRUE unless you set the focus to a control
        // EXCEPTION: OCX Property Pages should return FALSE
        }

        void CBiography::OnDestroy()
        {
        CDialog::OnDestroy();

        // TODO: Add your message handler code here

        }

        void CBiography::OnTimer(UINT nIDEvent)
        {
        // TODO: Add your message handler code here and/or call default

        CDialog::OnTimer(nIDEvent);
        }

      6. ClassWizard, Edit the Code - (functions - OnInitDialog, OnDestroy(), OnTimer):
      7. In the ClassWizard dialog box, select the Message Maps tab and in the Class Name box,
        select the class CBiography.
      8. In the Member Functions list, select the function name - OnInitDialog:
        Choose Edit Code
        -or-
        Double-click the function name.
        The insertion point moves to the function in the Biography.cpp file. Edit the Text Code.
        Examine these changes ...

        Note:
        Repeat
        the last step for these functions - OnDestroy, OnTimer.

        - Biography.cpp file: - the new Text Code is red

        // Biography.cpp : implementation file
        //

        #include "stdafx.h"
        #include "TestVC0.h"
        #include "Biography.h"

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

        /////////////////////////////////////////////////////////////////////////////
        // CBiography dialog


        CBiography::CBiography(CWnd* pParent /*=NULL*/)
        : CDialog(CBiography::IDD, pParent)
        {
        //{{AFX_DATA_INIT(CBiography)
        // NOTE: the ClassWizard will add member initialization here
        //}}AFX_DATA_INIT
        }


        void CBiography::DoDataExchange(CDataExchange* pDX)
        {
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CBiography)
        DDX_Control(pDX, IDC_STATICBITMAP, m_bitmap);
        //}}AFX_DATA_MAP
        }

        BEGIN_MESSAGE_MAP(CBiography, CDialog)
        //{{AFX_MSG_MAP(CBiography)
        // NOTE: the ClassWizard will add message map macros here
        //}}AFX_MSG_MAP
        END_MESSAGE_MAP()

        /////////////////////////////////////////////////////////////////////////////
        // CBiography message handlers
        BOOL CBiography::OnInitDialog()
        {
        CDialog::OnInitDialog();
        m_bitmapbutterfly1.LoadBitmap(IDB_BUTTERFLY1);
        m_bitmapbutterfly2.LoadBitmap(IDB_BUTTERFLY2);

        m_Timer =
        SetTimer(IDD_BIOGRAPHY,1,NULL);
        // TODO: Add extra initialization here

        return TRUE; // return TRUE unless you set the focus to a control
        // EXCEPTION: OCX Property Pages should return FALSE
        }

        void CBiography::OnDestroy()
        {
        CDialog::OnDestroy();
        KillTimer
        (m_Timer);
        // TODO: Add your message handler code here

        }

        void CBiography::OnTimer(UINT nIDEvent)
        {
        tm
        *vtma;
        time_t vtime_ta;

        time(&vtime_ta);
        vtma = localtime(&vtime_ta);

        if ((vtma->tm_sec % 2) == 0)
        m_bitmap.SetBitmap(m_bitmapbutterfly1);
        else
        m_bitmap.SetBitmap(m_bitmapbutterfly2);

        // TODO: Add your message handler code here and/or call default

        CDialog::OnTimer(nIDEvent);
        }


        Note:
        Objects declaration, examine these changes to Biography.h file: -
        the new Text Code is red

        #if !defined(AFX_BIOGRAPHY_H__3265EB26_1EAB_11D7_BFCA_FBE81CB16F4A__INCLUDED_)
        #define AFX_BIOGRAPHY_H__3265EB26_1EAB_11D7_BFCA_FBE81CB16F4A__INCLUDED_

        #if _MSC_VER > 1000
        #pragma once
        #endif // _MSC_VER > 1000
        // Biography.h : header file
        //

        /////////////////////////////////////////////////////////////////////////////
        // CBiography dialog

        class CBiography : public CDialog
        {
        // Construction
        public:
        CBiography(CWnd* pParent = NULL); // standard constructor

        // Dialog Data
        //{{AFX_DATA(CBiography)
        enum { IDD = IDD_BIOGRAPHY };
        CStatic m_bitmap;
        //}}AFX_DATA

        // Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CBiography)
        protected:
        virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
        //}}AFX_VIRTUAL

        // Implementation
        protected:
        UINT m_Timer;
        CBitmap m_bitmapbutterfly1, m_bitmapbutterfly2;

        // Generated message map functions
        //{{AFX_MSG(CBiography)
        virtual BOOL OnInitDialog();
        afx_msg void OnDestroy();
        afx_msg void OnTimer(UINT nIDEvent);
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
        };

        //{{AFX_INSERT_LOCATION}}
        // Microsoft Visual C++ will insert additional declarations immediately before the previous line.

        #endif // !defined(AFX_BIOGRAPHY_H__3265EB26_1EAB_11D7_BFCA_FBE81CB16F4A__INCLUDED_)
    Return to Main