Udostępnij przez


Jak stworzyć listę katalogów w kontrolce ListBox z pojedynczym wyborem

W tym temacie pokazano, jak używać pola listy z jednym wyborem do wyświetlania i uzyskiwania dostępu do zawartości katalogu. Pole listy jednokrotnego wyboru jest domyślnym typem pola listy. Użytkownik może wybrać tylko jeden element w danym momencie w polu listy z jednym wyborem.

Przykładowy kod języka C++ w tym temacie umożliwia użytkownikowi wyświetlenie listy plików w bieżącym katalogu, wybranie pliku z listy i usunięcie go.

Co musisz wiedzieć

Technologie

  • kontrolek systemu Windows

Warunki wstępne

  • C/C++
  • Programowanie interfejsu użytkownika systemu Windows

Instrukcje

Aplikacja do wyświetlania katalogów musi wykonywać następujące zadania dotyczące pola listy:

  • Zainicjuj pole listy wyboru.
  • Pobierz wybór użytkownika z pola listy.
  • Usuń nazwę pliku z pola listy po usunięciu wybranego pliku.

W poniższym przykładzie kodu języka C++ procedura okna dialogowego inicjuje pole listy pojedynczego wyboru (IDC_FILELIST), używając funkcji DlgDirList, aby wypełnić pole listy nazwami wszystkich plików w bieżącym katalogu. Gdy użytkownik wybierze plik i wybierze przycisk Usuń, funkcja DlgDirSelectEx pobiera nazwę wybranego pliku. Kod usuwa plik przy użyciu funkcji DeleteFile i aktualizuje pole listy katalogów, wysyłając komunikat LB_DELETESTRING.

INT_PTR CALLBACK DlgDelFileProc(HWND hDlg, UINT message, 
        UINT wParam, LONG lParam) 
{ 
    PTSTR pszCurDir; 
    PTSTR pszFileToDelete; 
    int iLBItem; 
    int cStringsRemaining; 
    int iRet; 
    TCHAR achBuffer[MAX_PATH]; 
    TCHAR achTemp[MAX_PATH]; 
    BOOL fResult;     
 
    switch (message) 
    { 
        case WM_INITDIALOG: 
 
           // Initialize the list box by filling it with files from 
           // the current directory. 
           pszCurDir = achBuffer; 
           GetCurrentDirectory(MAX_PATH, pszCurDir); 
           DlgDirList(hDlg, pszCurDir, IDC_FILELIST, IDS_PATHTOFILL, 0); 
           SetFocus(GetDlgItem(hDlg, IDC_FILELIST)); 
           return FALSE; 
 
        case WM_COMMAND: 
 
            switch (LOWORD(wParam)) 
            { 
                case IDOK: 
 
                    // When the user presses the DEL (IDOK) button, 
                    // first retrieve the selected file. 
                    pszFileToDelete = achBuffer; 
                    DlgDirSelectEx(hDlg, pszFileToDelete, MAX_PATH, 
                        IDC_FILELIST); 

                    // Make sure the user really wants to delete the file.
                    achTemp[MAX_PATH];
                    StringCbPrintf (achTemp, ARRAYSIZE(achTemp),  
                            TEXT("Are you sure you want to delete %s?"), 
                            pszFileToDelete);
                    iRet = MessageBox(hDlg, achTemp, L"Deleting Files", 
                        MB_YESNO | MB_ICONEXCLAMATION);
                    if (iRet == IDNO)
                        return TRUE;

                    // Delete the file.
                    fResult = DeleteFile(pszFileToDelete); 
                    if (!fResult) 
                    { 
                        MessageBox(hDlg, L"Could not delete file.", 
                            NULL, MB_OK); 
                    } 
                    else // Remove the filename from the list box.
                    { 
                        // Get the selected item.
                        iLBItem = SendMessage(GetDlgItem(hDlg, IDC_FILELIST), 
                            LB_GETCURSEL, 0, 0); 
 
                        // Delete the selected item.
                        cStringsRemaining = SendMessage(GetDlgItem(hDlg, IDC_FILELIST), 
                            LB_DELETESTRING, iLBItem, 0); 
 
                        // If this is not the last item, set the selection to 
                        // the item immediately following the one just deleted.
                        // Otherwise, set the selection to the last item.
                         if (cStringsRemaining > iLBItem) 
                        { 
                            SendMessage(GetDlgItem(hDlg, IDC_FILELIST), 
                                LB_SETCURSEL, iLBItem, 0); 
                        } 
                        else 
                        { 
                            SendMessage(GetDlgItem(hDlg, IDC_FILELIST), 
                                LB_SETCURSEL, cStringsRemaining, 0); 
                        } 
                    } 
                    return TRUE; 
 
                case IDCANCEL: 
 
                    // Destroy the dialog box. 
                     EndDialog(hDlg, TRUE); 
                    return TRUE; 
 
                default: 
                    return FALSE; 
            } 
 
        default: 
            return FALSE; 
    } 
} 

Dokumentacja Kontrolki Pola Listy

O polach listy

używanie list boxów