Advertisement
AleksandarH

PS - Second Test #1

May 17th, 2022 (edited)
1,999
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Ex1.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "framework.h"
  5. #include "Ex1.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_EX1, 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_EX1));
  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_EX1));
  81.     wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  82.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  83.     wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_EX1);
  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[4];
  160.         p[0].x = 200; p[0].y = 200;
  161.         p[1].x = 500; p[1].y = 200;
  162.         p[2].x = 550; p[2].y = 150;
  163.         p[3].x = 250; p[3].y = 150;
  164.         Polygon(hdc, p, 4);
  165.         p[0].x = 500; p[0].y = 400;
  166.         p[1].x = 550; p[1].y = 350;
  167.         p[2].x = 550; p[2].y = 150;
  168.         p[3].x = 500; p[3].y = 200;
  169.         Polygon(hdc, p, 4);
  170.         Rectangle(hdc, 200, 200, 500, 400);
  171.         EndPaint(hWnd, &ps);
  172.     }
  173.     break;
  174.     case WM_DESTROY:
  175.         PostQuitMessage(0);
  176.         break;
  177.     default:
  178.         return DefWindowProc(hWnd, message, wParam, lParam);
  179.     }
  180.     return 0;
  181. }
  182.  
  183. // Message handler for about box.
  184. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  185. {
  186.     UNREFERENCED_PARAMETER(lParam);
  187.     switch (message)
  188.     {
  189.     case WM_INITDIALOG:
  190.         return (INT_PTR)TRUE;
  191.  
  192.     case WM_COMMAND:
  193.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  194.         {
  195.             EndDialog(hDlg, LOWORD(wParam));
  196.             return (INT_PTR)TRUE;
  197.         }
  198.         break;
  199.     }
  200.     return (INT_PTR)FALSE;
  201. }
  202.  
  203. INT_PTR CALLBACK Dialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  204. {
  205.     char data[100], result[100], * stopstring = nullptr;
  206.     double a = 0, b = 0, c = 0, math = 0;
  207.     int cb = 0, lb = 0;
  208.     BOOL* LPt = NULL;
  209.     BOOL SIG = TRUE;
  210.     UNREFERENCED_PARAMETER(lParam);
  211.     switch (message)
  212.     {
  213.     case WM_INITDIALOG:
  214.         SendDlgItemMessage(hDlg, IDC_COMBO1, CB_ADDSTRING, 0, (LPARAM)"-3");
  215.         SendDlgItemMessage(hDlg, IDC_COMBO1, CB_ADDSTRING, 0, (LPARAM)"3.3");
  216.         SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)"0.1");
  217.         SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)"10.1");
  218.         return (INT_PTR)TRUE;
  219.  
  220.     case WM_COMMAND:
  221.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  222.         {
  223.             EndDialog(hDlg, LOWORD(wParam));
  224.             return (INT_PTR)TRUE;
  225.         }
  226.         else if (LOWORD(wParam) == IDC_BUTTON1)
  227.         {
  228.             GetDlgItemText(hDlg, IDC_EDIT1, data, sizeof(data));
  229.             if (!(SendDlgItemMessage(hDlg, IDC_COMBO1, CB_FINDSTRING, 0, (LPARAM)data) > -1))
  230.             {
  231.                 SendDlgItemMessage(hDlg, IDC_COMBO1, CB_ADDSTRING, 0, (LPARAM)data);
  232.             }
  233.             GetDlgItemText(hDlg, IDC_EDIT2, data, sizeof(data));
  234.             if (!(SendDlgItemMessage(hDlg, IDC_LIST1, LB_FINDSTRING, 0, (LPARAM)data) > -1))
  235.             {
  236.                 SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)data);
  237.             }
  238.         }
  239.         else if (LOWORD(wParam) == IDC_BUTTON2)
  240.         {
  241.             if (IsDlgButtonChecked(hDlg, IDC_RADIO1))
  242.             {
  243.                 if (IsDlgButtonChecked(hDlg, IDC_CHECK1))
  244.                 {
  245.                     a = GetDlgItemInt(hDlg, IDC_EDIT3, LPt, SIG);
  246.                 }
  247.                 cb = SendDlgItemMessage(hDlg, IDC_COMBO1, CB_GETCURSEL, 0, 0);
  248.                 if (cb > -1)
  249.                 {
  250.                     SendDlgItemMessage(hDlg, IDC_COMBO1, CB_GETLBTEXT, cb, (LPARAM)data);
  251.                 }
  252.                 b = strtod(data, &stopstring);
  253.                 lb = SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETCURSEL, 0, 0);
  254.                 if (lb > -1)
  255.                 {
  256.                     SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETTEXT, lb, (LPARAM)data);
  257.                 }
  258.                 c = strtod(data, &stopstring);
  259.                 math = (a * c) - (b * a);
  260.                 sprintf_s(result, "%6.3f", math);
  261.                 SetDlgItemText(hDlg, IDC_EDIT4, result);
  262.             }
  263.             else if (IsDlgButtonChecked(hDlg, IDC_RADIO2))
  264.             {
  265.                 if (IsDlgButtonChecked(hDlg, IDC_CHECK1))
  266.                 {
  267.                     a = GetDlgItemInt(hDlg, IDC_EDIT3, LPt, SIG);
  268.                 }
  269.                 cb = SendDlgItemMessage(hDlg, IDC_COMBO1, CB_GETCURSEL, 0, 0);
  270.                 if (cb > -1)
  271.                 {
  272.                     SendDlgItemMessage(hDlg, IDC_COMBO1, CB_GETLBTEXT, cb, (LPARAM)data);
  273.                 }
  274.                 b = strtod(data, &stopstring);
  275.                 lb = SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETCURSEL, 0, 0);
  276.                 if (lb > -1)
  277.                 {
  278.                     SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETTEXT, lb, (LPARAM)data);
  279.                 }
  280.                 c = strtod(data, &stopstring);
  281.                 math = a - (b * c);
  282.                 sprintf_s(result, "%6.3f", math);
  283.                 SetDlgItemText(hDlg, IDC_EDIT4, result);
  284.             }
  285.         }
  286.         break;
  287.     }
  288.     return (INT_PTR)FALSE;
  289. }
  290.  
  291. INT_PTR CALLBACK Timer(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  292. {
  293.     static int step = 5;
  294.     UNREFERENCED_PARAMETER(lParam);
  295.     switch (message)
  296.     {
  297.     case WM_INITDIALOG:
  298.         SendDlgItemMessage(hDlg, IDC_PROGRESS1, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
  299.         SendDlgItemMessage(hDlg, IDC_PROGRESS2, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
  300.         SendDlgItemMessage(hDlg, IDC_PROGRESS1, PBM_SETSTEP, step, 0);
  301.         SendDlgItemMessage(hDlg, IDC_PROGRESS2, PBM_SETSTEP, step, 0);
  302.         return (INT_PTR)TRUE;
  303.  
  304.     case WM_COMMAND:
  305.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  306.         {
  307.             EndDialog(hDlg, LOWORD(wParam));
  308.             return (INT_PTR)TRUE;
  309.         }
  310.         if (LOWORD(wParam) == IDC_BUTTON1)
  311.         {
  312.             SetTimer(hDlg, TIMER1, 20, NULL);
  313.         }
  314.         break;
  315.  
  316.     case WM_TIMER:
  317.         if (SendDlgItemMessage(hDlg, IDC_PROGRESS1, PBM_GETPOS, 0, 0) < 100)
  318.         {
  319.             SendDlgItemMessage(hDlg, IDC_PROGRESS1, PBM_STEPIT, 0, 0);
  320.         }
  321.         else if (SendDlgItemMessage(hDlg, IDC_PROGRESS1, PBM_GETPOS, 0, 0) == 100)
  322.         {
  323.             if (SendDlgItemMessage(hDlg, IDC_PROGRESS2, PBM_GETPOS, 0, 0) < 100)
  324.             {
  325.                 SendDlgItemMessage(hDlg, IDC_PROGRESS2, PBM_STEPIT, 0, 0);
  326.                 if (SendDlgItemMessage(hDlg, IDC_PROGRESS2, PBM_GETPOS, 0, 0) == 100)
  327.                 {
  328.                     KillTimer(hDlg, TIMER1);
  329.                 }
  330.             }
  331.         }
  332.         break;
  333.     }
  334.     return (INT_PTR)FALSE;
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement