Advertisement
seba101

(WINAPI) Stoper

Dec 19th, 2017
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <math.h>
  3. #include <list>
  4. #include <stdio.h>
  5.  
  6. using namespace std;
  7.  
  8. LPCSTR NazwaKlasy = TEXT("Klasa Okienka");
  9. MSG Komunikat;
  10. HDC hdc;
  11. HWND hwnd;
  12. RECT Rect;
  13. PAINTSTRUCT ps;
  14. HWND hText,hButtonStart,hButtonStop,hButtonReset;
  15. int m, s, ms;
  16. bool start = NULL;
  17. char buf[50];
  18. SYSTEMTIME czasAktualny, czasStartu;
  19. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  20.  
  21. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  22. {
  23.     WNDCLASSEX wc;
  24.     wc.cbSize = sizeof(WNDCLASSEX);
  25.     wc.style = CS_HREDRAW | CS_VREDRAW;
  26.     wc.lpfnWndProc = WndProc;
  27.     wc.cbClsExtra = 0;
  28.     wc.cbWndExtra = 0;
  29.     wc.hInstance = hInstance;
  30.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  31.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  32.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  33.     wc.lpszMenuName = NULL;
  34.     wc.lpszClassName = NazwaKlasy;
  35.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  36.  
  37.     if (!RegisterClassEx(&wc))
  38.     {
  39.         MessageBox(NULL, TEXT("Nie mozna utworzyc okna"), TEXT("Niestety..."),
  40.             MB_ICONEXCLAMATION | MB_OK);
  41.         return 1;
  42.     }
  43.  
  44.     hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, NazwaKlasy, TEXT("Oto okienko"), WS_OVERLAPPEDWINDOW,
  45.         CW_USEDEFAULT, CW_USEDEFAULT, 800, 400, NULL, NULL, hInstance, NULL);
  46.  
  47.      hText = CreateWindowEx(0, "STATIC", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 50, 50, 0, 0,
  48.         hwnd, NULL, hInstance, NULL);
  49.  
  50.      hButtonStart = CreateWindowEx(0, "BUTTON", "Start", WS_CHILD | WS_VISIBLE,
  51.          0, 0, 150, 30, hwnd, NULL, hInstance, NULL);
  52.  
  53.     hButtonStop = CreateWindowEx(0, "BUTTON", "Stop", WS_CHILD | WS_VISIBLE,
  54.          0, 0, 150, 30, hwnd, NULL, hInstance, NULL);
  55.  
  56.     hButtonReset = CreateWindowEx(0, "BUTTON", "Reset", WS_CHILD | WS_VISIBLE,
  57.          0, 0, 150, 30, hwnd, NULL, hInstance, NULL);
  58.  
  59.     if (hwnd == NULL)
  60.     {
  61.         MessageBox(NULL, TEXT("Okno odmówiło przyjścia na świat!"), TEXT("Ale kicha..."), MB_ICONEXCLAMATION);
  62.         return 1;
  63.     }
  64.  
  65.     ShowWindow(hwnd, nCmdShow);
  66.     UpdateWindow(hwnd);
  67.  
  68.     while (GetMessage(&Komunikat, NULL, 0, 0))
  69.     {
  70.         TranslateMessage(&Komunikat);
  71.         DispatchMessage(&Komunikat);
  72.     }
  73.     return Komunikat.wParam;
  74. }
  75. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  76. {
  77.    
  78.  
  79.     switch (msg)
  80.     {
  81.     case WM_COMMAND:
  82.         if ((HWND)lParam == hButtonStart) { start = TRUE; GetLocalTime(&czasStartu); }
  83.         if ((HWND)lParam == hButtonStop) start = NULL;
  84.         if ((HWND)lParam == hButtonReset)
  85.         {
  86.             ms = 0, m = 0, s = 0;
  87.             wsprintf(buf, "%02d:%02d:%02d", m, s, ms);
  88.             SetWindowText(hText, buf);
  89.         }
  90.         break;
  91.     case WM_CREATE:
  92.  
  93.         SetTimer(hwnd, 1, 10, 0);
  94.         break;
  95.     case WM_TIMER:
  96.         if (start == TRUE)
  97.         {
  98.             GetLocalTime(&czasAktualny);
  99.             int r;
  100.             r = (czasAktualny.wMinute - czasStartu.wMinute) * 60000 + (czasAktualny.wSecond - czasStartu.wSecond) * 1000 + (czasAktualny.wMilliseconds - czasStartu.wMilliseconds);
  101.             m = (r / 60000);
  102.             s = (r % 60000) / 1000;
  103.             ms = ((r % 60000) % 1000) / 100;
  104.             wsprintf(buf, "%02d:%02d:%02d", m,s,ms);
  105.             SetWindowText(hText, buf);
  106.             InvalidateRect(hwnd, 0, 1);
  107.         }
  108.         break;
  109.     case WM_CLOSE:
  110.         DestroyWindow(hwnd);
  111.         break;
  112.     case WM_DESTROY:
  113.         KillTimer(hwnd, 1);
  114.         PostQuitMessage(0);
  115.         break;
  116.  
  117.     case WM_SIZE:
  118.  
  119.         GetClientRect(hwnd, &Rect);
  120.         MoveWindow(hText, Rect.right / 2 - 75, Rect.bottom / 2 - 50, 150, 50, TRUE);
  121.         MoveWindow(hButtonStart, Rect.right / 2 - 75, Rect.bottom / 2, 50, 25, TRUE);
  122.         MoveWindow(hButtonStop, Rect.right / 2 - 25, Rect.bottom / 2 , 50, 25, TRUE);
  123.         MoveWindow(hButtonReset, Rect.right / 2 + 25, Rect.bottom / 2 , 50, 25, TRUE);
  124.  
  125.         break;
  126.     default:
  127.         return DefWindowProc(hwnd, msg, wParam, lParam);
  128.     }
  129.  
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement