#include #include #include "currencyConverter.h" //0 = pound, 1 = euro, 2 = dollar. 1st item = FROM, 2nd item = TO const char currencies[3][7] = {"Pound", "Euro", "Dollar"}; const float conversions[3][3] = {{1.0, 1.4513, 1.873}, {0.6889, 1.0, 1.2903}, {0.534, 0.775, 1.0}}; float parseFloat (HWND hwnd, int itemID, BOOL &sucess); void performConversion (HWND hwnd); BOOL CALLBACK digCCEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam); float parseFloat (HWND hwnd, int itemID, BOOL &success) { float amount = 0.0f; int strLen = GetWindowTextLength(GetDlgItem(hwnd, itemID)); success = (strLen > 0); if (success) //Make sure something is typed { strLen++; //The original value does not include the NULL character //Reserves so many places in memory for the new char array char* buffer = (char*)GlobalAlloc(GPTR, strLen); //Gets the string and puts it in the buffer GetDlgItemText(hwnd, txtAmount, buffer, strLen); amount = atof(buffer); //Converts the string into a float GlobalFree((HANDLE)buffer); //Removes the array from memory } return amount; } void performConversion (HWND hwnd) { BOOL success; float amount = parseFloat(hwnd, txtAmount, success); if (!success) { MessageBox(hwnd, "Please enter a currency amount", "Missing fields", MB_OK | MB_ICONEXCLAMATION); } else if (amount < 0.01f) { MessageBox(hwnd, "Please only enter positive currency amounts", "Invalid range", MB_OK | MB_ICONEXCLAMATION); } else if (amount > 1000000.0f) { MessageBox(hwnd, "Please enter currency amounts under one million", "Invalid range", MB_OK | MB_ICONEXCLAMATION); } else { char strAnswer[15]; unsigned short int fromIndex = SendMessage(GetDlgItem(hwnd, cboxFrom), CB_GETCURSEL, 0, 0); unsigned short int toIndex = SendMessage(GetDlgItem(hwnd, cboxTo), CB_GETCURSEL, 0, 0); float answer = amount * conversions[fromIndex][toIndex]; if (toIndex == 0) { sprintf(strAnswer, "£%7.2f", answer); } else if (toIndex == 1) { sprintf(strAnswer, "€%7.2f", answer); } else { sprintf(strAnswer, "$%7.2f", answer); } SetDlgItemText(hwnd, lblAnswer, strAnswer); } } BOOL CALLBACK digCCEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam) { if (event == WM_INITDIALOG) { //Get a handle for the object to send messages to HWND handleCboxFrom = GetDlgItem(hwnd, cboxFrom); HWND handleCboxTo = GetDlgItem(hwnd, cboxTo); for (int i = 0; i < 3; i++) { SendMessage(handleCboxFrom, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)currencies[i]); SendMessage(handleCboxTo, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)currencies[i]); } //Set the selected item in the combo box, WPARAM = index, LPARAM = 0 SendMessage(handleCboxFrom, CB_SETCURSEL, 0, 0); SendMessage(handleCboxTo, CB_SETCURSEL, 1, 0); SetDlgItemText(hwnd, txtAmount, "1.0"); performConversion(hwnd); } else if (event == WM_COMMAND) { int objectID = LOWORD(object); if (objectID == cmdConvert) { performConversion(hwnd); } else if (objectID == cmdClose) { EndDialog(hwnd, objectID); } } else if (event == WM_CLOSE) { EndDialog(hwnd, object); } else { return false; } return true; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR args, int nCmdShow) { return DialogBox(hInstance, MAKEINTRESOURCE(digCC), NULL, digCCEventListener); }