Can not find the problem

Hello Everyone,

I have been working with my code to have a message box pop up after 20 Seconds asking to continue the program and of course it does not work and I am not sure why? I am using 2 timers and it is just a basic clock program to display the message box. Can someone tell me from my code where I am going wrong and what I can do to correct this. It has been driving me nuts and the books that I have do not explain the timers in a way that I understand. Thanks for the help in advance!

[code]
// A clock program

#include "stdafx.h"
#include
#include
#include
#include
#include "message5.h"


char str[80] = ""; // holds output string


CMainWin::CMainWin()
{
RECT r;

r.left = r.top = 10;
r.right = 250;
r.bottom = 60;

Create(NULL, "A Clock Program Modified", WS_OVERLAPPEDWINDOW, r);
}

// Initialize the application.
BOOL CApp::InitInstance()
{
m_pMainWnd = new CMainWin;

// start the timer
if((m_pMainWnd->SetTimer(1, 1000, NULL) != 1) &&
(m_pMainWnd->SetTimer(2, 5000, NULL))) return FALSE;



m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();

return TRUE;
}




// This is the application's message map.
BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_DESTROY()
ON_WM_CLOSE()
END_MESSAGE_MAP()

// Process a WM_PAINT message.
afx_msg void CMainWin::OnPaint()
{
CPaintDC dc(this);

// this line redisplays the last character
dc.TextOut(1, 1, str, strlen(str));
}



// Process WM_TIMER messages
afx_msg void CMainWin::OnTimer(UINT nTimerID)
{

CTime curtime = CTime::GetCurrentTime();
struct tm *newtime;

newtime = curtime.GetLocalTm();

wsprintf(str, asctime(newtime));
str[strlen(str)-1] = ''; // remove /r/n
InvalidateRect(NULL, 0);



if(nTimerID == 2 )
{
int i;


i = MessageBox("20 Seconds has pasted", "Would you like to continue?",
MB_ICONQUESTION | MB_YESNO);

switch(i) {
case IDYES:


break;
case IDNO:
MessageBox("Program Stopped", "No Selected", MB_OK);
OnClose( );

break;

}

}



}

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

CApp App; // instantiate the application


[/code]

And here is the header

[code]
// MESSAGE5.H

// This is the main window class.

class CMainWin : public CFrameWnd
{
public:
CMainWin();
afx_msg void OnPaint();
afx_msg void OnTimer(UINT nTimerID);
afx_msg void OnDestroy();
DECLARE_MESSAGE_MAP()
};

// This is the application class.
class CApp : public CWinApp
{
public:
BOOL InitInstance();
};

[/code]

Thanks again! :)

Comments

  • : Hello Everyone,
    :
    : I have been working with my code to have a message box pop up after 20 Seconds asking to continue the program and of course it does not work and I am not sure why? I am using 2 timers and it is just a basic clock program to display the message box. Can someone tell me from my code where I am going wrong and what I can do to correct this. It has been driving me nuts and the books that I have do not explain the timers in a way that I understand. Thanks for the help in advance!
    :
    : [code]
    : // A clock program
    :
    : #include "stdafx.h"
    : #include
    : #include
    : #include
    : #include
    : #include "message5.h"
    :
    :
    : char str[80] = ""; // holds output string
    :
    :
    : CMainWin::CMainWin()
    : {
    : RECT r;
    :
    : r.left = r.top = 10;
    : r.right = 250;
    : r.bottom = 60;
    :
    : Create(NULL, "A Clock Program Modified", WS_OVERLAPPEDWINDOW, r);
    : }
    :
    : // Initialize the application.
    : BOOL CApp::InitInstance()
    : {
    : m_pMainWnd = new CMainWin;
    :
    : // start the timer
    : if((m_pMainWnd->SetTimer(1, 1000, NULL) != 1) &&
    : (m_pMainWnd->SetTimer(2, 5000, NULL))) return FALSE;
    :
    :
    :
    : m_pMainWnd->ShowWindow(m_nCmdShow);
    : m_pMainWnd->UpdateWindow();
    :
    : return TRUE;
    : }
    :
    :
    :
    :
    : // This is the application's message map.
    : BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)
    : ON_WM_PAINT()
    : ON_WM_TIMER()
    : ON_WM_DESTROY()
    : ON_WM_CLOSE()
    : END_MESSAGE_MAP()
    :
    : // Process a WM_PAINT message.
    : afx_msg void CMainWin::OnPaint()
    : {
    : CPaintDC dc(this);
    :
    : // this line redisplays the last character
    : dc.TextOut(1, 1, str, strlen(str));
    : }
    :
    :
    :
    : // Process WM_TIMER messages
    : afx_msg void CMainWin::OnTimer(UINT nTimerID)
    : {
    :
    : CTime curtime = CTime::GetCurrentTime();
    : struct tm *newtime;
    :
    : newtime = curtime.GetLocalTm();
    :
    : wsprintf(str, asctime(newtime));
    : str[strlen(str)-1] = ''; // remove /r/n
    : InvalidateRect(NULL, 0);
    :
    :
    :
    : if(nTimerID == 2 )
    : {
    : int i;
    :
    :
    : i = MessageBox("20 Seconds has pasted", "Would you like to continue?",
    : MB_ICONQUESTION | MB_YESNO);
    :
    : switch(i) {
    : case IDYES:
    :
    :
    : break;
    : case IDNO:
    : MessageBox("Program Stopped", "No Selected", MB_OK);
    : OnClose( );
    :
    : break;
    :
    : }
    :
    : }
    :
    :
    :
    : }
    :
    : // Exit the application
    : afx_msg void CMainWin::OnDestroy()
    : {
    : KillTimer(1);
    : }
    :
    : CApp App; // instantiate the application
    :
    :
    : [/code]
    :
    : And here is the header
    :
    : [code]
    : // MESSAGE5.H
    :
    : // This is the main window class.
    :
    : class CMainWin : public CFrameWnd
    : {
    : public:
    : CMainWin();
    : afx_msg void OnPaint();
    : afx_msg void OnTimer(UINT nTimerID);
    : afx_msg void OnDestroy();
    : DECLARE_MESSAGE_MAP()
    : };
    :
    : // This is the application class.
    : class CApp : public CWinApp
    : {
    : public:
    : BOOL InitInstance();
    : };
    :
    : [/code]
    :
    : Thanks again! :)
    :
    what does the error tells you?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion