다음 예제에서는 표시 하 고 앞의 예제에서 사용 되는 대화 상자를 초기화 합니다. 그러나,이 코드 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;
}
}