Code:
case IDC_REMOVE:
{
// When the user clicks the Remove button, we first get the number
// of selected items
HWND hList = GetDlgItem(hwnd, IDC_LIST);
int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
if(count != LB_ERR)
{
if(count != 0)
{
// And then allocate room to store the list of selected items.
int i;
int *buf = GlobalAlloc(GPTR, sizeof(int) * count);
SendMessage(hList, LB_GETSELITEMS, (WPARAM)count, (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!!!
if (count == 1)
SetDlgItemText(hwnd, IDC_STATIC, msg1);
else if (count > 1)
SetDlgItemText(hwnd, IDC_STATIC, msg2);
else
MessageBox(NULL, "Error 1000101;\n\nPlease report this error.",
"Error!", MB_OK | MB_ICONEXCLAMATION);
for(i = count - 1; i >= 0; i--)
{
SendMessage(hList, LB_DELETESTRING, (WPARAM)buf[i], 0);
}
GlobalFree(buf);
}
else
{
MessageBox(hwnd, "No items selected.", "Warning", MB_OK);
}
}
else
{
MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
}
}
break;
case IDC_CLEAR:
{
// When the user clicks the Clear button, we first select all of the
// items and then go ahead just as we did previously with the Remove
// button to delete all of the items in the select box.
HWND hList = GetDlgItem(hwnd, IDC_LIST);
int count = 0;
SendMessage(hList, LB_SETSEL, (WPARAM)TRUE, (LPARAM)-1);
count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
if(count != LB_ERR)
{
if(count != 0)
{
// And then allocate room to store the list of selected items.
int i;
int *buf = GlobalAlloc(GPTR, sizeof(int) * count);
SendMessage(hList, LB_GETSELITEMS, (WPARAM)count, (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!!!
if (count == 1)
SetDlgItemText(hwnd, IDC_STATIC, msg1);
else if (count > 1)
SetDlgItemText(hwnd, IDC_STATIC, msg2);
else
MessageBox(NULL, "Error 1000101;\n\nPlease report this error.",
"Error!", MB_OK | MB_ICONEXCLAMATION);
for(i = count - 1; i >= 0; i--)
{
SendMessage(hList, LB_DELETESTRING, (WPARAM)buf[i], 0);
}
GlobalFree(buf);
}
else
{
MessageBox(hwnd, "There are no items to be removed.", "Warning", MB_OK);
}
}
else
{
MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
}
}
break;
thanks