Advertisement
AleksandarH

PS - First Test #2

May 24th, 2022
2,715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // WindowsProject3.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "framework.h"
  5. #include "WindowsProject3.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_WINDOWSPROJECT3, 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_WINDOWSPROJECT3));
  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_WINDOWSPROJECT3));
  81.     wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
  82.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  83.     wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_WINDOWSPROJECT3);
  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 ID_DIALOGANDTIMER_DIALOG:
  142.                 DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, Dialog);
  143.                 break;
  144.             case ID_DIALOGANDTIMER_TIMER:
  145.                 DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG2), hWnd, Timer);
  146.                 break;
  147.             case IDM_EXIT:
  148.                 DestroyWindow(hWnd);
  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.             // TODO: Add any drawing code that uses hdc here...
  160.             EndPaint(hWnd, &ps);
  161.         }
  162.         break;
  163.     case WM_DESTROY:
  164.         PostQuitMessage(0);
  165.         break;
  166.     default:
  167.         return DefWindowProc(hWnd, message, wParam, lParam);
  168.     }
  169.     return 0;
  170. }
  171.  
  172. // Message handler for about box.
  173. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  174. {
  175.     UNREFERENCED_PARAMETER(lParam);
  176.     switch (message)
  177.     {
  178.     case WM_INITDIALOG:
  179.         return (INT_PTR)TRUE;
  180.  
  181.     case WM_COMMAND:
  182.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  183.         {
  184.             EndDialog(hDlg, LOWORD(wParam));
  185.             return (INT_PTR)TRUE;
  186.         }
  187.         break;
  188.     }
  189.     return (INT_PTR)FALSE;
  190. }
  191.  
  192. INT_PTR CALLBACK Dialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  193. {
  194.     char data[100], result[100], * stopstring = nullptr;
  195.     float a = 0, b = 0, c = 0, math = 0;
  196.     int cbIndex = 0, lbIndex = 0;
  197.     BOOL* LPt = NULL;
  198.     BOOL SIG = FALSE;
  199.     UNREFERENCED_PARAMETER(lParam);
  200.     switch (message)
  201.     {
  202.     case WM_INITDIALOG:
  203.         SendDlgItemMessage(hDlg, IDC_COMBO1, CB_ADDSTRING, 0, (LPARAM)"-3");
  204.         SendDlgItemMessage(hDlg, IDC_COMBO1, CB_ADDSTRING, 0, (LPARAM)"3.3");
  205.         return (INT_PTR)TRUE;
  206.  
  207.     case WM_COMMAND:
  208.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  209.         {
  210.             EndDialog(hDlg, LOWORD(wParam));
  211.             return (INT_PTR)TRUE;
  212.         }
  213.         else if (LOWORD(wParam) == IDC_BUTTON1)
  214.         {
  215.             GetDlgItemText(hDlg, IDC_EDIT1, data, sizeof(data));
  216.             if (!(SendDlgItemMessage(hDlg, IDC_LIST1, LB_FINDSTRING, 0, (LPARAM)data) > -1))
  217.             {
  218.                 cbIndex = SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, 0, (LPARAM)data);
  219.                 SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETCURSEL, cbIndex, 0);
  220.             }
  221.         }
  222.         else if (LOWORD(wParam) == IDC_BUTTON2)
  223.         {
  224.             if (IsDlgButtonChecked(hDlg, IDC_RADIO1))
  225.             {
  226.  
  227.                 a = GetDlgItemInt(hDlg, IDC_EDIT2, LPt, SIG);
  228.                 b = GetDlgItemInt(hDlg, IDC_EDIT3, LPt, SIG);
  229.                 lbIndex = SendDlgItemMessage(hDlg, IDC_COMBO1, LB_GETCURSEL, 0, 0);
  230.                 if (lbIndex > -1)
  231.                 {
  232.                     SendDlgItemMessage(hDlg, IDC_COMBO1, CB_GETLBTEXT, lbIndex, (LPARAM)data);
  233.                 }
  234.                 c = strtod(data, &stopstring);
  235.                 if (((a)+(b)) == 0)
  236.                 {
  237.                     MessageBox(hDlg, "Cant divide by 0", "ERROR", MB_OK);
  238.                     break;
  239.                 }
  240.                 math = c/(a + b);
  241.                 sprintf_s(result, "%6.3f", math);
  242.                 SetDlgItemText(hDlg, IDC_EDIT4, result);
  243.             }
  244.             else if (IsDlgButtonChecked(hDlg, IDC_RADIO2))
  245.             {
  246.  
  247.                 a = GetDlgItemInt(hDlg, IDC_EDIT2, LPt, SIG);
  248.                 b = GetDlgItemInt(hDlg, IDC_EDIT3, LPt, SIG);
  249.                 lbIndex = SendDlgItemMessage(hDlg, IDC_COMBO1, CB_GETCURSEL, 0, 0);
  250.                 if (lbIndex > -1)
  251.                 {
  252.                     SendDlgItemMessage(hDlg, IDC_COMBO1, CB_GETLBTEXT, lbIndex, (LPARAM)data);
  253.                 }
  254.                 c = strtod(data, &stopstring);
  255.                 if (c  == 0)
  256.                 {
  257.                     MessageBox(hDlg, "Cant divide by 0", "ERROR", MB_OK);
  258.                     break;
  259.                 }
  260.                 math = (c + 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.         return (INT_PTR)TRUE;
  278.  
  279.     case WM_COMMAND:
  280.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  281.         {
  282.             EndDialog(hDlg, LOWORD(wParam));
  283.             return (INT_PTR)TRUE;
  284.         }
  285.         if (LOWORD(wParam) == IDC_BUTTON1)
  286.         {
  287.             SetTimer(hDlg, TIMER1, 20, NULL);
  288.         }
  289.         break;
  290.     }
  291.  
  292.  
  293.     return (INT_PTR)FALSE;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement