Advertisement
Alan468

Kalkulator WORKING + FICZER

Nov 29th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <stdio.h>
  3. #include <cstdio>
  4. #include <tchar.h>
  5. #include <time.h>
  6.  
  7. enum Operation { None, Add, Sub, Mul, Div }Op;
  8.  
  9. HWND EraseAll, EraseLast;
  10. HWND Numbers[10], AddBtn, SubBtn, Equal, OutputField;
  11. HWND hwnd;
  12. double Result;
  13. char buff[255];
  14.  
  15. bool A = false;
  16. void a() {
  17.    
  18.  
  19.     int PositionX = 50, PositionY = 100;
  20.  
  21.     for (int i = 1; i < 10; i++) {
  22.  
  23.         MoveWindow(Numbers[i], PositionX+(rand()%200)+1, PositionY + (rand() % 200) + 1, 50 + (rand() % 200) + 1, 50 + (rand() % 200) + 1, 1);
  24.  
  25.         if (PositionX == 150) {
  26.             PositionX = 50;
  27.             PositionY += 50;
  28.         }
  29.         else {
  30.             PositionX += 50;
  31.         }
  32.  
  33.  
  34.     }
  35. }
  36.  
  37. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  38.     switch (msg) {
  39.     case WM_TIMER:
  40.         if (A)a();
  41.         break;
  42.     case WM_KEYDOWN:
  43.         switch ((int)wParam) {
  44.         case VK_RETURN:
  45.         {
  46.             if (!A) {
  47.                 SetTimer(hwnd, 100, 20, NULL);
  48.                 A = true;
  49.             }
  50.             else {
  51.                 KillTimer(hwnd, 100);
  52.                 A = false;
  53.             }
  54.         }
  55.         break;
  56.         }
  57.         break;
  58.     case WM_COMMAND: {
  59.         if ((HWND)lParam == EraseAll) {
  60.             SetWindowText(OutputField, "");
  61.         }
  62.         if ((HWND)lParam == EraseLast) {
  63.             GetWindowText(OutputField, buff, 255);
  64.             int i = strlen(buff);
  65.             if (i > 0)
  66.                 buff[i - 1] = '\0';
  67.             SetWindowText(OutputField, buff);
  68.  
  69.         }
  70.  
  71.         if ((HWND)lParam == Equal) {
  72.             GetWindowText(OutputField, buff, 255);
  73.             int Num = atof(buff);
  74.  
  75.             switch (Op) {
  76.             case Add:
  77.                 Result += Num;
  78.                 break;
  79.             case Sub:
  80.                 Result -= Num;
  81.                 break;
  82.             case Mul:
  83.                 Result *= Num;
  84.                 break;
  85.             case Div:
  86.                 Result /= Num;
  87.                 break;
  88.             }
  89.  
  90.             sprintf_s(buff, "%.0f", Result);
  91.             SetWindowText(OutputField, buff);
  92.             Result = 0.0;
  93.             Op = None;
  94.         }
  95.  
  96.         if ((HWND)lParam == AddBtn) {
  97.             GetWindowText(OutputField, buff, 255);
  98.             Result = atof(buff);
  99.             SetWindowText(OutputField, "");
  100.             Op = Add;
  101.         }
  102.         if ((HWND)lParam == SubBtn) {
  103.             GetWindowText(OutputField, buff, 255);
  104.             Result = atof(buff);
  105.             SetWindowText(OutputField, "");
  106.             Op = Sub;
  107.         }
  108.  
  109.         for (int i = 0; i < 10; i++) {
  110.             if ((HWND)lParam == Numbers[i]) {
  111.                 GetWindowText(OutputField, buff, 255);
  112.                 if (strlen(buff) <= 16) {
  113.                     double Num = (atof(buff) * 10) + i;
  114.                     sprintf_s(buff, "%.0f", Num);
  115.                     SetWindowText(OutputField, buff);
  116.                 }
  117.             }
  118.         }
  119.     }break;
  120.  
  121.     case WM_CLOSE:DestroyWindow(hwnd); break;
  122.     case WM_DESTROY:PostQuitMessage(0); break;
  123.     default:return DefWindowProc(hwnd, msg, wParam, lParam);
  124.     }
  125.     return 0;
  126. }
  127.  
  128. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
  129.     srand(time(NULL));
  130.     WNDCLASSEX window;
  131.     MSG msg;
  132.     TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Tytul");
  133.  
  134.     window.cbClsExtra = NULL;
  135.     window.cbSize = sizeof(WNDCLASSEX);
  136.     window.cbWndExtra = NULL;
  137.     window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  138.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  139.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  140.     window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  141.     window.hInstance = hInstance;
  142.     window.lpfnWndProc = WndProc;
  143.     window.lpszClassName = Class_Name;
  144.     window.lpszMenuName = 0;
  145.     window.style = CS_VREDRAW | CS_HREDRAW;
  146.  
  147.     RegisterClassEx(&window);
  148.  
  149.     hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 300, 400, NULL, NULL, hInstance, NULL);
  150.  
  151.     int PositionX = 50, PositionY = 100;
  152.  
  153.     for (int i = 1; i < 10; i++) {
  154.         char buff[255];
  155.         sprintf_s(buff, "%d", i);
  156.         Numbers[i] = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", buff, WS_CHILD | WS_VISIBLE, PositionX, PositionY, 50, 50, hwnd, NULL, hInstance, NULL);
  157.  
  158.         if (PositionX == 150) {
  159.             PositionX = 50;
  160.             PositionY += 50;
  161.         }
  162.         else {
  163.             PositionX += 50;
  164.         }
  165.  
  166.     }
  167.  
  168.     Numbers[0] = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "0", WS_CHILD | WS_VISIBLE, PositionX, PositionY, 100, 50, hwnd, NULL, hInstance, NULL);
  169.  
  170.     AddBtn = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "+", WS_CHILD | WS_VISIBLE, 200, 100, 50, 50, hwnd, NULL, hInstance, NULL);
  171.  
  172.     SubBtn = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "-", WS_CHILD | WS_VISIBLE, 200, 150, 50, 50, hwnd, NULL, hInstance, NULL);
  173.  
  174.     Equal = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "=", WS_CHILD | WS_VISIBLE, 200, 200, 50, 100, hwnd, NULL, hInstance, NULL);
  175.  
  176.     OutputField = CreateWindowEx(WS_EX_WINDOWEDGE, "STATIC", NULL, WS_CHILD | WS_VISIBLE, 50, 20, 200, 50, hwnd, NULL, hInstance, NULL);
  177.  
  178.     EraseAll = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "C", WS_CHILD | WS_VISIBLE, 200, 70, 50, 30, hwnd, NULL, hInstance, NULL);
  179.  
  180.     EraseLast = CreateWindowEx(WS_EX_WINDOWEDGE, "BUTTON", "<-", WS_CHILD | WS_VISIBLE, 150, 70, 50, 30, hwnd, NULL, hInstance, NULL);
  181.  
  182.     ShowWindow(hwnd, nCmdShow);
  183.     UpdateWindow(hwnd);
  184.  
  185.     while (GetMessage(&msg, NULL, 0, 0)) {
  186.         TranslateMessage(&msg);
  187.         DispatchMessage(&msg);
  188.     }
  189.  
  190.     UnregisterClass(Class_Name, hInstance);
  191.     return msg.wParam;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement