AleksandarH

PS - First Test #1

May 22nd, 2022 (edited)
1,245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Ex4.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "framework.h"
  5. #include "Ex4.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    Calculator(HWND, UINT, WPARAM, LPARAM);
  22.  
  23. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  24.     _In_opt_ HINSTANCE hPrevInstance,
  25.     _In_ LPWSTR    lpCmdLine,
  26.     _In_ int       nCmdShow)
  27. {
  28.     UNREFERENCED_PARAMETER(hPrevInstance);
  29.     UNREFERENCED_PARAMETER(lpCmdLine);
  30.  
  31.     // TODO: Place code here.
  32.  
  33.     // Initialize global strings
  34.     LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  35.     LoadStringW(hInstance, IDC_EX4, szWindowClass, MAX_LOADSTRING);
  36.     MyRegisterClass(hInstance);
  37.  
  38.     // Perform application initialization:
  39.     if (!InitInstance(hInstance, nCmdShow))
  40.     {
  41.         return FALSE;
  42.     }
  43.  
  44.     HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_EX4));
  45.  
  46.     MSG msg;
  47.  
  48.     // Main message loop:
  49.     while (GetMessage(&msg, nullptr, 0, 0))
  50.     {
  51.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  52.         {
  53.             TranslateMessage(&msg);
  54.             DispatchMessage(&msg);
  55.         }
  56.     }
  57.  
  58.     return (int)msg.wParam;
  59. }
  60.  
  61. //
  62. //  FUNCTION: MyRegisterClass()
  63. //
  64. //  PURPOSE: Registers the window class.
  65. //
  66. ATOM MyRegisterClass(HINSTANCE hInstance)
  67. {
  68.     WNDCLASSEXW wcex;
  69.  
  70.     wcex.cbSize = sizeof(WNDCLASSEX);
  71.  
  72.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  73.     wcex.lpfnWndProc = WndProc;
  74.     wcex.cbClsExtra = 0;
  75.     wcex.cbWndExtra = 0;
  76.     wcex.hInstance = hInstance;
  77.     wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_EX4));
  78.     wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  79.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  80.     wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_EX4);
  81.     wcex.lpszClassName = szWindowClass;
  82.     wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  83.  
  84.     return RegisterClassExW(&wcex);
  85. }
  86.  
  87. //
  88. //   FUNCTION: InitInstance(HINSTANCE, int)
  89. //
  90. //   PURPOSE: Saves instance handle and creates main window
  91. //
  92. //   COMMENTS:
  93. //
  94. //        In this function, we save the instance handle in a global variable and
  95. //        create and display the main program window.
  96. //
  97. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  98. {
  99.     hInst = hInstance; // Store instance handle in our global variable
  100.  
  101.     HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  102.         CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
  103.  
  104.     if (!hWnd)
  105.     {
  106.         return FALSE;
  107.     }
  108.  
  109.     ShowWindow(hWnd, nCmdShow);
  110.     UpdateWindow(hWnd);
  111.  
  112.     return TRUE;
  113. }
  114.  
  115. //
  116. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  117. //
  118. //  PURPOSE: Processes messages for the main window.
  119. //
  120. //  WM_COMMAND  - process the application menu
  121. //  WM_PAINT    - Paint the main window
  122. //  WM_DESTROY  - post a quit message and return
  123. //
  124. //
  125. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  126. {
  127.     switch (message)
  128.     {
  129.     case WM_COMMAND:
  130.     {
  131.         int wmId = LOWORD(wParam);
  132.         // Parse the menu selections:
  133.         switch (wmId)
  134.         {
  135.         case IDM_ABOUT:
  136.             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  137.             break;
  138.         case IDM_EXIT:
  139.             DestroyWindow(hWnd);
  140.             break;
  141.         case ID_MENU_MESSAGEBOX:
  142.             MessageBox(hWnd, "Message", "Message Box", MB_OK | MB_ICONINFORMATION);
  143.             break;
  144.         case ID_MENU_ENABLEMENU:
  145.         {
  146.             HMENU hMenu = GetMenu(hWnd);
  147.             UINT res = GetMenuState(hMenu, ID_MENU_DISABLED, MF_BYCOMMAND);
  148.             if (res & MF_DISABLED)
  149.             {
  150.                 EnableMenuItem(hMenu, ID_MENU_DISABLED, MF_BYCOMMAND | MF_ENABLED);
  151.             }
  152.             else
  153.             {
  154.                 EnableMenuItem(hMenu, ID_MENU_DISABLED, MF_BYCOMMAND | MF_DISABLED);
  155.             }
  156.             break;
  157.         }
  158.         case ID_MENU_DELETE:
  159.         {
  160.             HMENU hMenu = GetMenu(hWnd);
  161.             DeleteMenu(hMenu, ID_MENU_DELETED, MF_BYCOMMAND);
  162.             break;
  163.         }
  164.         case ID_MENU_INSERT:
  165.         {
  166.             HMENU hMenu = GetMenu(hWnd);
  167.             if (GetMenuState(hMenu, ID_MENU_MESSAGEBOX + 1, MF_BYCOMMAND) == -1)
  168.             {
  169.                 MENUITEMINFO mii;
  170.                 ZeroMemory(&mii, sizeof(mii));
  171.                 mii.cbSize = sizeof(mii);
  172.                 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
  173.                 mii.wID = ID_MENU_MESSAGEBOX + 1;
  174.                 mii.fType = MFT_STRING;
  175.                 mii.dwTypeData = const_cast<char*>("Inserted");
  176.                 mii.fState = MFS_ENABLED;
  177.                 InsertMenuItem(hMenu, ID_MENU_ENABLEMENU, FALSE, &mii);
  178.             }
  179.             break;
  180.         }
  181.         case ID_MENU_MESSAGEBOX + 1:
  182.             DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, Calculator);
  183.             break;
  184.         default:
  185.             return DefWindowProc(hWnd, message, wParam, lParam);
  186.         }
  187.     }
  188.     break;
  189.     case WM_PAINT:
  190.     {
  191.         PAINTSTRUCT ps;
  192.         HDC hdc = BeginPaint(hWnd, &ps);
  193.         // TODO: Add any drawing code that uses hdc here...
  194.         EndPaint(hWnd, &ps);
  195.     }
  196.     break;
  197.     case WM_DESTROY:
  198.         PostQuitMessage(0);
  199.         break;
  200.     default:
  201.         return DefWindowProc(hWnd, message, wParam, lParam);
  202.     }
  203.     return 0;
  204. }
  205.  
  206. // Message handler for about box.
  207. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  208. {
  209.     UNREFERENCED_PARAMETER(lParam);
  210.     switch (message)
  211.     {
  212.     case WM_INITDIALOG:
  213.         return (INT_PTR)TRUE;
  214.  
  215.     case WM_COMMAND:
  216.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  217.         {
  218.             EndDialog(hDlg, LOWORD(wParam));
  219.             return (INT_PTR)TRUE;
  220.         }
  221.         break;
  222.     }
  223.     return (INT_PTR)FALSE;
  224. }
  225.  
  226. INT_PTR CALLBACK Calculator(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  227. {
  228.     char result[100];
  229.     double a = 0, b = 0, c = 0, math = 0;
  230.     BOOL* LPt = NULL;
  231.     BOOL SIG = TRUE;
  232.     UNREFERENCED_PARAMETER(lParam);
  233.     switch (message)
  234.     {
  235.     case WM_INITDIALOG:
  236.         CheckDlgButton(hDlg, IDC_RADIO1, BST_CHECKED);
  237.         return (INT_PTR)TRUE;
  238.  
  239.     case WM_COMMAND:
  240.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  241.         {
  242.             EndDialog(hDlg, LOWORD(wParam));
  243.             return (INT_PTR)TRUE;
  244.         }
  245.         else if (LOWORD(wParam) == IDC_BUTTON1)
  246.         {
  247.             if (IsDlgButtonChecked(hDlg, IDC_RADIO1))
  248.             {
  249.                 a = GetDlgItemInt(hDlg, IDC_EDIT1, LPt, SIG);
  250.                 b = GetDlgItemInt(hDlg, IDC_EDIT2, LPt, SIG);
  251.                 c = GetDlgItemInt(hDlg, IDC_EDIT3, LPt, SIG);
  252.                 if (c == 0)
  253.                 {
  254.                     MessageBox(hDlg, "ERROR: Cannot divise by 0", "Invalid Input", MB_OK | MB_ICONWARNING);
  255.                     break;
  256.                 }
  257.                 else
  258.                 {
  259.                     math = (a + b) / c;
  260.                     sprintf_s(result, "%6.3f", math);
  261.                     SetDlgItemText(hDlg, IDC_EDIT4, result);
  262.                 }
  263.             }
  264.             else if (IsDlgButtonChecked(hDlg, IDC_RADIO2))
  265.             {
  266.                 a = GetDlgItemInt(hDlg, IDC_EDIT1, LPt, SIG);
  267.                 b = GetDlgItemInt(hDlg, IDC_EDIT2, LPt, SIG);
  268.                 c = GetDlgItemInt(hDlg, IDC_EDIT3, LPt, SIG);
  269.                 math = (a - b) * c;
  270.                 sprintf_s(result, "%6.3f", math);
  271.                 SetDlgItemText(hDlg, IDC_EDIT4, result);
  272.             }
  273.         }
  274.         break;
  275.     }
  276.     return (INT_PTR)FALSE;
  277. }
Add Comment
Please, Sign In to add comment