Advertisement
Cieslin

PIU_KonwerterTemperatury

Dec 19th, 2017
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  5.  
  6. const COLORREF backgroundColor = RGB(255, 255, 255);
  7. HINSTANCE hInstance;
  8. enum {CELSJUSZ, KELWIN, FAHRENHEIT};
  9.  
  10. bool RejestracjaOkna(WNDCLASSEX &window)
  11. {
  12.     window.cbSize = sizeof(WNDCLASSEX);
  13.     window.style = CS_VREDRAW | CS_HREDRAW;
  14.     window.lpfnWndProc = ProceduraOkna;
  15.     window.cbClsExtra = 0;
  16.     window.cbWndExtra = 0;
  17.     window.hInstance = hInstance;
  18.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  19.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  20.     window.hbrBackground = (HBRUSH)CreateSolidBrush(backgroundColor);
  21.     window.lpszMenuName = NULL;
  22.     window.lpszClassName = "StandardoweOkno";
  23.     window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  24.  
  25.     if (!RegisterClassEx(&window))
  26.     {
  27.         MessageBox(NULL, "Problem z rejestracją okna", "Rejestracja okna", MB_ICONERROR | MB_OK);
  28.         return false;
  29.     }
  30.     return true;
  31. }
  32.  
  33.  
  34. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCMD)
  35. {
  36.     WNDCLASSEX window;
  37.     if (!RejestracjaOkna(window))
  38.         return 0;
  39.  
  40.     HWND uchwytOkna = 0;
  41.  
  42.     uchwytOkna = CreateWindowEx(
  43.         0,
  44.         "StandardoweOkno",
  45.         "Pierwsze okno",
  46.         WS_OVERLAPPEDWINDOW,
  47.         100,
  48.         100,
  49.         260,
  50.         200,
  51.         0,
  52.         0,
  53.         hInstance,
  54.         0);
  55.  
  56.     ShowWindow(uchwytOkna, SW_NORMAL);
  57.     UpdateWindow(uchwytOkna);
  58.  
  59.     MSG msg;
  60.  
  61.     for (; ;)
  62.     {
  63.         if (0 != GetMessage(&msg, 0, 0, 0))
  64.         {
  65.             TranslateMessage(&msg);
  66.             DispatchMessage(&msg);
  67.         }
  68.         if (msg.message == WM_QUIT) break;
  69.     }
  70.  
  71.     return (int)msg.wParam;
  72. }
  73.  
  74. LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  75. {
  76.     static HWND edit_C, edit_K, edit_F, static_C, static_K, static_F;
  77.     switch (message)
  78.     {
  79.     case WM_CREATE:
  80.     {
  81.         HINSTANCE &hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
  82.         edit_C = CreateWindowEx(NULL, TEXT("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 1, 2, 3, 4, hWnd, (HMENU)0, hInstance, NULL);
  83.         edit_K = CreateWindowEx(NULL, TEXT("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 5, 6, 7, 8, hWnd, (HMENU)1, hInstance, NULL);
  84.         edit_F = CreateWindowEx(NULL, TEXT("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 9, 10, 11, 12, hWnd, (HMENU)2, hInstance, NULL);
  85.         static_C = CreateWindowEx(NULL, TEXT("STATIC"), TEXT("Celsjusz"), WS_CHILD | WS_VISIBLE | WS_BORDER, 13, 14, 15, 16, hWnd, (HMENU)3, hInstance, NULL);
  86.         static_K = CreateWindowEx(NULL, TEXT("STATIC"), TEXT("Kelwin"), WS_CHILD | WS_VISIBLE | WS_BORDER, 17, 18, 19, 20, hWnd, (HMENU)4, hInstance, NULL);
  87.         static_F = CreateWindowEx(NULL, TEXT("STATIC"), TEXT("Farenheit"), WS_CHILD | WS_VISIBLE | WS_BORDER, 21, 22, 23, 24, hWnd, (HMENU)5, hInstance, NULL);
  88.     }
  89.         break;
  90.     case WM_SIZE:
  91.     {
  92.         RECT clientRect;
  93.         GetClientRect(hWnd, &clientRect);
  94.         int xEdit = (clientRect.right - 50) / 2;
  95.         int y = 10;
  96.         int xStatic = (clientRect.right - 190) / 2;
  97.         int widthEdit = 100;
  98.         int height = 20;
  99.         int widthStatic = 70;
  100.  
  101.         /////////////////////////////////////////////////////////////////////////
  102.         MoveWindow(static_C, xStatic, y, widthStatic, height, TRUE);
  103.         MoveWindow(edit_C, xEdit, y, widthEdit, height, TRUE);
  104.         /////////////////////////////////////////////////////////////////////////
  105.         MoveWindow(static_K, xStatic, y += height + 10, widthStatic, height, TRUE);
  106.         MoveWindow(edit_K, xEdit, y, widthEdit, height, TRUE);
  107.         /////////////////////////////////////////////////////////////////////////
  108.         MoveWindow(static_F, xStatic, y += height + 10, widthStatic, height, TRUE);
  109.         MoveWindow(edit_F, xEdit, y, widthEdit, height, TRUE);
  110.         /////////////////////////////////////////////////////////////////////////
  111.     }
  112.         break;
  113.     case WM_COMMAND:
  114.     {
  115.         static char buff[11];
  116.         static double temp_C = 0;
  117.         static double temp_K = 0;
  118.         static double temp_F = 0;
  119.         switch (LOWORD(wParam))
  120.         {
  121.         case CELSJUSZ:
  122.         {
  123.             if (GetFocus() != (HWND)lParam)
  124.                 break;
  125.             GetWindowTextA(edit_C, buff, 10);
  126.             temp_C = atof(buff);
  127.             temp_K = temp_C + 273.15;
  128.             temp_F = 1.8 * temp_C + 32;
  129.             sprintf_s(buff, "%.2lf", temp_K);
  130.             SetWindowTextA(edit_K, buff);
  131.             sprintf_s(buff, "%.2lf", temp_F);
  132.             SetWindowTextA(edit_F, buff);
  133.         }
  134.         break;
  135.         case KELWIN:
  136.         {
  137.             if (GetFocus() != (HWND)lParam)
  138.                 break;
  139.             GetWindowTextA(edit_K, buff, 10);
  140.             temp_K = atof(buff);
  141.             temp_C = temp_K - 273.15;
  142.             temp_F = 1.8 * temp_C + 32;
  143.             sprintf_s(buff, "%.2lf", temp_C);
  144.             SetWindowTextA(edit_C, buff);
  145.             sprintf_s(buff, "%.2lf", temp_F);
  146.             SetWindowTextA(edit_F, buff);
  147.         }
  148.         break;
  149.         case FAHRENHEIT:
  150.         {
  151.             if (GetFocus() != (HWND)lParam)
  152.                 break;
  153.             GetWindowTextA(edit_F, buff, 10);
  154.             temp_F = atof(buff);
  155.             temp_C = (5.0/9.0) * (temp_F -32);
  156.             temp_K = temp_C + 273.15;
  157.             sprintf_s(buff, "%.2lf", temp_C);
  158.             SetWindowTextA(edit_C, buff);
  159.             sprintf_s(buff, "%.2lf", temp_K);
  160.             SetWindowTextA(edit_K, buff);
  161.         }
  162.         break;
  163.         }
  164.     }
  165.     break;
  166.     case WM_GETMINMAXINFO:
  167.         ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 260;
  168.         ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 200;
  169.         break;
  170.     case WM_CLOSE:
  171.         if (MessageBox(hWnd, "Czy chcesz zakonczyc dzialanie programu?", "Dzialanie programu", MB_YESNO | MB_ICONERROR) == IDNO)
  172.             break;
  173.     case WM_DESTROY:
  174.         PostQuitMessage(0);
  175.         break;
  176.     default:
  177.         return DefWindowProc(hWnd, message, wParam, lParam);
  178.     }
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement