I am using the Timer in my application. I displayed the current system time and currently running application time using the
SetTimer(1,999,NULL)
. I created one text box and start button.
When I enter the integer value in the text box (ex: 10) and press the start button, the message box should pop-up after 10 seconds. For this I created another
SetTimer(2,m_Sec*1000,NULL)
. Everything was fine but when I execute the application for every sec the text box is refreshed.
I can't enter the value in that text box.
What changes I have to make.
Here is my code.
m_Sec
is the
UINT
variable of the text box.
#include "stdafx.h"
#include "TickCounter.h"
#include "TickCounterDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CTickCounterDlg::CTickCounterDlg(CWnd* pParent )
: CDialog(CTickCounterDlg::IDD, pParent)
, m_Comp(_T(""))
, m_App(_T(""))
, m_Sec(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTickCounterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_COMP_TIME, m_CompTime);
DDX_Control(pDX, IDC_APP_TIME, m_AppTime);
DDX_Text(pDX, IDC_COMP_TIME, m_Comp);
DDX_Text(pDX, IDC_APP_TIME, m_App);
DDX_Text(pDX, IDC_SEC, m_Sec);
}
BEGIN_MESSAGE_MAP(CTickCounterDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_START, &CTickCounterDlg::OnBnClickedStart)
END_MESSAGE_MAP()
BOOL CTickCounterDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE);
CompTime = CTime::GetTickCount();
SetTimer(1, 999, NULL);
SetTimer(2,10000,NULL);
return TRUE; }
void CTickCounterDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<wparam>(dc.GetSafeHdc()), 0);
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;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CTickCounterDlg::OnQueryDragIcon()
{
return static_cast<hcursor>(m_hIcon);
}
void CTickCounterDlg::OnTimer(UINT_PTR nIDEvent)
{
if(nIDEvent==1)
{
CTime CurTime=CTime::GetTickCount();
CTimeSpan Difference=CurTime-CompTime;
unsigned int compHour,compMin,compSec;
unsigned int curSec,curMin,curHour;
compHour=Difference.GetHours();
compMin=Difference.GetMinutes();
compSec=Difference.GetSeconds();
curHour=CurTime.GetHour();
curMin=CurTime.GetMinute();
curSec=CurTime.GetSecond();
m_Comp.Format(L"This computer current Time is %d hours, %d minutes %d seconds", curHour, curMin, curSec);
m_App.Format(L"This application has been running for %d hours, %d minutes %d seconds", compHour, compMin, compSec);
}
if(nIDEvent==2)
{
MessageBox(L"hai");
KillTimer(2);
}
UpdateData(FALSE);
CDialog::OnTimer(nIDEvent);
}
void CTickCounterDlg::OnBnClickedStart()
{
UpdateData();
SetTimer(2,m_Sec*1000,NULL);
}