Working with win 32 timers.
-
Hello All, I have a working timer in MFC can anyone tell me how to change it to Win 32.
MFC Timer Code. // Declarations #define ELAPSED_TIMER 101 time_t lStopTime; time_t lStartTime; CTimeSpan cteElapsedTime;
//Start Timer time(&lStartTime); SetTimer(s->m_hWnd,ELAPSED_TIMER, 1000, NULL);
//Kill Timer KillTimer(s->m_hWnd, ELAPSED_TIMER);
//Timer Function void CSDlg::OnTimer(UINT nIDEvent) { if(nIDEvent == ELAPSED_TIMER) { time(&lStopTime); cteElapsedTime = CTimeSpan(lStopTime-lStartTime); CString csElapsedTime; csElapsedTime.Format("%02d:%02d:%02d", cteElapsedTime.GetHours(), cteElapsedTime.GetMinutes(), cteElapsedTime.GetSeconds()); if(IsWindowVisible()) { m_TIME.SetWindowText(csElapsedTime); } } CDialog::OnTimer(nIDEvent); }
How to change it to win 32 application ?? what are the changes which I have to make in the below code????int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. INT_PTR mRet = DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), GetDesktopWindow(), MainDlgProc); return 0; } BOOL CALLBACK MainDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_INITDIALOG: { SetTimer(hwnd,IDT_TIMER, 1000,(TIMERPROC) NULL); } break; case WM_CLOSE: EndDialog(hwnd, 0); break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_EXIT: EndDialog(hwnd, 0); break; case IDC_Start: //Starts the timer on button click break; case IDC_End: //Ends the timer on button click KillTimer(hwnd, IDT_TIMER); break; } default: return FALSE; } return TRUE; }
Thanking you, Suresh HC -
Suresh H wrote:
BOOL CALLBACK MainDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) {
You need to add one more case here,
case WM_TIMER:
if((UINT)wParam== ELAPSED_TIMER)
{
//do your stuff
}Prasad Notifier using ATL | Operator new[],delete[][^]
Hi Prasad, Thanks for the response. In the declaration only I am getting error. But it is working in MFC. Hw to do it in Win32 ???
#define ELAPSED_TIMER 101 time_t lStopTime; time_t lStartTime; CTimeSpan cteElapsedTime;
error C2146: syntax error : missing ';' before identifier 'cteElapsedTime' error C2501: 'CTimeSpan' : missing storage-class or type specifiers fatal error C1004: unexpected end of file found -
Hi Prasad, Thanks for the response. In the declaration only I am getting error. But it is working in MFC. Hw to do it in Win32 ???
#define ELAPSED_TIMER 101 time_t lStopTime; time_t lStartTime; CTimeSpan cteElapsedTime;
error C2146: syntax error : missing ';' before identifier 'cteElapsedTime' error C2501: 'CTimeSpan' : missing storage-class or type specifiers fatal error C1004: unexpected end of file foundSuresh H wrote:
error C2146: syntax error : missing ';' before identifier 'cteElapsedTime' error C2501: 'CTimeSpan' : missing storage-class or type specifiers
Its
MFC
stuff. Usedifftime
instead. See itsMSDN
example.Prasad Notifier using ATL | Operator new[],delete[][^]
-
Suresh H wrote:
error C2146: syntax error : missing ';' before identifier 'cteElapsedTime' error C2501: 'CTimeSpan' : missing storage-class or type specifiers
Its
MFC
stuff. Usedifftime
instead. See itsMSDN
example.Prasad Notifier using ATL | Operator new[],delete[][^]
Hi Prasad, No much information in MSDN. i got this link for win32 timers. http://msdn2.microsoft.com/en-us/library/ms632592.aspx
-
Hi Prasad, No much information in MSDN. i got this link for win32 timers. http://msdn2.microsoft.com/en-us/library/ms632592.aspx
I was taking about difftime[^] example from
MSDN
.Prasad Notifier using ATL | Operator new[],delete[][^]
-
I was taking about difftime[^] example from
MSDN
.Prasad Notifier using ATL | Operator new[],delete[][^]
Prasad example gives the time diffrence only.
int main( void ) { time_t start, finish; long loop; double result, elapsed_time; printf( "Multiplying 2 floating point numbers 500 million times...\n" ); time( &start ); for( loop = 0; loop < 500000000; loop++ ) result = 3.63 * 5.27; time( &finish ); elapsed_time = difftime( finish, start ); printf( "\nProgram takes %6.0f seconds.\n", elapsed_time ); }
-
Prasad example gives the time diffrence only.
int main( void ) { time_t start, finish; long loop; double result, elapsed_time; printf( "Multiplying 2 floating point numbers 500 million times...\n" ); time( &start ); for( loop = 0; loop < 500000000; loop++ ) result = 3.63 * 5.27; time( &finish ); elapsed_time = difftime( finish, start ); printf( "\nProgram takes %6.0f seconds.\n", elapsed_time ); }
You need to use one of time management[^] function.
difftime
is used as substitue forCTimeSpan
.Prasad Notifier using ATL | Operator new[],delete[][^]
-
Hello All, I have a working timer in MFC can anyone tell me how to change it to Win 32.
MFC Timer Code. // Declarations #define ELAPSED_TIMER 101 time_t lStopTime; time_t lStartTime; CTimeSpan cteElapsedTime;
//Start Timer time(&lStartTime); SetTimer(s->m_hWnd,ELAPSED_TIMER, 1000, NULL);
//Kill Timer KillTimer(s->m_hWnd, ELAPSED_TIMER);
//Timer Function void CSDlg::OnTimer(UINT nIDEvent) { if(nIDEvent == ELAPSED_TIMER) { time(&lStopTime); cteElapsedTime = CTimeSpan(lStopTime-lStartTime); CString csElapsedTime; csElapsedTime.Format("%02d:%02d:%02d", cteElapsedTime.GetHours(), cteElapsedTime.GetMinutes(), cteElapsedTime.GetSeconds()); if(IsWindowVisible()) { m_TIME.SetWindowText(csElapsedTime); } } CDialog::OnTimer(nIDEvent); }
How to change it to win 32 application ?? what are the changes which I have to make in the below code????int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. INT_PTR mRet = DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), GetDesktopWindow(), MainDlgProc); return 0; } BOOL CALLBACK MainDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_INITDIALOG: { SetTimer(hwnd,IDT_TIMER, 1000,(TIMERPROC) NULL); } break; case WM_CLOSE: EndDialog(hwnd, 0); break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_EXIT: EndDialog(hwnd, 0); break; case IDC_Start: //Starts the timer on button click break; case IDC_End: //Ends the timer on button click KillTimer(hwnd, IDT_TIMER); break; } default: return FALSE; } return TRUE; }
Thanking you, Suresh HC -
-
You're welcome ;)
WhiteSky