Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Project: CIS023_F2018_HW12 Roshmund Romero
- // Module: CIS023_F2018_HW12 Roshmund Romero.cpp
- // Author: Roshmund Romero
- // Date: September 22, 2018
- // Purpose: Load and use data in an array of objects.
- // Use a virtual function.
- // Review menu operation.
- //
- #include "stdafx.h"
- #include "CIS023_F2018_HW12 Roshmund Romero.h"
- #define MAX_LOADSTRING 100
- #define SIZE_ARRAY 9 // size of CarList Array
- // Global Variables:
- HINSTANCE hInst; // current instance
- WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
- WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
- HWND hWnd_ListBox; // list box controls
- string CarList[9][4]; // size of NumberList Array]
- // Forward declarations of functions included in this code module:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- bool LoadFile(); // load text file into array
- void LoadListbox(); // load listbox from array
- int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPWSTR lpCmdLine,
- _In_ int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- // TODO: Place code here.
- // Initialize global strings
- LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadStringW(hInstance, IDC_CIS023_F2018_HW12ROSHMUNDROMERO, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- // Perform application initialization:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CIS023_F2018_HW12ROSHMUNDROMERO));
- MSG msg;
- // Main message loop:
- while (GetMessage(&msg, nullptr, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int) msg.wParam;
- }
- //
- // FUNCTION: MyRegisterClass()
- //
- // PURPOSE: Registers the window class.
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEXW wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CIS023_F2018_HW12ROSHMUNDROMERO));
- wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+0);
- wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_CIS023_F2018_HW12ROSHMUNDROMERO);
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
- return RegisterClassExW(&wcex);
- }
- //
- // FUNCTION: InitInstance(HINSTANCE, int)
- //
- // PURPOSE: Saves instance handle and creates main window
- //
- // COMMENTS:
- //
- // In this function, we save the instance handle in a global variable and
- // create and display the main program window.
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- hInst = hInstance; // Store instance handle in our global variable
- HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, 600, 300, nullptr, nullptr, hInstance, nullptr);
- if (!hWnd)
- {
- return FALSE;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- //
- // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
- //
- // PURPOSE: Processes messages for the main window.
- //
- // WM_COMMAND - process the application menu
- // WM_PAINT - Paint the main window
- // WM_DESTROY - post a quit message and return
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_COMMAND:
- {
- int wmId = LOWORD(wParam);
- // Parse the menu selections:
- switch (wmId)
- {
- case ID_FILE_LOAD:
- {
- LoadFile();
- //LoadListbox();
- TCHAR szOutput[100]; // output buffer
- string strOutput;
- string strStore;
- size_t pReturnValue; // return code for mbstowcs_s
- strOutput =
- CarList[0][0]; // largest int found in array (&)
- mbstowcs_s( // convert string to TCHAR
- &pReturnValue, // return value
- szOutput, // outputo TCHAR
- 100, // size of TCHAR
- strOutput.c_str(), // source string
- 100); // max chars to conver
- SendMessage( // send a message to a window
- hWnd_ListBox, // handle of window
- LB_ADDSTRING, // messgae id
- NULL, // WPARAM, not used
- (LPARAM)szOutput); // THCAR to add to listbox // LPARAM, not used
- }
- break;
- case IDM_ABOUT:
- DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
- break;
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- }
- break;
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hWnd, &ps);
- // TODO: Add any drawing code that uses hdc here...
- EndPaint(hWnd, &ps);
- }
- break;
- case WM_CREATE:
- {
- // create list box
- hWnd_ListBox = CreateWindow(
- TEXT("LISTBOX"), // type of object
- TEXT(""), // starts blank (filled later)
- WS_VISIBLE | WS_CHILD | // attributes
- WS_VSCROLL | WS_BORDER |
- LBS_SORT, // sorted list box
- 20, 50, 545, 190, // location and size
- hWnd, NULL, NULL, NULL); // parent window, the rest are not used here
- // set listbox font to fixed width
- HFONT hFont = (HFONT)GetStockObject(OEM_FIXED_FONT);
- SendMessage(hWnd_ListBox, WM_SETFONT, WPARAM(hFont), NULL);
- // labels
- CreateWindow(TEXT("STATIC"),
- TEXT("Vehicle Name"),
- WS_VISIBLE | WS_CHILD,
- 20, 20, 800, 30, hWnd, NULL, NULL, NULL);
- CreateWindow(TEXT("STATIC"),
- TEXT("Engine Type"),
- WS_VISIBLE | WS_CHILD,
- 215, 20, 800, 30, hWnd, NULL, NULL, NULL);
- CreateWindow(TEXT("STATIC"),
- TEXT("Style"),
- WS_VISIBLE | WS_CHILD,
- 310, 20, 60, 30, hWnd, NULL, NULL, NULL);
- CreateWindow(TEXT("STATIC"),
- TEXT("Occupants"),
- WS_VISIBLE | WS_CHILD,
- 415, 20, 70, 30, hWnd, NULL, NULL, NULL);
- CreateWindow(TEXT("STATIC"),
- TEXT("Tax"),
- WS_VISIBLE | WS_CHILD,
- 535, 20, 50, 30, hWnd, NULL, NULL, NULL);
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- // Message handler for about box.
- INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- UNREFERENCED_PARAMETER(lParam);
- switch (message)
- {
- case WM_INITDIALOG:
- return (INT_PTR)TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return (INT_PTR)TRUE;
- }
- break;
- }
- return (INT_PTR)FALSE;
- }
- bool LoadFile()
- {
- ifstream inFile; // file handle
- string strFilespec = // file name
- "CIS023_F2018_HW12_Data.txt";
- TCHAR szBuffer[100]; // array integer converted to TCHAR
- string strName;
- string strEngine;
- string strStyle;
- string strOccupants;
- size_t pReturnValue; // return code for mbstowcs_s
- TCHAR szOutput[100]; // output to list box
- inFile.open(strFilespec); // open the file
- if (inFile.is_open()) // did it open
- {
- // read file into array
- for (int i = 0; i < 9; i++) // read SIZE_ARRAY number of lines from file
- {
- getline(inFile, strName, ','); // read a line from the array
- getline(inFile, strEngine, ','); // read a line from the array
- getline(inFile, strStyle, ','); // read a line from the array
- getline(inFile, strOccupants, '\n'); // read a line from the array
- int j = 0;
- if ( j < 4)
- {
- CarList[i][j] = // copy line from file into array
- strName.c_str(); // convert string object to integer
- j++;
- CarList[i][j] += // copy line from file into array
- strEngine.c_str();; // convert string object to integer
- j++;
- CarList[i][j] += // copy line from file into array
- strStyle.c_str();; // convert string object to integer
- j++;
- CarList[i][j] += // copy line from file into array
- strOccupants.c_str(); // convert string object to integer
- j = 0;
- }
- }
- inFile.close(); // close the file
- }
- else
- return false; // file did not open
- for (int i = 0; i < SIZE_ARRAY; i++) // load array into
- {
- mbstowcs_s(
- &pReturnValue,
- szOutput,
- 100,
- strName.c_str(),
- 100); // max chars to conver
- SendMessage( // send a message to a window
- hWnd_ListBox, // handle of window
- LB_ADDSTRING, // messgae id
- NULL, // WPARAM, not used
- (LPARAM)szBuffer); // THCAR to add to listbox
- }
- return true; // file okay
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement