tags:

views:

248

answers:

3

Hey!

I had this idea of creating a count down timer, like 01:02, on the screen (fullsize). One thing is that I really don't have a clue on how to start.

I do know basic c/c++, win32 api and a bit of gdi.

Anyone have any pointers on how to start this? My program would be like making the computer into a big stopwatch (but with added features)

Not asking for code, just some ideas/primers on how to start this. Doing mostly web stuff has made me a bit rusty in win programming.

Any ideas are welcomed

Thanks

Note: I think I have to do this with c/c++ because of speed. My stopwatch program would run in a very slow pc, something like a p3 800mhz, so speed is really important.

A: 

Create a timer, have your application respond to the timer event by sending a paint message to itself. Be sure to remove the timer when app exits.

Joe Soul-bringer
+2  A: 

If you have some experience with windows message processing and the Win32 API, this should get you started.

LRESULT WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
  HDC hdc; 
  PAINTSTRUCT ps; 
  RECT r; 
  char szBuffer[200]; 
  static int count = 120; 
  int seconds = 0;
  int minutes = 0;
  int hours  = 0;

  switch (message) { 
  case WM_CREATE: 
    // create a 1 second timer 
    SetTimer (hwnd, ID_TIMER, 1000, NULL); 
    return 0;      

  case WM_PAINT:
    if(count > 0)
    {
        hdc = BeginPaint (hwnd, &ps); 
        GetClientRect (hwnd, &r);
        hours = count / 3600;
        minutes = (count / 60) % 60;
        seconds = count % 60;
        wsprintf (szBuffer, "Hours: %d Minutes: %d Seconds: %d", hours, minutes, seconds); 
        DrawText (hdc, szBuffer, -1, &r, DT_LEFT); 
        EndPaint (hwnd, &ps); 
    }
    else
    {
        SendMessage (hwnd, WM_CLOSE, 0, 0L)
    }
    return 0; 

  case WM_TIMER: 
    count--;       
    InvalidateRect (hwnd, NULL, TRUE);
    return 0;        

  case WM_DESTROY: 
    KillTimer (hwnd, ID_TIMER); 
    PostQuitMessage (0); 
    return 0; 
  }  /* end switch */ 
 }

Here's a good link on using timers:

Using Timers

Phaedrus
Wow! Thanks!! I will be sure to analyze this code and I really do hope this helps me out :)Thanks again!
AntonioCS
Don't count on time ... er ... counting with WM_TIMER, since it will be inaccurate. Better use system time in milliseconds and update it in WM_TIMER's one second intervals with the current system time value.
macbirdie
A: 

A clock runs at one screenchange per second; an 800MHz CPU handles about 800 million instructions per second. That's about 799 million more than you need, so efficiency is NOT an issue. It would still work in Jvascript inside a browser. Doesn't mean that C or C++ are unsuitable, though.

The easiest way to program Win32 is through a supporting library. wxWidgets and Qt are good choices, and both free. They save you quite a bit on the nuts & bolts side. Basically, in both you'd create a Window object containing a textbox object and a timer object, and you'd just wire the timer tick to a text update.

MSalters