CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 1999
    Posts
    3

    OnTimer( ) Please help!!

    Modify or write additional header or C++ program code for the program mfc02 such that,
    when the window is closed, the program will display a message box that reports the
    signature of OnTimer function. In your own words please explain how and where
    the data type of the variable holding the signature was declared in the program below
    and what is the purpose of the signature map.
    [code]
    // MFC02.H

    ////////////////////////////////////
    //// DECLARATION ////
    ////////////////////////////////////

    // derive a window class from CFrameWND
    // Clock Program
    class CMainWinublic CFrameWnd
    {
    public:
    CMainWin();
    afx_msg void OnPaint();
    afx_msg void OnTimer(UINT ID);
    afx_msg void OnDestroy();
    DECLARE_MESSAGE_MAP();
    };

    // derive an application class from CWinApp
    class CApp : public CWinApp
    {
    public: // use default constructor
    BOOL InitInstance();
    };

    // MFC02.cpp
    #include <afxwin.h>
    #include "MFC02.h"
    #include <string.h>
    #include <time.h>

    ///////////////////////////////////////
    char str[80]; // output string

    CApp App; // Application Object
    ///////////////////////////////////////
    //CApp member functions

    CMainWin::CMainWin()
    {
    RECT r;
    r.left = r.top = 10;
    r.right = 200;
    r.bottom = 200;
    Create (NULL, "Clock Program", WS_OVERLAPPEDWINDOW,r);
    }

    BOOL CApp::InitInstance()
    {
    m_pMainWnd = new CMainWin();
    //Start Timer
    if (m_pMainWnd -> SetTimer(1,1000,NULL) !=1) return FALSE;
    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
    }
    ////////////////////////////////////

    BEGIN_MESSAGE_MAP (CMainWin, CFrameWnd)

    ON_WM_PAINT()
    ON_WM_TIMER()
    ON_WM_DESTROY()
    END_MESSAGE_MAP()



    afx_msg void CMainWin :: OnPaint()
    {

    CPaintDC dc(this);
    dc.TextOut(1,1,str,strlen(str));
    }



    afx_msg void CMainWin :: OnTimer(UINT ID)
    {
    CTime curtime = CTime::GetCurrentTime();
    struct tm*newtime;

    newtime = curtime.GetLocalTm();
    strcpy(str,asctime(newtime));
    str[strlen(str)-1]='\0'; //remove \n
    InvalidateRect(NULL,0);

    }


    // Exit the application
    afx_msg void CMainWin :: OnDestroy()
    {
    KillTimer (1);
    }

    [/ccode]



  2. #2
    Join Date
    May 1999
    Location
    Atlanta, GA, USA
    Posts
    443

    Re: OnTimer( ) Please help!!

    Hi.

    Your mistake is that you put SetTimer func at the wrong place.
    You should put SetTimer in CMainWin::OnCreate().

    You put WM_TIMER message at CMainWin message map and this is right.
    But even though you put SetTimer in CApp, it is not related to
    the message map of CMainWin.

    Hope for help.
    -Masaaki Onishi-


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured