AleksandarH

PS - Second Test #2

May 17th, 2022 (edited)
1,070
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Ex2.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "framework.h"
  5. #include "Ex2.h"
  6. #include <CommCtrl.h>
  7. #include <stdio.h>
  8.  
  9. #define MAX_LOADSTRING 100
  10.  
  11. // Global Variables:
  12. HINSTANCE hInst;                                // current instance
  13. WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
  14. WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
  15.  
  16. // Forward declarations of functions included in this code module:
  17. ATOM                MyRegisterClass(HINSTANCE hInstance);
  18. BOOL                InitInstance(HINSTANCE, int);
  19. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  20. INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  21. INT_PTR CALLBACK    Dialog(HWND, UINT, WPARAM, LPARAM);
  22. INT_PTR CALLBACK    Timer(HWND, UINT, WPARAM, LPARAM);
  23.  
  24. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  25.     _In_opt_ HINSTANCE hPrevInstance,
  26.     _In_ LPWSTR    lpCmdLine,
  27.     _In_ int       nCmdShow)
  28. {
  29.     UNREFERENCED_PARAMETER(hPrevInstance);
  30.     UNREFERENCED_PARAMETER(lpCmdLine);
  31.  
  32.     // TODO: Place code here.
  33.  
  34.     // Initialize global strings
  35.     LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  36.     LoadStringW(hInstance, IDC_EX2, szWindowClass, MAX_LOADSTRING);
  37.     MyRegisterClass(hInstance);
  38.  
  39.     // Perform application initialization:
  40.     if (!InitInstance(hInstance, nCmdShow))
  41.     {
  42.         return FALSE;
  43.     }
  44.  
  45.     HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_EX2));
  46.  
  47.     MSG msg;
  48.  
  49.     // Main message loop:
  50.     while (GetMessage(&msg, nullptr, 0, 0))
  51.     {
  52.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  53.         {
  54.             TranslateMessage(&msg);
  55.             DispatchMessage(&msg);
  56.         }
  57.     }
  58.  
  59.     return (int)msg.wParam;
  60. }
  61.  
  62.  
  63.  
  64. //
  65. //  FUNCTION: MyRegisterClass()
  66. //
  67. //  PURPOSE: Registers the window class.
  68. //
  69. ATOM MyRegisterClass(HINSTANCE hInstance)
  70. {
  71.     WNDCLASSEXW wcex;
  72.  
  73.     wcex.cbSize = sizeof(WNDCLASSEX);
  74.  
  75.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  76.     wcex.lpfnWndProc = WndProc;
  77.     wcex.cbClsExtra = 0;
  78.     wcex.cbWndExtra = 0;
  79.     wcex.hInstance = hInstance;
  80.     wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_EX2));
  81.     wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  82.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  83.     wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_EX2);
  84.     wcex.lpszClassName = szWindowClass;
  85.     wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  86.  
  87.     return RegisterClassExW(&wcex);
  88. }
  89.  
  90. //
  91. //   FUNCTION: InitInstance(HINSTANCE, int)
  92. //
  93. //   PURPOSE: Saves instance handle and creates main window
  94. //
  95. //   COMMENTS:
  96. //
  97. //        In this function, we save the instance handle in a global variable and
  98. //        create and display the main program window.
  99. //
  100. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  101. {
  102.     hInst = hInstance; // Store instance handle in our global variable
  103.  
  104.     HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  105.         CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
  106.  
  107.     if (!hWnd)
  108.     {
  109.         return FALSE;
  110.     }
  111.  
  112.     ShowWindow(hWnd, nCmdShow);
  113.     UpdateWindow(hWnd);
  114.  
  115.     return TRUE;
  116. }
  117.  
  118. //
  119. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  120. //
  121. //  PURPOSE: Processes messages for the main window.
  122. //
  123. //  WM_COMMAND  - process the application menu
  124. //  WM_PAINT    - Paint the main window
  125. //  WM_DESTROY  - post a quit message and return
  126. //
  127. //
  128. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  129. {
  130.     switch (message)
  131.     {
  132.     case WM_COMMAND:
  133.     {
  134.         int wmId = LOWORD(wParam);
  135.         // Parse the menu selections:
  136.         switch (wmId)
  137.         {
  138.         case IDM_ABOUT:
  139.             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  140.             break;
  141.         case IDM_EXIT:
  142.             DestroyWindow(hWnd);
  143.             break;
  144.         case ID_DIALOG_DIALOG:
  145.             DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, Dialog);
  146.             break;
  147.         case ID_DIALOG_TIMER:
  148.             DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG2), hWnd, Timer);
  149.             break;
  150.         default:
  151.             return DefWindowProc(hWnd, message, wParam, lParam);
  152.         }
  153.     }
  154.     break;
  155.     case WM_PAINT:
  156.     {
  157.         PAINTSTRUCT ps;
  158.         HDC hdc = BeginPaint(hWnd, &ps);
  159.         POINT p[5];
  160.         p[0].x = 500; p[0].y = 500;
  161.         p[1].x = 375; p[1].y = 375;
  162.         p[2].x = 450; p[2].y = 300;
  163.         p[3].x = 550; p[3].y = 300;
  164.         p[4].x = 625; p[4].y = 375;
  165.         Polygon(hdc, p, 5);
  166.         p[0].x = 450; p[0].y = 300;
  167.         p[1].x = 500; p[1].y = 500;
  168.         p[2].x = 550; p[2].y = 300;
  169.         Polygon(hdc, p, 3);
  170.         MoveToEx(hdc, 375, 375, 0);
  171.         LineTo(hdc, 625, 375);
  172.         EndPaint(hWnd, &ps);
  173.     }
  174.     break;
  175.     case WM_DESTROY:
  176.         PostQuitMessage(0);
  177.         break;
  178.     default:
  179.         return DefWindowProc(hWnd, message, wParam, lParam);
  180.     }
  181.     return 0;
  182. }
  183.  
  184. // Message handler for about box.
  185. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  186. {
  187.     UNREFERENCED_PARAMETER(lParam);
  188.     switch (message)
  189.     {
  190.     case WM_INITDIALOG:
  191.         return (INT_PTR)TRUE;
  192.  
  193.     case WM_COMMAND:
  194.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  195.         {
  196.             EndDialog(hDlg, LOWORD(wParam));
  197.             return (INT_PTR)TRUE;
  198.         }
  199.         break;
  200.     }
  201.     return (INT_PTR)FALSE;
  202. }
  203.  
  204. INT_PTR CALLBACK Dialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  205. {
  206.     char data[100], result[100], * stopstring = nullptr;
  207.     double a = 0, b = 0, c = 0, math = 0;
  208.     int cbIndex = 0, lbIndex = 0;
  209.     BOOL* LPt = NULL;
  210.     BOOL SIG = TRUE;
  211.     UNREFERENCED_PARAMETER(lParam);
  212.     switch (message)
  213.     {
  214.     case WM_INITDIALOG:
  215.         SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)"2.3");
  216.         SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)"10.4");
  217.         return (INT_PTR)TRUE;
  218.  
  219.     case WM_COMMAND:
  220.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  221.         {
  222.             EndDialog(hDlg, LOWORD(wParam));
  223.             return (INT_PTR)TRUE;
  224.         }
  225.         else if (LOWORD(wParam) == IDC_BUTTON1)
  226.         {
  227.             GetDlgItemText(hDlg, IDC_EDIT1, data, sizeof(data));
  228.             if (!(SendDlgItemMessage(hDlg, IDC_COMBO1, CB_FINDSTRING, 0, (LPARAM)data) > -1))
  229.             {
  230.                 cbIndex = SendDlgItemMessage(hDlg, IDC_COMBO1, CB_ADDSTRING, 0, (LPARAM)data);
  231.                 SendDlgItemMessage(hDlg, IDC_COMBO1, CB_SETCURSEL, cbIndex, 0);
  232.             }
  233.         }
  234.         else if (LOWORD(wParam) == IDC_BUTTON2)
  235.         {
  236.             if (IsDlgButtonChecked(hDlg, IDC_RADIO1))
  237.             {
  238.                 a = GetDlgItemInt(hDlg, IDC_EDIT2, LPt, SIG);
  239.                 b = GetDlgItemInt(hDlg, IDC_EDIT3, LPt, SIG);
  240.                 lbIndex = SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETCURSEL, 0, 0);
  241.                 if (lbIndex > -1)
  242.                 {
  243.                     SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETTEXT, lbIndex, (LPARAM)data);
  244.                 }
  245.                 c = strtod(data, &stopstring);
  246.                 math = (a + b) / c;
  247.                 sprintf_s(result, "%6.3f", math);
  248.                 SetDlgItemText(hDlg, IDC_EDIT4, result);
  249.             }
  250.             else if (IsDlgButtonChecked(hDlg, IDC_RADIO2))
  251.             {
  252.                 a = GetDlgItemInt(hDlg, IDC_EDIT2, LPt, SIG);
  253.                 b = GetDlgItemInt(hDlg, IDC_EDIT3, LPt, SIG);
  254.                 lbIndex = SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETCURSEL, 0, 0);
  255.                 if (lbIndex > -1)
  256.                 {
  257.                     SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETTEXT, lbIndex, (LPARAM)data);
  258.                 }
  259.                 c = strtod(data, &stopstring);
  260.                 math = (a - b) * c;
  261.                 sprintf_s(result, "%6.3f", math);
  262.                 SetDlgItemText(hDlg, IDC_EDIT4, result);
  263.             }
  264.         }
  265.         break;
  266.     }
  267.     return (INT_PTR)FALSE;
  268. }
  269.  
  270. INT_PTR CALLBACK Timer(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  271. {
  272.     static int step = 5;
  273.     UNREFERENCED_PARAMETER(lParam);
  274.     switch (message)
  275.     {
  276.     case WM_INITDIALOG:
  277.         SendDlgItemMessage(hDlg, IDC_PROGRESS1, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
  278.         SendDlgItemMessage(hDlg, IDC_PROGRESS2, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
  279.         SendDlgItemMessage(hDlg, IDC_PROGRESS1, PBM_SETSTEP, step, 0);
  280.         SendDlgItemMessage(hDlg, IDC_PROGRESS2, PBM_SETSTEP, step, 0);
  281.         return (INT_PTR)TRUE;
  282.  
  283.     case WM_COMMAND:
  284.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  285.         {
  286.             EndDialog(hDlg, LOWORD(wParam));
  287.             return (INT_PTR)TRUE;
  288.         }
  289.         if (LOWORD(wParam) == IDC_BUTTON1)
  290.         {
  291.             SetTimer(hDlg, TIMER1, 20, NULL);
  292.         }
  293.         break;
  294.  
  295.     case WM_TIMER:
  296.     {
  297.         if (SendDlgItemMessage(hDlg, IDC_PROGRESS1, PBM_GETPOS, 0, 0) < 100)
  298.         {
  299.             SendDlgItemMessage(hDlg, IDC_PROGRESS1, PBM_STEPIT, 0, 0);
  300.         }
  301.         else if (SendDlgItemMessage(hDlg, IDC_PROGRESS1, PBM_GETPOS, 0, 0) == 100)
  302.         {
  303.             if (SendDlgItemMessage(hDlg, IDC_PROGRESS2, PBM_GETPOS, 0, 0) < 100)
  304.             {
  305.                 SendDlgItemMessage(hDlg, IDC_PROGRESS2, PBM_STEPIT, 0, 0);
  306.                 if (SendDlgItemMessage(hDlg, IDC_PROGRESS2, PBM_GETPOS, 0, 0) == 100)
  307.                 {
  308.                     KillTimer(hDlg, TIMER1);
  309.                 }
  310.             }
  311.         }
  312.         break;
  313.     }
  314.     }
  315.     return (INT_PTR)FALSE;
  316. }
Add Comment
Please, Sign In to add comment