#Include "windows.bi" #Include "resource.bi" Dim Shared As HMODULE hInstance Dim Shared As ZString Ptr lpCmdLine Function DlgProc(hwnd As HWND, Message As UINT, wParam As WPARAM, lParam As LPARAM) As BOOL Select Case Message Case WM_INITDIALOG ' This is where we set up the dialog box, and initialise any default values SetDlgItemText(hwnd, IDC_TEXT, "This is a string") SetDlgItemInt(hwnd, IDC_NUMBER, 5, FALSE) Case WM_COMMAND Select Case LoWord(wParam) Case IDC_ADD ' When somebody clicks the Add button, first we get the number of ' they entered Dim As BOOL bSuccess Dim As Long nTimes = GetDlgItemInt(hwnd, IDC_NUMBER, @bSuccess, FALSE) If bSuccess Then ' Then we get the string they entered ' First we need to find out how long it is so that we can ' allocate some memory Dim As Long length = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT)) If length > 0 Then ' Now we allocate, and get the string into our buffer Dim As Long i Dim As ZString Ptr buf = GlobalAlloc(GPTR_, length + 1) GetDlgItemText(hwnd, IDC_TEXT, buf, length + 1) ' Now we add the string to the list box however many times ' the user asked us to. For i = 0 To nTimes Dim As Long index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, Cast(LPARAM, buf)) ' Here we are associating the value nTimes with the item ' just for the heck of it, we'll use it to display later. ' Normally you would put some more useful data here, such ' as a pointer. SendDlgItemMessage(hwnd, IDC_LIST, LB_SETITEMDATA, Cast(WPARAM, index), Cast(LPARAM, nTimes)) Next ' Dont' forget to free the memory! GlobalFree(Cast(HANDLE, buf)) Else MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK) EndIf Else MessageBox(hwnd, "Couldn't translate that number :(", "Warning", MB_OK) EndIf Case IDC_REMOVE ' When the user clicks the Remove button, we first get the number ' of selected items Dim As HWND hList = GetDlgItem(hwnd, IDC_LIST) Dim As Long count = SendMessage(hList, LB_GETSELCOUNT, 0, 0) If count <> LB_ERR Then If count <> 0 Then ' And then allocate room to store the list of selected items. Dim As Long i Dim As Long Ptr buf = GlobalAlloc(GPTR_, SizeOf(Long) * count) SendMessage(hList, LB_GETSELITEMS, Cast(WPARAM, count), Cast(LPARAM, buf)) ' Now we loop through the list and remove each item that was ' selected. ' WARNING!!! ' We loop backwards, because if we removed items ' from top to bottom, it would change the indexes of the other ' items!!! For i = count - 1 To 0 Step -1 SendMessage(hList, LB_DELETESTRING, Cast(WPARAM, buf[i]), 0) Next GlobalFree(buf) Else MessageBox(hwnd, "No items selected.", "Warning", MB_OK) EndIf Else MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK) EndIf Case IDC_CLEAR SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0) Case IDC_LIST Select Case HiWord(wParam) Case LBN_SELCHANGE ' Get the number of items selected. Dim As HWND hList = GetDlgItem(hwnd, IDC_LIST) Dim As Long count = SendMessage(hList, LB_GETSELCOUNT, 0, 0) If count <> LB_ERR Then ' We only want to continue if one and only one item is ' selected. If count = 1 Then ' Since we know ahead of time we're only getting one ' index, there's no need to allocate an array. Dim As Long index Dim As Long err_ = SendMessage(hList, LB_GETSELITEMS, 1, Cast(LPARAM, @index)) If err_ <> LB_ERR Then ' Get the data we associated with the item above ' (the number of times it was added) Dim As Long data_ = SendMessage(hList, LB_GETITEMDATA, Cast(WPARAM, index), 0) SetDlgItemInt(hwnd, IDC_SHOWCOUNT, data_, FALSE) Else MessageBox(hwnd, "Error getting selected item :(", "Warning", MB_OK) EndIf Else ' No items selected, or more than one ' Either way, we aren't going to process this. SetDlgItemText(hwnd, IDC_SHOWCOUNT, "-") EndIf Else MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK) EndIf End Select End Select Case WM_CLOSE EndDialog(hwnd, 0) Case Else Return FALSE End Select Return TRUE End Function Function WinMain(hInstance As HINSTANCE, hPrevInstance As HINSTANCE, lpCmdLine As LPSTR, nCmdShow As Long) As Long Return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, @DlgProc) End Function ' Program start hInstance = GetModuleHandle(NULL) lpCmdLine = GetCommandLine WinMain(hInstance, NULL, lpCmdLine, SW_SHOWDEFAULT) ExitProcess(0)