Advertisement
seba101

(WINAPI) KM -> M

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