Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.34 KB | None | 0 0
  1. // WIN_Project_2.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "WIN_Project_2.h"
  6. #include <stdio.h>
  7. #include <string>
  8. #define MAX_LOADSTRING 100
  9.  
  10. using namespace std;
  11.  
  12. // Global Variables:
  13. HINSTANCE hInst; // current instance
  14. WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  15. WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
  16.  
  17. // Forward declarations of functions included in this code module:
  18. ATOM MyRegisterClass(HINSTANCE hInstance);
  19. BOOL InitInstance(HINSTANCE, int);
  20. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  21. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  22.  
  23. INT_PTR CALLBACK Swap(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); //Swap Dialog Box
  24. INT_PTR CALLBACK Calc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); //Calc Dialog Box
  25. INT_PTR CALLBACK MathTest(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); //Math Test Dialog Box
  26.  
  27. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  28. _In_opt_ HINSTANCE hPrevInstance,
  29. _In_ LPWSTR lpCmdLine,
  30. _In_ int nCmdShow)
  31. {
  32. UNREFERENCED_PARAMETER(hPrevInstance);
  33. UNREFERENCED_PARAMETER(lpCmdLine);
  34.  
  35. // TODO: Place code here.
  36.  
  37. // Initialize global strings
  38. LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  39. LoadStringW(hInstance, IDC_WINPROJECT2, szWindowClass, MAX_LOADSTRING);
  40. MyRegisterClass(hInstance);
  41.  
  42. // Perform application initialization:
  43. if (!InitInstance (hInstance, nCmdShow))
  44. {
  45. return FALSE;
  46. }
  47.  
  48. HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINPROJECT2));
  49.  
  50. MSG msg;
  51.  
  52. // Main message loop:
  53. while (GetMessage(&msg, nullptr, 0, 0))
  54. {
  55. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  56. {
  57. TranslateMessage(&msg);
  58. DispatchMessage(&msg);
  59. }
  60. }
  61.  
  62. return (int) msg.wParam;
  63. }
  64.  
  65.  
  66.  
  67. //
  68. // FUNCTION: MyRegisterClass()
  69. //
  70. // PURPOSE: Registers the window class.
  71. //
  72. ATOM MyRegisterClass(HINSTANCE hInstance)
  73. {
  74. WNDCLASSEXW wcex;
  75.  
  76. wcex.cbSize = sizeof(WNDCLASSEX);
  77.  
  78. wcex.style = CS_HREDRAW | CS_VREDRAW;
  79. wcex.lpfnWndProc = WndProc;
  80. wcex.cbClsExtra = 0;
  81. wcex.cbWndExtra = 0;
  82. wcex.hInstance = hInstance;
  83. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINPROJECT2));
  84. wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  85. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  86. wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINPROJECT2);
  87. wcex.lpszClassName = szWindowClass;
  88. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  89.  
  90. return RegisterClassExW(&wcex);
  91. }
  92.  
  93. //
  94. // FUNCTION: InitInstance(HINSTANCE, int)
  95. //
  96. // PURPOSE: Saves instance handle and creates main window
  97. //
  98. // COMMENTS:
  99. //
  100. // In this function, we save the instance handle in a global variable and
  101. // create and display the main program window.
  102. //
  103. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  104. {
  105. hInst = hInstance; // Store instance handle in our global variable
  106.  
  107. HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  108. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
  109.  
  110. if (!hWnd)
  111. {
  112. return FALSE;
  113. }
  114.  
  115. ShowWindow(hWnd, nCmdShow);
  116. UpdateWindow(hWnd);
  117.  
  118. return TRUE;
  119. }
  120.  
  121. //
  122. // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  123. //
  124. // PURPOSE: Processes messages for the main window.
  125. //
  126. // WM_COMMAND - process the application menu
  127. // WM_PAINT - Paint the main window
  128. // WM_DESTROY - post a quit message and return
  129. //
  130. //
  131. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  132. {
  133. switch (message)
  134. {
  135. case WM_COMMAND:
  136. {
  137. int wmId = LOWORD(wParam);
  138. // Parse the menu selections:
  139. switch (wmId)
  140. {
  141. case ID_CHECK:
  142. {
  143. HMENU hMenu = GetMenu(hWnd);
  144. UINT res = GetMenuState(hMenu, ID_CHECK, MF_BYCOMMAND);
  145. if (res & MF_CHECKED)
  146. CheckMenuItem(hMenu, ID_CHECK, MF_BYCOMMAND | MF_UNCHECKED);
  147. else
  148. CheckMenuItem(hMenu, ID_CHECK, MF_BYCOMMAND | MF_CHECKED);
  149. } break;
  150. case ID_ENABLE:
  151. {
  152. HMENU hMenu = GetMenu(hWnd);
  153. UINT res = GetMenuState(hMenu, ID_DISABLE, MF_BYCOMMAND);
  154. if (res & MF_GRAYED)
  155. EnableMenuItem(hMenu, ID_DISABLE, MF_BYCOMMAND | MF_ENABLED);
  156. else
  157. EnableMenuItem(hMenu, ID_DISABLE, MF_BYCOMMAND | MF_GRAYED);
  158. }
  159. break;
  160.  
  161. case ID_DOSOMETHING:
  162. {
  163. MessageBox(NULL, "Something has been done!", "I did it!", MB_OK | MB_ICONASTERISK);
  164. } break;
  165. case ID_ADD:
  166. {
  167. HMENU hMenu = GetMenu(hWnd);
  168.  
  169. if (GetMenuState(hMenu, ID_ADD + 1, MF_BYCOMMAND) == -1) // == -1 – означава, че не // съществува елемент с такова ID - IDM_ADD + 1
  170. {
  171. MENUITEMINFO mii; //съдържа информация за елемент на менюто
  172. ZeroMemory(&mii, sizeof(mii)); //запълва съответния блок от паметта с нули
  173. mii.cbSize = sizeof(mii); //размер на структурата в байтове
  174. mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
  175. mii.wID = ID_ADD + 1; // ID-то на елемента
  176. mii.fType = MFT_STRING; // типа на елемента - string
  177. mii.dwTypeData = LPSTR("Delete me, Alice"); //текст на елем. от менюто
  178. mii.fState = MFS_ENABLED; //да е достъпно
  179.  
  180. InsertMenuItem(hMenu, ID_ADD, FALSE, &mii);
  181. //добавяне на елемента, инициализиран в структурата &mii, пред елемента с посоченото ID, т.е. пред IDM_ADD
  182. }
  183. }
  184. break;
  185.  
  186. case ID_ADD + 1: //при избор елемент с това ID от менюто се изтрива
  187. {
  188. int x = MessageBox(NULL, "Are you sure?", "Delete?", MB_YESNO | MB_ICONQUESTION);
  189. if (x==IDYES) {
  190. HMENU hMenu = GetMenu(hWnd);
  191. DeleteMenu(hMenu, ID_ADD + 1, MF_BYCOMMAND);
  192. } //изтрива елемента с това ID
  193. }
  194. break;
  195.  
  196. case ID_MATH:
  197. DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG3), hWnd, MathTest);
  198. break;
  199. case IDM_CALC:
  200. DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG2), hWnd, Calc);
  201. break; // calc dlg box
  202. case IDM_SWAP:
  203. DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, Swap);
  204. break; // swap dlg box
  205. case IDM_ABOUT:
  206. DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  207. break;
  208. case IDM_EXIT:
  209. DestroyWindow(hWnd);
  210. break;
  211. default:
  212. return DefWindowProc(hWnd, message, wParam, lParam);
  213. }
  214. }
  215. break;
  216.  
  217. case WM_RBUTTONDOWN: //при натиснат десен бутон на мишката
  218. {
  219. HMENU hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_MENU2));
  220. // зарежда темплейта на менюто от ресурсите, съдържащо бързото меню
  221. HMENU hSubMenu = GetSubMenu(hMenu, 0); //взема първото бързо меню от темплейта
  222. POINT pt = { LOWORD(lParam), HIWORD(lParam) }; //текущи координати на мишката
  223. ClientToScreen(hWnd, &pt); //преобразува коорд. на мишката в коорд. на екрана
  224.  
  225. TrackPopupMenu(hSubMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, NULL);
  226. //показва бързо меню и проследява избора на елементи от менюто
  227. DestroyMenu(hMenu); //разрушава менюто и освобождава ресурсите
  228. }
  229. break;
  230.  
  231. case WM_PAINT:
  232. {
  233. PAINTSTRUCT ps;
  234. HDC hdc = BeginPaint(hWnd, &ps);
  235. // TODO: Add any drawing code that uses hdc here...
  236. EndPaint(hWnd, &ps);
  237. }
  238. break;
  239. case WM_DESTROY:
  240. PostQuitMessage(0);
  241. break;
  242. default:
  243. return DefWindowProc(hWnd, message, wParam, lParam);
  244. }
  245. return 0;
  246. }
  247.  
  248. // Message handler for about box.
  249. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  250. {
  251. UNREFERENCED_PARAMETER(lParam);
  252. switch (message)
  253. {
  254. case WM_INITDIALOG:
  255. return (INT_PTR)TRUE;
  256.  
  257. case WM_COMMAND:
  258. if (LOWORD(wParam) == IDC_LUIGI)
  259. {
  260. MessageBox(NULL,
  261. _T("It works?"),
  262. _T("It does!"),
  263. NULL);
  264. }
  265. if (LOWORD(wParam) == ID_ZDR)
  266. {
  267. MessageBox(NULL,
  268. _T("It works?"),
  269. _T("It does!"),
  270. NULL);
  271. }
  272.  
  273. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  274. {
  275. EndDialog(hDlg, LOWORD(wParam));
  276. return (INT_PTR)TRUE;
  277. }
  278. break;
  279. }
  280. return (INT_PTR)FALSE;
  281. }
  282.  
  283. int cor = 0, wr = 0;
  284.  
  285. INT_PTR CALLBACK MathTest(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  286. {
  287. UNREFERENCED_PARAMETER(lParam);
  288. switch (message)
  289. {
  290. case WM_INITDIALOG:
  291.  
  292. //CheckDlgButton(hDlg, IDC_PLUS, BST_CHECKED);
  293.  
  294. CheckDlgButton(hDlg, IDC_R1, BST_CHECKED);
  295.  
  296. return (INT_PTR)TRUE;
  297.  
  298. case WM_COMMAND:
  299.  
  300. if (LOWORD(wParam) == IDC_CHECK)
  301. {
  302. if (IsDlgButtonChecked(hDlg, IDC_PLUS))
  303. {
  304. int a = GetDlgItemInt(hDlg, IDC_A, NULL, true);
  305. int b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  306. int c = GetDlgItemInt(hDlg, IDC_C, NULL, true);
  307. int sum = a + b;
  308. if (sum == c)
  309. {
  310. cor++;
  311. SetDlgItemInt(hDlg, IDC_CORRECT, cor, true);
  312.  
  313. }
  314. else
  315. {
  316. SetDlgItemInt(hDlg, IDC_WRONG, wr, true);
  317. wr++;
  318. }
  319. }
  320.  
  321. if (IsDlgButtonChecked(hDlg, IDC_MINUS))
  322. {
  323. int a = GetDlgItemInt(hDlg, IDC_A, NULL, true);
  324. int b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  325. int c = GetDlgItemInt(hDlg, IDC_C, NULL, true);
  326. int sum = a - b;
  327. if (sum == c)
  328. {
  329. cor++;
  330. SetDlgItemInt(hDlg, IDC_CORRECT, cor, true);
  331.  
  332. }
  333. else
  334. {
  335. SetDlgItemInt(hDlg, IDC_WRONG, wr, true);
  336. wr++;
  337. }
  338. }
  339.  
  340. if (IsDlgButtonChecked(hDlg, IDC_MUL))
  341. {
  342. int a = GetDlgItemInt(hDlg, IDC_A, NULL, true);
  343. int b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  344. int c = GetDlgItemInt(hDlg, IDC_C, NULL, true);
  345. int sum = a * b;
  346. if (sum == c)
  347. {
  348. cor++;
  349. SetDlgItemInt(hDlg, IDC_CORRECT, cor, true);
  350.  
  351. }
  352. else
  353. {
  354. SetDlgItemInt(hDlg, IDC_WRONG, wr, true);
  355. wr++;
  356. }
  357. }
  358.  
  359. if (IsDlgButtonChecked(hDlg, IDC_DIV))
  360. {
  361. int a = GetDlgItemInt(hDlg, IDC_A, NULL, true);
  362. int b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  363. if (b == 0)
  364. {
  365. MessageBox(hDlg,
  366. _T("No Value to divide by or dividing bt zer0 "),
  367. _T("Error!"),
  368. NULL);
  369. break;
  370. }
  371. else
  372. {
  373. int c = GetDlgItemInt(hDlg, IDC_C, NULL, true);
  374. int sum = a / b;
  375. if (sum == c)
  376. {
  377. cor++;
  378. SetDlgItemInt(hDlg, IDC_CORRECT, cor, true);
  379.  
  380. }
  381. else
  382. {
  383. SetDlgItemInt(hDlg, IDC_WRONG, wr, true);
  384. wr++;
  385. }
  386. }
  387. }
  388. }
  389.  
  390. if (LOWORD(wParam) == IDC_RANDOM)
  391. {
  392. if (IsDlgButtonChecked(hDlg, IDC_R1))
  393. {
  394. int a = rand() % (21 + 1 -0) + 0;
  395. int b = rand() % (21 + 1 - 0) + 0;
  396. SetDlgItemInt(hDlg, IDC_A, a, true);
  397. SetDlgItemInt(hDlg, IDC_B, b, true);
  398. }
  399.  
  400. if (IsDlgButtonChecked(hDlg, IDC_R2))
  401. {
  402. int a = rand() % (50 + 1 - 20) + 20;
  403. int b = rand() % (50 + 1 - 21) + 21;
  404. SetDlgItemInt(hDlg, IDC_A, a, true);
  405. SetDlgItemInt(hDlg, IDC_B, b, true);
  406. }
  407. if (IsDlgButtonChecked(hDlg, IDC_R3))
  408. {
  409. int a = rand() % (100 + 1 - 51) + 51;
  410. int b = rand() % (100 + 1 - 51) + 51;
  411. SetDlgItemInt(hDlg, IDC_A, a, true);
  412. SetDlgItemInt(hDlg, IDC_B, b, true);
  413. }
  414. }
  415.  
  416.  
  417.  
  418. if (LOWORD(wParam) == IDC_CLEAR)
  419. {
  420. SetDlgItemText(hDlg, IDC_A, NULL);
  421. SetDlgItemText(hDlg, IDC_B, NULL);
  422. SetDlgItemText(hDlg, IDC_C, NULL);
  423. }
  424.  
  425. if (LOWORD(wParam) == IDC_FINISH)
  426. {
  427. // TO DO
  428. double res;
  429. if (cor > wr)
  430. {
  431. res = wr / cor;
  432. }
  433. else
  434. {
  435. res = cor / wr;
  436. }
  437.  
  438. res *=100;
  439. char s1[3];
  440. // res = atof(s1);
  441. string s2 = to_string(res);
  442. // res.c_tring();
  443. MessageBox(hDlg,
  444. _T(s2.c_str(),"%"),
  445. _T(" Not yet Done"),
  446. NULL);
  447. break;
  448. }
  449.  
  450. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  451. {
  452. EndDialog(hDlg, LOWORD(wParam));
  453. return (INT_PTR)TRUE;
  454. }
  455. break;
  456. }
  457. return (INT_PTR)FALSE;
  458. }
  459.  
  460.  
  461. INT_PTR CALLBACK Swap(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) //dialog box for SWAPPING "int" and "text"
  462. {
  463. UNREFERENCED_PARAMETER(lParam);
  464. switch (message)
  465. {
  466. case WM_INITDIALOG:
  467.  
  468.  
  469. return (INT_PTR)TRUE;
  470.  
  471. case WM_COMMAND:
  472. if (LOWORD(wParam) == ID_SWAPINT) {
  473. int a = GetDlgItemInt(hDlg, IDC_EDIT1, NULL, true);
  474. int b = GetDlgItemInt(hDlg, IDC_EDIT2, NULL, true);
  475.  
  476. SetDlgItemInt(hDlg, IDC_EDIT2, a, true);
  477. SetDlgItemInt(hDlg, IDC_EDIT1, b, true);
  478. }
  479.  
  480. if (LOWORD(wParam) == ID_SWAPTXT) {
  481. char t1[20],t2[20];
  482. GetDlgItemText(hDlg, IDC_EDIT3, t1, 20);
  483. GetDlgItemText(hDlg, IDC_EDIT4, t2, 20);
  484.  
  485. SetDlgItemText(hDlg, IDC_EDIT3, t2);
  486. SetDlgItemText(hDlg, IDC_EDIT4, t1);
  487. }
  488.  
  489.  
  490.  
  491. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  492. {
  493. EndDialog(hDlg, LOWORD(wParam));
  494. return (INT_PTR)TRUE;
  495. }
  496. break;
  497. }
  498. return (INT_PTR)FALSE;
  499. }
  500.  
  501. /*INT_PTR CALLBACK Calc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  502. {
  503. UNREFERENCED_PARAMETER(lParam);
  504. switch (message)
  505. {
  506. case WM_INITDIALOG:
  507. return (INT_PTR)TRUE;
  508.  
  509. case WM_COMMAND:
  510. if (LOWORD(wParam) == ID_PLUS)
  511. {
  512. int a, b, c;
  513. a= GetDlgItemInt(hDlg, IDC_A, NULL, true);
  514. b= GetDlgItemInt(hDlg, IDC_B, NULL, true);
  515.  
  516. c = a + b;
  517.  
  518. SetDlgItemInt(hDlg, IDC_RESULT, c, true);
  519. }
  520.  
  521. if (LOWORD(wParam) == ID_MINUS)
  522. {
  523. int a, b, c;
  524. a = GetDlgItemInt(hDlg, IDC_A, NULL, true);
  525. b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  526.  
  527. c = a - b;
  528.  
  529. SetDlgItemInt(hDlg, IDC_RESULT, c, true);
  530. }
  531.  
  532. if (LOWORD(wParam) == ID_MULT)
  533. {
  534. int a, b, c;
  535. a = GetDlgItemInt(hDlg, IDC_A, NULL, true);
  536. b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  537.  
  538. c = a * b;
  539.  
  540. SetDlgItemInt(hDlg, IDC_RESULT, c, true);
  541. }
  542.  
  543. if (LOWORD(wParam) == ID_DIVIDE)
  544. {
  545. int a, b, c;
  546. a = GetDlgItemInt(hDlg, IDC_A, NULL, true);
  547. b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  548.  
  549. if (0 == b)
  550. {
  551. MessageBox(hDlg,
  552. _T("No Value to divide by or dividing bt zer0 "),
  553. _T("Error!"),
  554. NULL);
  555. break;
  556. }
  557.  
  558. else
  559. {
  560. c = a / b;
  561.  
  562. SetDlgItemInt(hDlg, IDC_RESULT, c, true);
  563. }
  564. }
  565.  
  566. if (LOWORD(wParam) == ID_MULT)
  567. {
  568. int a, b, c;
  569. a = GetDlgItemInt(hDlg, IDC_A, NULL, true);
  570. b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  571.  
  572. c = a * b;
  573.  
  574. SetDlgItemInt(hDlg, IDC_RESULT, c, true);
  575. }
  576.  
  577. if (LOWORD(wParam) == IDC_A2)
  578. {
  579. int a, c;
  580. a = GetDlgItemInt(hDlg, IDC_A, NULL, true);
  581. //b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  582.  
  583. c = a * a;
  584.  
  585. SetDlgItemInt(hDlg, IDC_RESULT, c, true);
  586. }
  587.  
  588. if (LOWORD(wParam) == IDC_B2)
  589. {
  590. int a, c;
  591. a = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  592. //b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  593.  
  594. c = a * a;
  595.  
  596. SetDlgItemInt(hDlg, IDC_RESULT, c, true);
  597. }
  598.  
  599. if (LOWORD(wParam) == IDC_LEFTOVER)
  600. {
  601. int a, b, c;
  602. a = GetDlgItemInt(hDlg, IDC_A, NULL, true);
  603. b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  604. if (0 == b)
  605. {
  606. MessageBox(hDlg,
  607. _T("No Value to divide by or dividing bt zer0 "),
  608. _T("Error!"),
  609. NULL);
  610. break;
  611. }
  612.  
  613. else
  614. {
  615. c = a % b;
  616.  
  617. SetDlgItemInt(hDlg, IDC_RESULT, c, true);
  618. }
  619. }
  620.  
  621. if (LOWORD(wParam) == IDC_CLEAR)
  622. {
  623. SetDlgItemText(hDlg, IDC_A, NULL);
  624. SetDlgItemText(hDlg, IDC_B, NULL);
  625. SetDlgItemText(hDlg, IDC_RESULT, NULL);
  626. }
  627.  
  628. if (LOWORD(wParam) == IDC_RAND)
  629. {
  630. int a = rand() % 100;
  631. int b = rand() % 100;
  632. SetDlgItemInt(hDlg, IDC_A, a,true);
  633. SetDlgItemInt(hDlg, IDC_B, b, true);
  634. }
  635.  
  636. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  637. {
  638. EndDialog(hDlg, LOWORD(wParam));
  639. return (INT_PTR)TRUE;
  640. }
  641. break;
  642. }
  643. return (INT_PTR)FALSE;
  644. } */
  645.  
  646. INT_PTR CALLBACK Calc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  647. {
  648. UNREFERENCED_PARAMETER(lParam);
  649. switch (message)
  650. {
  651. case WM_INITDIALOG:
  652. return (INT_PTR)TRUE;
  653.  
  654. case WM_COMMAND:
  655. if (LOWORD(wParam) == ID_PLUS)
  656. {
  657. char s1[20], s2[20];
  658. GetDlgItemText(hDlg, IDC_A, s1, 20);
  659. GetDlgItemText(hDlg, IDC_B, s2, 20);
  660.  
  661. double a = atof(s1);
  662. double b = atof(s2);
  663. double c = a + b;
  664. string s3 = to_string(c);
  665.  
  666. size_t t = s3.find_last_not_of('0');
  667.  
  668. int l = s3.length() - t;
  669. s3.erase(t + 1, l);
  670.  
  671. SetDlgItemText(hDlg, IDC_RESULT, s3.c_str());
  672. }
  673.  
  674. if (LOWORD(wParam) == ID_MINUS)
  675. {
  676. char s1[20], s2[20];
  677. GetDlgItemText(hDlg, IDC_A, s1, 20);
  678. GetDlgItemText(hDlg, IDC_B, s2, 20);
  679.  
  680. double a = atof(s1);
  681. double b = atof(s2);
  682. double c = a - b;
  683. string s3 = to_string(c);
  684.  
  685. size_t t = s3.find_last_not_of('0');
  686.  
  687. int l = s3.length() - t;
  688. s3.erase(t + 1, l);
  689.  
  690. SetDlgItemText(hDlg, IDC_RESULT, s3.c_str());
  691. }
  692.  
  693. if (LOWORD(wParam) == ID_MULT)
  694. {
  695. char s1[20], s2[20];
  696. GetDlgItemText(hDlg, IDC_A, s1, 20);
  697. GetDlgItemText(hDlg, IDC_B, s2, 20);
  698.  
  699. double a = atof(s1);
  700. double b = atof(s2);
  701. double c = a * b;
  702. string s3 = to_string(c);
  703.  
  704. size_t t = s3.find_last_not_of('0');
  705.  
  706. int l = s3.length() - t;
  707. s3.erase(t + 1, l);
  708.  
  709. SetDlgItemText(hDlg, IDC_RESULT, s3.c_str());
  710. }
  711.  
  712. if (LOWORD(wParam) == ID_DIVIDE)
  713. {
  714. char s1[20], s2[20];
  715. GetDlgItemText(hDlg, IDC_A, s1, 20);
  716. GetDlgItemText(hDlg, IDC_B, s2, 20);
  717.  
  718. double a = atof(s1);
  719. double b = atof(s2);
  720.  
  721. if (0 == b)
  722. {
  723. MessageBox(hDlg,
  724. _T("No Value to divide by or dividing bt zer0 "),
  725. _T("Error!"),
  726. NULL);
  727. break;
  728. }
  729.  
  730. else
  731. {
  732. double c = a / b;
  733.  
  734. string s3 = to_string(c);
  735.  
  736. size_t t = s3.find_last_not_of('0');
  737.  
  738. int l = s3.length() - t;
  739. s3.erase(t + 1, l);
  740.  
  741. SetDlgItemText(hDlg, IDC_RESULT, s3.c_str());
  742. }
  743. }
  744.  
  745.  
  746. if (LOWORD(wParam) == IDC_A2)
  747. {
  748. char s1[20];
  749.  
  750. GetDlgItemText(hDlg, IDC_A, s1, 20);
  751.  
  752. double a = atof(s1);
  753. //b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  754. double c = a * a;
  755.  
  756. string s3 = to_string(c);
  757.  
  758. size_t t = s3.find_last_not_of('0');
  759.  
  760. int l = s3.length() - t;
  761. s3.erase(t + 1, l);
  762.  
  763. SetDlgItemText(hDlg, IDC_RESULT, s3.c_str());
  764.  
  765. }
  766.  
  767. if (LOWORD(wParam) == IDC_B2)
  768. {
  769. char s1[20];
  770.  
  771. GetDlgItemText(hDlg, IDC_B, s1, 20);
  772.  
  773. double a = atof(s1);
  774. //b = GetDlgItemInt(hDlg, IDC_B, NULL, true);
  775. double c = a * a;
  776.  
  777. string s3 = to_string(c);
  778.  
  779. size_t t = s3.find_last_not_of('0');
  780.  
  781. int l = s3.length() - t;
  782. s3.erase(t + 1, l);
  783.  
  784. SetDlgItemText(hDlg, IDC_RESULT, s3.c_str());
  785. }
  786.  
  787. if (LOWORD(wParam) == IDC_LEFTOVER)
  788. {
  789. char s1[20], s2[20];
  790. GetDlgItemText(hDlg, IDC_A, s1, 20);
  791. GetDlgItemText(hDlg, IDC_B, s2, 20);
  792.  
  793. double a = atof(s1);
  794. double b = atof(s2);
  795.  
  796. if (0 == b)
  797. {
  798. MessageBox(hDlg,
  799. _T("No Value to divide by or dividing bt zer0 "),
  800. _T("Error!"),
  801. NULL);
  802. break;
  803. }
  804.  
  805. else
  806. {
  807. double c = a / b;
  808.  
  809. string s3 = to_string(c);
  810.  
  811. size_t t = s3.find_last_not_of('0');
  812.  
  813. int l = s3.length() - t;
  814. s3.erase(t + 1, l);
  815.  
  816. SetDlgItemText(hDlg, IDC_RESULT, s3.c_str());
  817. }
  818. }
  819. if (LOWORD(wParam) == IDC_CLEAR)
  820. {
  821. SetDlgItemText(hDlg, IDC_A, NULL);
  822. SetDlgItemText(hDlg, IDC_B, NULL);
  823. SetDlgItemText(hDlg, IDC_RESULT, NULL);
  824. }
  825.  
  826. if (LOWORD(wParam) == IDC_RAND)
  827. {
  828. double a = (double)rand() / 100;
  829.  
  830. double b = (double)rand() / 100 ;
  831.  
  832.  
  833. string s1 = to_string(a);
  834. string s2 = to_string(b);
  835.  
  836.  
  837. SetDlgItemText(hDlg, IDC_A, s1.c_str());
  838. SetDlgItemText(hDlg, IDC_B, s2.c_str());
  839. }
  840.  
  841. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  842. {
  843. EndDialog(hDlg, LOWORD(wParam));
  844. return (INT_PTR)TRUE;
  845. }
  846. break;
  847. }
  848.  
  849. return (INT_PTR)FALSE;
  850. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement