Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.58 KB | None | 0 0
  1. // Project: CIS023_F2018_HW12 Roshmund Romero
  2. // Module: CIS023_F2018_HW12 Roshmund Romero.cpp
  3. // Author: Roshmund Romero
  4. // Date: September 22, 2018
  5. // Purpose: Load and use data in an array of objects.
  6. // Use a virtual function.
  7. // Review menu operation.
  8. //
  9.  
  10. #include "stdafx.h"
  11. #include "CIS023_F2018_HW12 Roshmund Romero.h"
  12.  
  13. #define MAX_LOADSTRING 100
  14. #define SIZE_ARRAY 9 // size of CarList Array
  15.  
  16. // Global Variables:
  17. HINSTANCE hInst; // current instance
  18. WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  19. WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
  20.  
  21. HWND hWnd_ListBox; // list box controls
  22. string CarList[9][4]; // size of NumberList Array]
  23.  
  24. // Forward declarations of functions included in this code module:
  25. ATOM MyRegisterClass(HINSTANCE hInstance);
  26. BOOL InitInstance(HINSTANCE, int);
  27. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  28. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  29.  
  30. bool LoadFile(); // load text file into array
  31. void LoadListbox(); // load listbox from array
  32.  
  33. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  34. _In_opt_ HINSTANCE hPrevInstance,
  35. _In_ LPWSTR lpCmdLine,
  36. _In_ int nCmdShow)
  37. {
  38. UNREFERENCED_PARAMETER(hPrevInstance);
  39. UNREFERENCED_PARAMETER(lpCmdLine);
  40.  
  41. // TODO: Place code here.
  42.  
  43. // Initialize global strings
  44. LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  45. LoadStringW(hInstance, IDC_CIS023_F2018_HW12ROSHMUNDROMERO, szWindowClass, MAX_LOADSTRING);
  46. MyRegisterClass(hInstance);
  47.  
  48. // Perform application initialization:
  49. if (!InitInstance (hInstance, nCmdShow))
  50. {
  51. return FALSE;
  52. }
  53.  
  54. HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CIS023_F2018_HW12ROSHMUNDROMERO));
  55.  
  56. MSG msg;
  57.  
  58. // Main message loop:
  59. while (GetMessage(&msg, nullptr, 0, 0))
  60. {
  61. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  62. {
  63. TranslateMessage(&msg);
  64. DispatchMessage(&msg);
  65. }
  66. }
  67.  
  68. return (int) msg.wParam;
  69. }
  70.  
  71.  
  72.  
  73. //
  74. // FUNCTION: MyRegisterClass()
  75. //
  76. // PURPOSE: Registers the window class.
  77. //
  78. ATOM MyRegisterClass(HINSTANCE hInstance)
  79. {
  80. WNDCLASSEXW wcex;
  81.  
  82. wcex.cbSize = sizeof(WNDCLASSEX);
  83.  
  84. wcex.style = CS_HREDRAW | CS_VREDRAW;
  85. wcex.lpfnWndProc = WndProc;
  86. wcex.cbClsExtra = 0;
  87. wcex.cbWndExtra = 0;
  88. wcex.hInstance = hInstance;
  89. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CIS023_F2018_HW12ROSHMUNDROMERO));
  90. wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  91. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+0);
  92. wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_CIS023_F2018_HW12ROSHMUNDROMERO);
  93. wcex.lpszClassName = szWindowClass;
  94. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  95.  
  96. return RegisterClassExW(&wcex);
  97. }
  98.  
  99. //
  100. // FUNCTION: InitInstance(HINSTANCE, int)
  101. //
  102. // PURPOSE: Saves instance handle and creates main window
  103. //
  104. // COMMENTS:
  105. //
  106. // In this function, we save the instance handle in a global variable and
  107. // create and display the main program window.
  108. //
  109. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  110. {
  111. hInst = hInstance; // Store instance handle in our global variable
  112.  
  113. HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  114. CW_USEDEFAULT, 0, 600, 300, nullptr, nullptr, hInstance, nullptr);
  115.  
  116. if (!hWnd)
  117. {
  118. return FALSE;
  119. }
  120.  
  121. ShowWindow(hWnd, nCmdShow);
  122. UpdateWindow(hWnd);
  123.  
  124. return TRUE;
  125. }
  126.  
  127. //
  128. // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  129. //
  130. // PURPOSE: Processes messages for the main window.
  131. //
  132. // WM_COMMAND - process the application menu
  133. // WM_PAINT - Paint the main window
  134. // WM_DESTROY - post a quit message and return
  135. //
  136. //
  137. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  138. {
  139. switch (message)
  140. {
  141. case WM_COMMAND:
  142. {
  143. int wmId = LOWORD(wParam);
  144. // Parse the menu selections:
  145. switch (wmId)
  146. {
  147. case ID_FILE_LOAD:
  148. {
  149. LoadFile();
  150. //LoadListbox();
  151.  
  152. TCHAR szOutput[100]; // output buffer
  153. string strOutput;
  154. string strStore;
  155. size_t pReturnValue; // return code for mbstowcs_s
  156.  
  157.  
  158. strOutput =
  159. CarList[0][0]; // largest int found in array (&)
  160.  
  161.  
  162.  
  163.  
  164. mbstowcs_s( // convert string to TCHAR
  165. &pReturnValue, // return value
  166. szOutput, // outputo TCHAR
  167. 100, // size of TCHAR
  168. strOutput.c_str(), // source string
  169. 100); // max chars to conver
  170.  
  171.  
  172. SendMessage( // send a message to a window
  173. hWnd_ListBox, // handle of window
  174. LB_ADDSTRING, // messgae id
  175. NULL, // WPARAM, not used
  176. (LPARAM)szOutput); // THCAR to add to listbox // LPARAM, not used
  177.  
  178. }
  179. break;
  180. case IDM_ABOUT:
  181. DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  182. break;
  183. case IDM_EXIT:
  184. DestroyWindow(hWnd);
  185. break;
  186. default:
  187. return DefWindowProc(hWnd, message, wParam, lParam);
  188. }
  189. }
  190. break;
  191. case WM_PAINT:
  192. {
  193. PAINTSTRUCT ps;
  194. HDC hdc = BeginPaint(hWnd, &ps);
  195. // TODO: Add any drawing code that uses hdc here...
  196. EndPaint(hWnd, &ps);
  197. }
  198. break;
  199.  
  200. case WM_CREATE:
  201. {
  202. // create list box
  203. hWnd_ListBox = CreateWindow(
  204. TEXT("LISTBOX"), // type of object
  205. TEXT(""), // starts blank (filled later)
  206. WS_VISIBLE | WS_CHILD | // attributes
  207. WS_VSCROLL | WS_BORDER |
  208. LBS_SORT, // sorted list box
  209. 20, 50, 545, 190, // location and size
  210. hWnd, NULL, NULL, NULL); // parent window, the rest are not used here
  211.  
  212. // set listbox font to fixed width
  213.  
  214. HFONT hFont = (HFONT)GetStockObject(OEM_FIXED_FONT);
  215. SendMessage(hWnd_ListBox, WM_SETFONT, WPARAM(hFont), NULL);
  216.  
  217. // labels
  218. CreateWindow(TEXT("STATIC"),
  219. TEXT("Vehicle Name"),
  220. WS_VISIBLE | WS_CHILD,
  221. 20, 20, 800, 30, hWnd, NULL, NULL, NULL);
  222.  
  223. CreateWindow(TEXT("STATIC"),
  224. TEXT("Engine Type"),
  225. WS_VISIBLE | WS_CHILD,
  226. 215, 20, 800, 30, hWnd, NULL, NULL, NULL);
  227.  
  228. CreateWindow(TEXT("STATIC"),
  229. TEXT("Style"),
  230. WS_VISIBLE | WS_CHILD,
  231. 310, 20, 60, 30, hWnd, NULL, NULL, NULL);
  232.  
  233. CreateWindow(TEXT("STATIC"),
  234. TEXT("Occupants"),
  235. WS_VISIBLE | WS_CHILD,
  236. 415, 20, 70, 30, hWnd, NULL, NULL, NULL);
  237.  
  238. CreateWindow(TEXT("STATIC"),
  239. TEXT("Tax"),
  240. WS_VISIBLE | WS_CHILD,
  241. 535, 20, 50, 30, hWnd, NULL, NULL, NULL);
  242. }
  243. break;
  244.  
  245. case WM_DESTROY:
  246. PostQuitMessage(0);
  247. break;
  248. default:
  249. return DefWindowProc(hWnd, message, wParam, lParam);
  250. }
  251. return 0;
  252. }
  253.  
  254. // Message handler for about box.
  255. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  256. {
  257. UNREFERENCED_PARAMETER(lParam);
  258. switch (message)
  259. {
  260. case WM_INITDIALOG:
  261. return (INT_PTR)TRUE;
  262.  
  263. case WM_COMMAND:
  264. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  265. {
  266. EndDialog(hDlg, LOWORD(wParam));
  267. return (INT_PTR)TRUE;
  268. }
  269. break;
  270. }
  271. return (INT_PTR)FALSE;
  272. }
  273.  
  274. bool LoadFile()
  275. {
  276. ifstream inFile; // file handle
  277. string strFilespec = // file name
  278. "CIS023_F2018_HW12_Data.txt";
  279. TCHAR szBuffer[100]; // array integer converted to TCHAR
  280. string strName;
  281. string strEngine;
  282. string strStyle;
  283. string strOccupants;
  284. size_t pReturnValue; // return code for mbstowcs_s
  285. TCHAR szOutput[100]; // output to list box
  286.  
  287. inFile.open(strFilespec); // open the file
  288.  
  289. if (inFile.is_open()) // did it open
  290. {
  291. // read file into array
  292. for (int i = 0; i < 9; i++) // read SIZE_ARRAY number of lines from file
  293. {
  294. getline(inFile, strName, ','); // read a line from the array
  295. getline(inFile, strEngine, ','); // read a line from the array
  296. getline(inFile, strStyle, ','); // read a line from the array
  297. getline(inFile, strOccupants, '\n'); // read a line from the array
  298.  
  299. int j = 0;
  300. if ( j < 4)
  301. {
  302.  
  303.  
  304. CarList[i][j] = // copy line from file into array
  305. strName.c_str(); // convert string object to integer
  306.  
  307. j++;
  308.  
  309. CarList[i][j] += // copy line from file into array
  310. strEngine.c_str();; // convert string object to integer
  311.  
  312. j++;
  313.  
  314. CarList[i][j] += // copy line from file into array
  315. strStyle.c_str();; // convert string object to integer
  316.  
  317. j++;
  318.  
  319. CarList[i][j] += // copy line from file into array
  320. strOccupants.c_str(); // convert string object to integer
  321.  
  322. j = 0;
  323. }
  324. }
  325. inFile.close(); // close the file
  326. }
  327. else
  328. return false; // file did not open
  329.  
  330. for (int i = 0; i < SIZE_ARRAY; i++) // load array into
  331. {
  332.  
  333. mbstowcs_s(
  334. &pReturnValue,
  335. szOutput,
  336. 100,
  337. strName.c_str(),
  338. 100); // max chars to conver
  339.  
  340. SendMessage( // send a message to a window
  341. hWnd_ListBox, // handle of window
  342. LB_ADDSTRING, // messgae id
  343. NULL, // WPARAM, not used
  344. (LPARAM)szBuffer); // THCAR to add to listbox
  345. }
  346.  
  347. return true; // file okay
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement