ตัวอย่างต่อไปนี้แสดง และเริ่มต้นกล่องโต้ตอบที่ใช้ในตัวอย่างก่อนหน้านี้ อย่างไรก็ตาม รหัสนี้ใช้สไตล์ LBS_MULTIPLESEL เพื่อให้ผู้ใช้เลือกแฟ้มมากกว่าหนึ่งแฟ้มแต่ละครั้ง เมื่อผู้ใช้เลือกปุ่มลบตัวอย่างการส่งข้อความLB_GETSELCOUNT (การดึงข้อมูลหมายเลขของแฟ้มที่เลือกไว้) และความต้องการของLB_GETSELITEMS (เพื่อดึงอาร์เรย์ของรายการของกล่องรายการที่เลือก) หลังจากลบแฟ้ม รหัสเอาสินค้าที่สอดคล้องกันจากกล่องรายการ โดยการส่งข้อความLB_DELETESTRING.
#define BUFFER MAX_PATH
#define BIGBUFF 8192
BOOL APIENTRY DlgDelFilesProc(
HWND hDlg, // window handle to dialog box
UINT message, // type of message
UINT wParam, // message-specific information
LONG lParam)
{
DWORD cchCurDir;
LPTSTR lpszCurDir;
LPTSTR lpszFileToDelete;
int nSelItems;
int nSelItemsInBuffer;
TCHAR tchBuffer[BUFFER];
TCHAR tchMsgBuff[BUFFER];
int nBuffer[BIGBUFF];
int i;
BOOL fResult;
HWND hListBox;
switch (message) {
case WM_INITDIALOG:
// Initialize the list box by filling it with files from
// the current directory.
lpszCurDir = tchBuffer;
GetCurrentDirectory(cchCurDir, lpszCurDir);
DlgDirList(hDlg, lpszCurDir, IDL_FILES, IDS_PATHTOFILL, 0);
SetFocus(GetDlgItem(hDlg, IDL_FILES));
return FALSE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
// When the user presses the Delete (IDOK)
// button, delete all the selected files.
lpszFileToDelete = tchBuffer;
hListBox = GetDlgItem(hDlg, IDL_FILES);
nSelItems = SendMessage(hListBox,
LB_GETSELCOUNT, 0, 0);
nSelItemsInBuffer = SendMessage(hListBox,
LB_GETSELITEMS, 512, (LPARAM) nBuffer);
if (nSelItems > nSelItemsInBuffer)
{
MessageBox(hDlg, "Too many items selected.",
NULL, MB_OK);
}
else
{
// Go through the list backwards because after
// deleting an item the indices change for
// every subsequent item. By going backward,
// the indices are never invalidated.
for (i = nSelItemsInBuffer - 1; i >= 0; i--)
{
SendMessage(hListBox, LB_GETTEXT,
nBuffer[i],
(LPARAM) lpszFileToDelete);
fResult = DeleteFile(lpszFileToDelete);
if (!fResult)
{
sprintf(tchMsgBuff,
"Could not delete file: %s "
"GetLastError = %u",
(LPARAM) lpszFileToDelete);
// Call app-defined error handler.
ErrorHandler(tchMsgBuff);
}
else
{
SendMessage(hListBox, LB_DELETESTRING,
nBuffer[i], 0);
}
}
SendMessage(hListBox, LB_SETCARETINDEX, 0, 0);
}
return TRUE;
case IDCANCEL:
// Destroy the dialog box.
EndDialog(hDlg, TRUE);
return TRUE;
default:
return FALSE;
}
default:
return FALSE;
}
}