Gitee代码链接(有100多MB)
新增文件为:AddListView.cpp
创建ListView
CreateWindow(WC_LISTVIEW, _T("这是ListView"), WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT|LVS_SHOWSELALWAYS, 180, 50, 500, 200, hWnd, (HMENU)IDC_LIST_1, NULL, NULL);
插入 第0行第0列
// 给ListView添加 第0行第0列
LVCOLUMN colInfo = {0};
colInfo.mask |= LVCF_WIDTH | LVCF_TEXT;
colInfo.cx = 100;
colInfo.pszText = (LPWSTR)_T("第0列");
// 发送消息插入 第0行第0列
SendMessage(hListCtrl, LVM_INSERTCOLUMN, 0, (LPARAM)&colInfo);
插入 第0行第1列
// 给ListView添加 第0行第1列
LVCOLUMN colInfo_1 = { 0 };
colInfo_1.mask |= LVCF_WIDTH | LVCF_TEXT;
colInfo_1.cx = 100;
colInfo_1.pszText = (LPWSTR)_T("第1列");
// colInfo_1.fmt = LVCFMT_CENTER; 设置文字居中无效
// 发送消息插入 第0行第1列
SendMessage(hListCtrl, LVM_INSERTCOLUMN, 1, (LPARAM)&colInfo_1);
使用 ListView_InsertColumn 代替 SendMessage
// 给ListView添加 第0行第2列
LVCOLUMN colInfo_2 = { 0 };
colInfo_2.mask |= LVCF_WIDTH | LVCF_TEXT;
colInfo_2.cx = 100;
colInfo_2.pszText = (LPWSTR)_T("第2列");
// 可以使用 ListView_InsertColumn 代替 SendMessage
ListView_InsertColumn(hListCtrl, 2, &colInfo_2);
给ListView添加 第1行第0列
// 给ListView添加 第1行第0列的Item
LV_ITEM row_item = {0};
row_item.mask |= LVIF_TEXT;
row_item.pszText = (LPWSTR)_T("aaaaaaaaaa");
// 第0列的 Item 必须使用 ListView_InsertItem 设置,不能使用 ListView_SetItem
ListView_InsertItem(hListCtrl, &row_item);
和上一节程序 Windows 添加Button的响应事件 的变化是,在 case WM_CREATE: 增加下面的代码,并且在函数外面定义了 UINT IDC_LIST_1 = 300;
case WM_CREATE:
{
// 创建ListView WS_BORDER -- 显示边框
HWND hListCtrl = CreateWindow(WC_LISTVIEW, _T("这是ListView"), WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT|LVS_SHOWSELALWAYS, 180, 50, 500, 200, hWnd, (HMENU)IDC_LIST_1, NULL, NULL);
// 给ListView添加 第0行第0列
LVCOLUMN colInfo = {0};
colInfo.mask |= LVCF_WIDTH | LVCF_TEXT;
colInfo.cx = 100;
colInfo.pszText = (LPWSTR)_T("第0列");
// 发送消息插入 第0行第0列
SendMessage(hListCtrl, LVM_INSERTCOLUMN, 0, (LPARAM)&colInfo);
// 给ListView添加 第0行第1列
LVCOLUMN colInfo_1 = { 0 };
colInfo_1.mask |= LVCF_WIDTH | LVCF_TEXT;
colInfo_1.cx = 100;
colInfo_1.pszText = (LPWSTR)_T("第1列");
// colInfo_1.fmt = LVCFMT_CENTER; 设置文字居中无效
// 发送消息插入 第0行第1列
SendMessage(hListCtrl, LVM_INSERTCOLUMN, 1, (LPARAM)&colInfo_1);
// 给ListView添加 第0行第2列
LVCOLUMN colInfo_2 = { 0 };
colInfo_2.mask |= LVCF_WIDTH | LVCF_TEXT;
colInfo_2.cx = 100;
colInfo_2.pszText = (LPWSTR)_T("第2列");
// 可以使用 ListView_InsertColumn 代替 SendMessage
ListView_InsertColumn(hListCtrl, 2, &colInfo_2);
// 给ListView添加 第0行第2列
LVCOLUMN colInfo_3 = { 0 };
colInfo_3.mask |= LVCF_WIDTH | LVCF_TEXT;
colInfo_3.cx = 100;
colInfo_3.pszText = (LPWSTR)_T("第3列");
// 可以使用 ListView_InsertColumn 代替 SendMessage
ListView_InsertColumn(hListCtrl, 3, &colInfo_3);
// 给ListView添加 第1行第0列的Item
LV_ITEM row_item = {0};
row_item.mask |= LVIF_TEXT;
row_item.pszText = (LPWSTR)_T("aaaaaaaaaa");
// 第0列的 Item 必须使用 ListView_InsertItem 设置,不能使用 ListView_SetItem
ListView_InsertItem(hListCtrl, &row_item);
// 给ListView添加 第1行第1列的Item
ZeroMemory(&row_item, sizeof(row_item));
row_item.mask |= LVIF_TEXT;
row_item.iItem = 0;
row_item.iSubItem = 1;
row_item.pszText = (LPWSTR)_T("bbbbbbbbb");
ListView_SetItem(hListCtrl, &row_item);
// 给ListView添加 第1行第2列的Item
ZeroMemory(&row_item, sizeof(row_item));
row_item.mask |= LVIF_TEXT;
row_item.iItem = 0;
row_item.iSubItem = 2;
row_item.pszText = (LPWSTR)_T("ccccccccc");
ListView_SetItem(hListCtrl, &row_item);
// 给ListView添加 第1行第3列的Item
// 可以直接使用 ListView_SetItemText 插入一列
ListView_SetItemText(hListCtrl, 0, 3, (LPWSTR)_T("dddddddd"));
// 添加 第2行第0列
ZeroMemory(&row_item, sizeof(row_item));
row_item.mask |= LVIF_TEXT;
row_item.iItem = 1;
row_item.pszText = (LPWSTR)_T("eeeeeeeeeeee");
ListView_InsertItem(hListCtrl, &row_item);
// 添加 第2行第1列
ZeroMemory(&row_item, sizeof(row_item));
row_item.mask |= LVIF_TEXT;
row_item.iItem = 1;
row_item.iSubItem = 1;
row_item.pszText = (LPWSTR)_T("fffffffff");
ListView_SetItem(hListCtrl, &row_item);
// 给ListView添加 第2行第2列的Item
// 可以直接使用 ListView_SetItemText 插入一列
ListView_SetItemText(hListCtrl, 1, 2, (LPWSTR)_T("gggggggg"));
// 给ListView添加 第2行第3列的Item
// 可以直接使用 ListView_SetItemText 插入一列
ListView_SetItemText(hListCtrl, 1, 3, (LPWSTR)_T("hhhhhhhh"));
}