Pushok

Visual Stop-Watch (selfmade 100%)

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