Bagikan melalui


Cara Membuat Kotak Daftar Pilihan Ganda

Topik ini menunjukkan cara menampilkan dan mengakses konten direktori dalam kotak daftar beberapa pilihan. Dalam kotak daftar beberapa pilihan, pengguna dapat memilih lebih dari satu item pada satu waktu.

Contoh kode C++ dalam topik ini memungkinkan pengguna untuk melihat daftar file di direktori saat ini, memilih grup file dari daftar, dan menghapusnya.

Apa yang perlu Anda ketahui

Teknologi

Prasyarat

  • C/C++
  • Pemrograman Antarmuka Pengguna Windows

Petunjuk

Aplikasi daftar direktori harus melakukan tugas terkait kotak daftar berikut:

  • Menginisialisasi kotak daftar.
  • Ambil pilihan pengguna dari kotak daftar.
  • Hapus nama file dari kotak daftar setelah file yang dipilih dihapus.

Dalam contoh kode C++ berikut, prosedur kotak dialog menginisialisasi kotak daftar beberapa pilihan (IDC_FILELIST) dengan menggunakan fungsi DlgDirList untuk mengisi kotak daftar dengan nama semua file di direktori saat ini.

Saat pengguna memilih grup file dan memilih tombol Hapus, prosedur kotak dialog mengirim pesan LB_GETSELCOUNT, untuk mengambil jumlah file yang dipilih, dan pesan LB_GETSELITEMS, untuk mengambil array item kotak daftar yang dipilih. Setelah menghapus file, prosedur dialog menghapus item terkait dari kotak daftar dengan mengirim pesan LB_DELETESTRING.

#define BIGBUFF 8192 
 
INT_PTR CALLBACK DlgDelFilesProc(HWND hDlg, UINT message, 
        UINT wParam, LONG lParam) 
{ 
    PTSTR pszCurDir; 
    PTSTR pszFileToDelete; 
    int cSelItems; 
    int cSelItemsInBuffer; 
    TCHAR achBuffer[MAX_PATH]; 
    int aSelItems[BIGBUFF]; 
    int i; 
    BOOL fResult; 
    HWND hListBox;
    int iRet;
 
    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 list of selected files. 
                    pszFileToDelete = achBuffer; 
                    hListBox = GetDlgItem(hDlg, IDC_FILELIST); 
                    cSelItems = SendMessage(hListBox, 
                            LB_GETSELCOUNT, 0, 0); 
 
                    cSelItemsInBuffer = SendMessage(hListBox, 
                            LB_GETSELITEMS, 512, (LPARAM) aSelItems); 
 
                    if (cSelItems > cSelItemsInBuffer) 
                    { 
                        MessageBox(hDlg, L"Too many items selected.", 
                                NULL, MB_OK); 
                    } 
                    else 
                    { 

                        // Make sure the user really wants to delete the files.
                        iRet = MessageBox(hDlg, 
                            L"Are you sure you want to delete these files?", 
                            L"Deleting Files", MB_YESNO | MB_ICONEXCLAMATION);
                        if (iRet == IDNO)
                            return TRUE;

                        // Go through the list backward because after deleting
                        // an item the indices change for every subsequent 
                        // item. By going backward, the indices are never 
                        // invalidated. 
                        for (i = cSelItemsInBuffer - 1; i >= 0; i--) 
                        { 
                            SendMessage(hListBox, LB_GETTEXT, 
                                        aSelItems[i], 
                                        (LPARAM) pszFileToDelete); 
 
                            fResult = DeleteFile(pszFileToDelete); 
                            if (!fResult) 
                            {                     
                                MessageBox(hDlg, L"Could not delete file.", 
                                    NULL, MB_OK);     
                            } 
                            else 
                            { 
                                SendMessage(hListBox, LB_DELETESTRING, 
                                        aSelItems[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; 
    } 
} 

Referensi Kontrol Kotak Daftar

Tentang Kotak Daftar

Menggunakan Kotak Daftar