#include #include "list.h" BOOL CALLBACK digListEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam); BOOL CALLBACK digListEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam) { if (event == WM_INITDIALOG) { SetDlgItemText(hwnd, txtItem, "Dutch language"); } else if (event == WM_COMMAND) { int objectID = LOWORD(object); if (objectID == cmdAdd) { //Unlike in java where you use txtItem.getText(), in C++ //you have to copy the text from the textfield into a variable (char array) //First need to find out how many chars are in the text. GetWindowTextLength //does not directly get the POINTER ID of the item on the dialogue box, so //have to use GetDlgItem, to get the POINTER ID. //IMPORTANT, GetWindowTextLength returns the amount of chars, minus the null terminator $ //Thus it may say 14, but the actual text is 14 + $, so if you used that directly the null //terminator would overwrite the next space in memory, thus you must add 1 to length int len = GetWindowTextLength(GetDlgItem(hwnd, txtItem)); if (len > 0) //Something is typed { len += 1; char* buffer; //GlobalAlloc reserves space in memory, filling it with 0's and returns //the pointer of where the space starts. buffer = (char*)GlobalAlloc(GPTR, len); GetDlgItemText(hwnd, txtItem, buffer, len); SendDlgItemMessage(hwnd, lstItems, LB_ADDSTRING, 0, (LPARAM)buffer); GlobalFree((HANDLE)buffer); SetDlgItemText(hwnd, txtItem, ""); } else { MessageBox(hwnd, "Field is empty!", "Error", MB_OK | MB_ICONEXCLAMATION); } } else if (objectID == cmdRemove) { HWND handleDig = GetDlgItem(hwnd, lstItems); int index = SendMessage(handleDig, LB_GETCURSEL, 0, 0); if (index != LB_ERR) { SendMessage(handleDig, LB_DELETESTRING, (WPARAM)index, 0); } else { MessageBox(hwnd, "You must select an item to remove it", "Error", MB_OK | MB_ICONEXCLAMATION); } } else if (objectID == cmdClear) { SendDlgItemMessage(hwnd, lstItems, LB_RESETCONTENT, 0, 0); } 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(digList), NULL, digListEventListener); }