peterzig

[PIU] Cale na Centymetry

Nov 23rd, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.81 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define SCREEN_WIDTH 700
  3. #define SCREEN_HEIGHT 600
  4. #define TIMER_ID 1
  5. #define BUFF_SIZE 64
  6.  
  7. #include <windows.h>
  8. #include <tchar.h>
  9.  
  10. TCHAR className[] = TEXT("Class");
  11. TCHAR appName[] = TEXT("Przelicznik jednostek");
  12.  
  13. HWND hwnd, valueTextField, resultTextField, inchRadioButton, centyRadioButton;
  14. MSG msg;
  15.  
  16. RECT getLocalCoordinates() {
  17.     RECT Rect;
  18.     GetWindowRect(hwnd, &Rect);
  19.     MapWindowPoints(HWND_DESKTOP, GetParent(hwnd), (LPPOINT)&Rect, 2);
  20.     return Rect;
  21. }
  22.  
  23. void SetMoveWindow(const RECT clientRect) {
  24.     MoveWindow(valueTextField, clientRect.right / 3, clientRect.bottom / 2.5, clientRect.right / 7, clientRect.bottom / 20, 1);
  25.     MoveWindow(resultTextField, clientRect.right / 2, clientRect.bottom / 2.5, clientRect.right / 7, clientRect.bottom / 20, 1);
  26.     MoveWindow(inchRadioButton, clientRect.right / 3, clientRect.bottom / 2, clientRect.right / 7, clientRect.bottom / 20, 1);
  27.     MoveWindow(centyRadioButton, clientRect.right / 2, clientRect.bottom / 2, clientRect.right / 7, clientRect.bottom / 20, 1);
  28.  
  29.     if (clientRect.right <= 500) {
  30.         RECT rect = getLocalCoordinates();
  31.  
  32.         MoveWindow(hwnd, rect.left, rect.top, 501, rect.bottom - rect.top, 1);
  33.     }
  34.  
  35.     if (clientRect.bottom <= 500) {
  36.         RECT rect = getLocalCoordinates();
  37.  
  38.         MoveWindow(hwnd, rect.left, rect.top, rect.right - rect.left, 501, 1);
  39.     }
  40. }
  41.  
  42. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  43.     static RECT clientRect;
  44.     static bool operation = false;
  45.  
  46.     switch (msg) {
  47.     case WM_CREATE: {
  48.         SetTimer(hwnd, 100, TIMER_ID, NULL);
  49.         GetClientRect(hwnd, &clientRect);
  50.         SetMoveWindow(clientRect);
  51.     }break;
  52.  
  53.     case WM_SIZE: {
  54.         GetClientRect(hwnd, &clientRect);
  55.         SetMoveWindow(clientRect);
  56.     }break;
  57.  
  58.     case WM_COMMAND:
  59.         if ((HWND)lParam == inchRadioButton)
  60.             operation = true;
  61.         else if ((HWND)lParam == centyRadioButton)
  62.             operation = false;
  63.         break;
  64.  
  65.     case WM_TIMER: {
  66.         TCHAR buf[BUFF_SIZE];
  67.         GetWindowText(valueTextField, buf, BUFF_SIZE);
  68.  
  69.         if (wcslen(buf) < 1)
  70.             _swprintf(buf, TEXT(""));
  71.  
  72.         if (_wtof(buf))
  73.             if (operation)
  74.                 _swprintf(buf, TEXT("%f"), _wtof(buf) * 2.54f);
  75.             else
  76.                 _swprintf(buf, TEXT("%f"), _wtof(buf) / 2.54f);
  77.  
  78.         SetWindowText(resultTextField, buf);
  79.     }break;
  80.  
  81.     case WM_CLOSE:
  82.         DestroyWindow(hwnd);
  83.         break;
  84.  
  85.     case WM_DESTROY: {
  86.         KillTimer(hwnd, TIMER_ID);
  87.         PostQuitMessage(0);
  88.     }break;
  89.  
  90.     default:
  91.         return DefWindowProc(hwnd, msg, wParam, lParam);
  92.     }
  93.  
  94.     return 0;
  95. }
  96.  
  97. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  98. {
  99.     WNDCLASSEX wc;
  100.     wc.cbSize = sizeof(WNDCLASSEX);
  101.     wc.style = 0;
  102.     wc.cbClsExtra = 0;
  103.     wc.cbWndExtra = 0;
  104.     wc.hInstance = hInstance;
  105.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  106.     wc.lpfnWndProc = WndProc;
  107.     wc.lpszClassName = className;
  108.     wc.lpszMenuName = NULL;
  109.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  110.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  111.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  112.  
  113.     if (RegisterClassEx(&wc) == 0) {
  114.         MessageBox(NULL, TEXT("Some problem with RegisterClassEx"), className, MB_OK);
  115.         return 1;
  116.     }
  117.  
  118.     hwnd = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_CLIENTEDGE, className, appName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, hInstance, 0);
  119.     if (hwnd == NULL) {
  120.         MessageBox(NULL, TEXT("Some problem with CreateWindowEx by hwnd"), className, MB_OK);
  121.         return 1;
  122.     }
  123.  
  124.     valueTextField = CreateWindowEx(0, TEXT("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 0, 0, 0, 0, hwnd, NULL, hInstance, 0);
  125.     if (valueTextField == NULL) {
  126.         MessageBox(NULL, TEXT("Some problem with CreateWindowEx by valueTextField"), className, MB_OK);
  127.         return 1;
  128.     }
  129.  
  130.     resultTextField = CreateWindowEx(0, TEXT("STATIC"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 0, 0, 0, 0, hwnd, NULL, hInstance, 0);
  131.     if (resultTextField == NULL) {
  132.         MessageBox(NULL, TEXT("Some problem with CreateWindowEx by resultTextField"), className, MB_OK);
  133.         return 1;
  134.     }
  135.  
  136.     inchRadioButton = CreateWindowEx(0, TEXT("BUTTON"), TEXT("cal -> cm"), WS_CHILD | WS_VISIBLE | WS_BORDER | BS_AUTORADIOBUTTON, 0, 0, 0, 0, hwnd, NULL, hInstance, 0);
  137.     if (inchRadioButton == NULL) {
  138.         MessageBox(NULL, TEXT("Some problem with CreateWindowEx by inchRadioButton"), className, MB_OK);
  139.         return 1;
  140.     }
  141.  
  142.     centyRadioButton = CreateWindowEx(0, TEXT("BUTTON"), TEXT("cm -> cal"), WS_CHILD | WS_VISIBLE | WS_BORDER | BST_CHECKED | BS_AUTORADIOBUTTON, 0, 0, 0, 0, hwnd, NULL, hInstance, 0);
  143.     if (centyRadioButton == NULL) {
  144.         MessageBox(NULL, TEXT("Some problem with CreateWindowEx by centyRadioButton"), className, MB_OK);
  145.         return 1;
  146.     }
  147.  
  148.     ShowWindow(hwnd, nCmdShow);
  149.     UpdateWindow(hwnd);
  150.  
  151.     while (GetMessage(&msg, NULL, 0, 0) > 0)
  152.     {
  153.         TranslateMessage(&msg);
  154.         DispatchMessage(&msg);
  155.     }
  156.  
  157.     UnregisterClass(className, hInstance);
  158.  
  159.     return msg.wParam;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment