Pushok

Stop-Watch

Apr 22nd, 2011
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include <windows.h>
  2. #include <windowsx.h>
  3. #include <tchar.h>
  4. #include <stdlib.h>
  5.  
  6. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  7.  
  8. VOID CALLBACK TimerEvent( HWND window, UINT Msg, UINT_PTR EventID, DWORD SYSTime);
  9.  
  10. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  11.     LPSTR lpCmdLine, int nCmdShow )
  12. {
  13.     MSG  msg ;    
  14.     WNDCLASS wc;
  15.     ZeroMemory(&wc, sizeof(wc));
  16.  
  17.     wc.lpszClassName = TEXT( "Timer" );
  18.     wc.hInstance     = hInstance ;
  19.     wc.hbrBackground = CreateSolidBrush(RGB(0,0,0));
  20.     wc.lpfnWndProc   = WndProc ;
  21.     wc.hCursor       = LoadCursor(0, IDC_ARROW);
  22.  
  23.  
  24.     RegisterClass(&wc);
  25.     CreateWindow( wc.lpszClassName, TEXT("Stop Watch"),
  26.         WS_SYSMENU | WS_VISIBLE,
  27.         150, 150, 230, 230, 0, 0, hInstance, 0);  
  28.  
  29.     while( GetMessage(&msg, NULL, 0, 0)) {
  30.         TranslateMessage(&msg);
  31.         DispatchMessage(&msg);
  32.     }
  33.     return (int) msg.wParam;
  34. }
  35.  
  36. int GlobTime = 0;
  37.  
  38. LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
  39. {
  40.     static HWND MenuItems[3] = {0};
  41.  
  42.     switch(msg)  
  43.     {
  44.     case WM_CREATE:
  45.         {
  46.             MenuItems[0] = CreateWindow(TEXT("button"), TEXT("Старт"),    
  47.                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  48.                 120, 150, 80, 25,        
  49.                 hwnd, (HMENU) 1, NULL, NULL);  
  50.  
  51.             MenuItems[1] = CreateWindow(TEXT("button"), TEXT("Стоп"),    
  52.                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  53.                 20, 150, 80, 25,        
  54.                 hwnd, (HMENU) 2, NULL, NULL);
  55.  
  56.             MenuItems[2] = CreateWindow(TEXT("button"), NULL,    
  57.                 WS_VISIBLE | WS_CHILD | BS_GROUPBOX,
  58.                 20, 15, 180, 120,        
  59.                 hwnd, (HMENU) 3, NULL, NULL);
  60.  
  61.             break;
  62.         }
  63.  
  64.     case WM_COMMAND:
  65.         {
  66.             if(HIWORD(wParam) == BN_CLICKED)
  67.             {
  68.                 if (LOWORD(wParam) == 1)
  69.                 {
  70.                     GlobTime = 0;
  71.                     KillTimer(hwnd, 1);
  72.                     SetTimer(hwnd, 1, 1000, TimerEvent);
  73.                 }
  74.                 else if(LOWORD(wParam) == 2)
  75.                 {
  76.                     GlobTime = 0;
  77.                     InvalidateRect(hwnd, 0, false);
  78.                     KillTimer(hwnd, 1);
  79.                 }
  80.             }
  81.             break;
  82.         }
  83.     case WM_PAINT:
  84.         {
  85.             PAINTSTRUCT ps;
  86.  
  87.             HDC hd = BeginPaint((HWND)MenuItems[2], &ps);
  88.  
  89.             HFONT hfontMSSS = CreateFont (80,40,FW_DONTCARE,FW_DONTCARE,FW_BOLD,
  90.                 FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
  91.                 CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_DONTCARE,L"System");
  92.  
  93.             SelectFont(hd, hfontMSSS);
  94.             SetTextColor(hd, RGB(255,0,0));
  95.             SetBkColor(hd, RGB(0,0,0));
  96.  
  97.             TCHAR timeNow[6] = {0}, csec[3] = {0}, cmin[3] = {0};
  98.             int sec = GlobTime, mins = GlobTime;
  99.  
  100.             do
  101.             {
  102.                 if(sec - 60 < 0 && sec != 0)
  103.                 {
  104.                     wsprintfW(csec, sec > 9 ? L"%d" : L"0%d", sec);
  105.                     break;
  106.                 }
  107.                 else if(!sec)
  108.                 {
  109.                     wcscat(csec, L"00");
  110.                     break;
  111.                 }
  112.                
  113.                 sec -= 60;
  114.             } while (1);
  115.  
  116.             //wcscat(timeNow, L":");
  117.  
  118.             if(GlobTime - sec == 0)
  119.             {
  120.                 wcscat(cmin, L"00");
  121.             }
  122.             else
  123.             {
  124.                 wsprintfW(cmin, (GlobTime - sec) / 60 > 9 ? L"%d" : L"0%d", (GlobTime - sec) / 60);
  125.             }
  126.  
  127.             wsprintf(timeNow, L"%s:%s", cmin, csec);
  128.  
  129.  
  130.             TextOut(hd, 0, 10, timeNow, _tcslen(timeNow));
  131.             EndPaint(hwnd, &ps);
  132.             break;
  133.         }
  134.     case WM_DESTROY:
  135.         {
  136.             KillTimer(hwnd, 1);
  137.             PostQuitMessage(0);
  138.             break;
  139.         }
  140.     }
  141.     return DefWindowProc(hwnd, msg, wParam, lParam);
  142. }
  143.  
  144. VOID CALLBACK TimerEvent( HWND window, UINT Msg, UINT_PTR EventID, DWORD SYSTime)
  145. {
  146.     if(++GlobTime > 60 * 60 - 1) GlobTime = 0;
  147.     InvalidateRect(window, 0, false);
  148. }
Add Comment
Please, Sign In to add comment