Cara Menambahkan Kolom List-View
Topik ini menunjukkan cara menambahkan kolom ke kontrol tampilan daftar. Kolom digunakan untuk menampilkan item dan subitem saat kontrol tampilan daftar berada dalam tampilan laporan (detail). Teks dari kolom yang dipilih juga dapat ditampilkan dalam tampilan petak peta.
Apa yang perlu Anda ketahui
Teknologi
Prasyarat
- C/C++
- Pemrograman Antarmuka Pengguna Windows
Peraturan
Untuk menambahkan kolom ke kontrol tampilan daftar, kirim pesan LVM_INSERTCOLUMN atau gunakan makro ListView_InsertColumn. Untuk menghapus kolom, gunakan pesan LVM_DELETECOLUMN.
Contoh kode C++ berikut memanggil makro ListView_InsertColumn untuk menambahkan kolom ke kontrol tampilan daftar. Judul kolom didefinisikan dalam file header aplikasi sebagai sumber daya string, yang diberi nomor berturut-turut dimulai dengan IDS_FIRSTCOLUMN. Jumlah kolom didefinisikan dalam file header sebagai C_COLUMNS.
// InitListViewColumns: Adds columns to a list-view control.
// hWndListView: Handle to the list-view control.
// Returns TRUE if successful, and FALSE otherwise.
BOOL InitListViewColumns(HWND hWndListView)
{
WCHAR szText[256]; // Temporary buffer.
LVCOLUMN lvc;
int iCol;
// Initialize the LVCOLUMN structure.
// The mask specifies that the format, width, text,
// and subitem members of the structure are valid.
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
// Add the columns.
for (iCol = 0; iCol < C_COLUMNS; iCol++)
{
lvc.iSubItem = iCol;
lvc.pszText = szText;
lvc.cx = 100; // Width of column in pixels.
if ( iCol < 2 )
lvc.fmt = LVCFMT_LEFT; // Left-aligned column.
else
lvc.fmt = LVCFMT_RIGHT; // Right-aligned column.
// Load the names of the column headings from the string resources.
LoadString(g_hInst,
IDS_FIRSTCOLUMN + iCol,
szText,
sizeof(szText)/sizeof(szText[0]));
// Insert the columns into the list view.
if (ListView_InsertColumn(hWndListView, iCol, &lvc) == -1)
return FALSE;
}
return TRUE;
}
Topik terkait