-
using setTime format
Hey everyone, Im trying to add a clock to my program in the status bar that just shows how long the user has been using the program. so itll be basically a timer, so when the user first executes the program itll start and look like this: 0:00:00. Then after 1 second, itll look like this 0:00:01 and so on.
Anyways, I went on www.msdn.com and checked out the library and the only function i could see that would be of any use to me was the setTime format. I was wondering if I am on the right track and how would i add it to the status bar.
Thanks a lot!
-
How about:
Code:
char buffer[50];
int hours = seconds_count / 3600; /* 3600 seconds in an hour. */
int minutes = (seconds_count % 3600) / 60; /* Get remainder after removing hours
* and divide by 60 to get minutes. */
int seconds = (seconds_count % 60); /* Get remainder after removing minutes. */
sprintf(buffer, "%d:%02d:%02d", hours, minutes, seconds);
SetWindowText(hwndStatus, buffer);
-
How do i make it tick. Like itll start at 00:00:00, how do i make it change it 00:00:01, then 00:00:02 and so on?
-
Use a timer.
Setup:
Code:
/* Get the start time. */
g_timeStart = time();
/* Set a timer to fire every second (1000 milliseconds) */
SetTimer(NULL, 0, 1000, TimerProc);
Your TimerProc:
Code:
VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
time_t timeNow = time();
int seconds_count = (int) difftime(timeNow, g_timeStart);
char buffer[50];
int hours = seconds_count / 3600; /* 3600 seconds in an hour. */
int minutes = (seconds_count % 3600) / 60; /* Get remainder after removing hours
* and divide by 60 to get minutes. */
int seconds = (seconds_count % 60); /* Get remainder after removing minutes. */
sprintf(buffer, "%d:%02d:%02d", hours, minutes, seconds);
SetWindowText(hwndStatus, buffer);
}
-
Hey, where should i declare the first part. I put it in my MainWndproc. Anyways i got 5 errors,and here they are.
D:\Documents and Settings\Desktop\STP\STP\stpwin1.c(9126) : error C2065: 'TimerProc' : undeclared identifier
D:\Documents and Settings\Desktop\STP\STP\stpwin1.c(9123) : error C2198: 'time' : too few actual parameters
D:\Documents and Settings\Desktop\STP\STP\stpwin1.c(9123) : error C2065: 'g_timeStart' : undeclared identifier
D:\Documents and Settings\Desktop\STP\STP\stpwin1.c(10621) : error C2373: 'TimerProc' : redefinition; different type modifiers
D:\Documents and Settings\Desktop\STP\STP\stpwin1.c(10622) : error C2198: 'time' : too few actual parameters
-
I think you could have worked through these errors:
- error C2065: 'TimerProc' : undeclared identifier - Use a prototype.
- error C2198: 'time' : too few actual parameters - time() takes one argument so the code should be:
Code:
g_timeStart = time(NULL);
- error C2065: 'g_timeStart' : undeclared identifier - The g_ indicates a global variable. It is up to you to declare it somewhere:
Code:
time_t g_timeStart;
-
this is the error i got
C:\Documents and Settings\Desktop\STP\STP\stpwin1.c(647) : error C2099: initializer is not a constant
when i did this
g_timeStart = time(NULL);
-
also this caused me an error:
time_t timeNow = time();
C:\Documents and Settings\Desktop\STP\STP\stpwin1.c(10619) : error C2198: 'time' : too few actual parameters
-
>> error C2198: 'time' : too few actual parameters <<
See number 2 above!
>> error C2099: initializer is not a constant <<
In C, you can't call a function outside a function.
-
for time_t timeNow = time(NULL) i got this error
C:\Documents and Settings\Desktop\STP\STP\stpwin1.c(10640) : error C2275: 'time_t' : illegal use of this type as an expression
-
ok nevermind, i got it to compile but it doesnt show up on my window