The best way to create a timer on the screen

I had the idea of ​​creating a countdown timer, for example, 01:02, on the screen (fullsize). One thing is that I really don't know how to start.

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

Does anyone have pointers on how to start this? My program would be like making a computer a great stopwatch (but with added features)

Not asking for code, just some ideas / primers on how to start this. Doing mostly web content made me a little rusty in programming wins.

Any ideas are welcome

thanks

Note. I think I need to do this with c / C ++ because of speed. My stopwatch program will run on a very slow PC, something like p3 800mhz, so speed is really important.

+3
source share
3 answers

If you have experience with Windows message processing and the Win32 API, you should get 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 nice reference to using timers:

Using timers

+3
source

Create a timer, ask the application to respond to the timer event by sending a message with paint to itself. Be sure to delete the timer when the application exits.

0
source

; 800 800 . 799 , , . Jvascript . , C ++ .

The easiest way to program Win32 is through a supporting library. wxWidgets and Qt are good choices and both are free. They saved you a little on nuts and bolts. Basically, in both you will create a Window object containing a text field object and a timer object, and you will simply bind the timer to the text update.

0
source

Source: https://habr.com/ru/post/1702318/


All Articles