使用列表控制的步骤如下:
- 调用CreateWindowEx函数来创建一个列表控件,指定它的类名为SysListView32。您还可以在此处指定控件初次显示时的方式。
- 创建和初始化用在列表控件中显示项目的图象列表(如果存在)。
- 向列表控件中插入列,如果显示的方式是报告方式这一步是必须的。
- 向控件中插入项目和自项目。
所用到的两种数据结构:
列:
LV_COLUMN STRUCT
imask dd ?
fmt dd ?
lx dd ?
pszText dd ?
cchTextMax dd ?
iSubItem dd ?
iImage dd ?
iOrder dd ?
LV_COLUMN ENDS
Field name | Meanings |
---|---|
imask | 一组标志位标明该结构体中那些成员变量中的值有效。它的意义和上面我们提到的LV_COLUMN型结构体中向对应的成员变量基本相同。更详细的信息,可以查询WIN32 API 手册。 |
iItem | 该结构体代表的项目的索引号。索引号是从0开始编号的。该值和表单的“行”类似。 |
iSubItem | 和上一个成员变量指定的项目相连的子项目的索引号。您可以把它当作表单的“列”。譬如您想要把一个项目插入到新创建的列表视图控件,iItem的 值应为0(因为该项目是第一个项目),iSubItem的值也应当为0(我们想把该项目插到第一列)。如果你想指定一个子项目和该项目相连,iItem中 应该是您想要相连的项目的索引号,iSubItem的值应当是大于0的值,具体的值取决于您想把该子项目插在那一列。如果你的列表视图控件一共有4列的 化,第一列包含了项目,其余3列是留给子项目的。如果您想把子项目插在第四列,应当指定该值为3。 |
state | 该成员变量包含的标志位反应了项目的状态。状态的改变可能是由用户的操作引起的或是程序改变的。这些状态包括:是否有焦点/高亮度显示/被选中(由于被剪切)/被选中等。另外还包括,以1为基数的索引用来代表是否处使用重叠/状态图标。 |
stateMask | 由于上面的成员变量包含状态标志位、重叠的位图索引号、和状态位图的索引号,我们需要告诉WINDOWS我们到底需要设定或查询那一个值。该成员变量就是用来做这项工作的。 |
pszText | 当我们想设定项目的属性时,它包含项目名称的ASCII码的字符串的地址。当查询项目的属性时,该成员变量将用来接收查询返回的项目的名称。 |
cchTextMax | 仅当您用来查询项目的属性时才需要使用该值,这时它包含上一个成员变量的大小。 |
iImage | 图标在列表视图中的图象链表中的索引号。 |
lParam | 用户定义的值,当您给项目排序时使用。当您告诉列表视图对项目排序时,列表视图将成对地比较项目。 它将会把两个项目的lParam的值传给您,这样您就可以进行比较先列出那一个了。如果您现在还不太明白的话,没有系,我们稍后还要讲关于排序的问题。 |
控件通过SendMessage来发送消息来控制,常用的消息有:
LVM_INSERTCOLUMN 加入列,wParam 为整型,指定列号,lParam 为指向LV_COLUMN结构的指针
LVM_SETCOLUMN 设置列,参数同上
LVM_INSERTITEM 加入项目或子项目,wParam 为0,lParam 为指向LV_ITEM结构的指针
LVM_SETITEM 设置项目或子项目,参数同上
LVM_GETITEM 取得项目或子项目,参数同上
LVM_GETNEXTITEM 取得下一个项目或子项目,可以用来取得光标选择的项目
LVM_DELETEITEM 删除项目或子项目,wParam 为整型,指定项目索引号,lParam 为0
LVM_DELETEALLITEMS 删除所有项目,wParam 和 lParam 均为0
LVM_SETTEXTCOLOR 设置文字颜色,wParam 为0,lParam 为颜色的RGB值
LVM_SETTEXTBKCOLOR 设置文字背景色,参数同上
LVM_SETBKCOLOR 设置背景色,参数同上
下面是用SysListView32控件实现的简单的学生管理系统程序的一部分:
代码
void InsertColumn(void)
{
LV_COLUMN lvc;
lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.pszText = "学号";
lvc.cx = 60;
SendMessage(hList, LVM_INSERTCOLUMN, 0, (long)&lvc);
lvc.pszText = "姓名";
lvc.cx = 80;
SendMessage(hList, LVM_INSERTCOLUMN, 1, (long)&lvc);
lvc.pszText = "性别";
lvc.cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 2, (long)&lvc);
lvc.pszText = "年龄";
lvc.cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 3, (long)&lvc);
lvc.pszText = "出生日期";
lvc.cx = 100;
SendMessage(hList, LVM_INSERTCOLUMN, 4, (long)&lvc);
lvc.pszText = "籍贯";
lvc.cx = 150;
SendMessage(hList, LVM_INSERTCOLUMN, 5, (long)&lvc);
lvc.pszText = "入学时间";
lvc.cx = 100;
SendMessage(hList, LVM_INSERTCOLUMN, 6, (long)&lvc);
lvc.pszText = "数学";
lvc.cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 7, (long)&lvc);
lvc.pszText = "英语";
lvc.cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 8, (long)&lvc);
lvc.pszText = "政治";
lvc.cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 9, (long)&lvc);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInst;
HMENU hMenu;
LV_ITEM lvi;
int i;
switch (message) {
case WM_CREATE:
hInst = ((LPCREATESTRUCT)lParam) -> hInstance;
hMenu = GetMenu(hwnd);
InitCommonControls();
hList = CreateWindowEx(NULL, TEXT("SysListView32"), NULL, LVS_REPORT | WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, NULL, hInst, NULL);
InsertColumn();
SendMessage(hList, LVM_SETTEXTCOLOR, 0, #ffffff);
SendMessage(hList, LVM_SETBKCOLOR, 0, #646464);
SendMessage(hList, LVM_SETTEXTBKCOLOR, 0, #000000);
Refresh();
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDREFRESH:
Refresh();
break;
case IDINSERT:
if (DialogBox(hInst, TEXT("StudInfo"), hwnd, InsertDlgProc)) InvalidateRect(hwnd, NULL, TRUE);
break;
case IDEDIT:
lvi.iItem = SendMessage(hList, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
if (-1 == lvi.iItem) {
MessageBox(hwnd, "请选择一条记录!", "Edit Info", MB_OK | MB_ICONWARNING);
break;
}
lvi.mask = LVIF_TEXT;
lvi.cchTextMax = 20;
for (i = 0; i < 10; ++i) {
lvi.iSubItem = i;
lvi.pszText = buf[i];
SendMessage(hList, LVM_GETITEM, 0, (long)&lvi);
}
if (DialogBox(hInst, TEXT("StudInfo"), hwnd, EditDlgProc)) InvalidateRect(hwnd, NULL, TRUE);
break;
case IDDELETE:
lvi.iItem = SendMessage(hList, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
if (-1 == lvi.iItem) {
MessageBox(hwnd, "请选择一条记录!", "Delete Info", MB_OK | MB_ICONWARNING);
break;
}
SendMessage(hList, LVM_DELETEITEM, lvi.iItem, 0);
lvi.mask = LVIF_TEXT;
lvi.iSubItem = 0;
lvi.pszText = buf[0];
lvi.cchTextMax = 20;
SendMessage(hList, LVM_GETITEM, 0, (long)&lvi);
sprintf(query, "DELETE FROM student WHERE id = %s", buf[0]);
ODBCConnect(hwnd);
ODBCQuery(query);
ODBCQueryEnd();
ODBCDisconnect(hwnd);
break;
case IDEXIT:
SendMessage(hwnd, WM_CLOSE, 0, 0);
break;
case IDABOUT:
MessageBox(hwnd, "Students Info Administration System Developed by anonymous", "About", MB_OK | MB_ICONINFORMATION);
}
break;
case WM_SIZE:
MoveWindow(hList, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
{
LV_COLUMN lvc;
lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.pszText = "学号";
lvc.cx = 60;
SendMessage(hList, LVM_INSERTCOLUMN, 0, (long)&lvc);
lvc.pszText = "姓名";
lvc.cx = 80;
SendMessage(hList, LVM_INSERTCOLUMN, 1, (long)&lvc);
lvc.pszText = "性别";
lvc.cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 2, (long)&lvc);
lvc.pszText = "年龄";
lvc.cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 3, (long)&lvc);
lvc.pszText = "出生日期";
lvc.cx = 100;
SendMessage(hList, LVM_INSERTCOLUMN, 4, (long)&lvc);
lvc.pszText = "籍贯";
lvc.cx = 150;
SendMessage(hList, LVM_INSERTCOLUMN, 5, (long)&lvc);
lvc.pszText = "入学时间";
lvc.cx = 100;
SendMessage(hList, LVM_INSERTCOLUMN, 6, (long)&lvc);
lvc.pszText = "数学";
lvc.cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 7, (long)&lvc);
lvc.pszText = "英语";
lvc.cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 8, (long)&lvc);
lvc.pszText = "政治";
lvc.cx = 40;
SendMessage(hList, LVM_INSERTCOLUMN, 9, (long)&lvc);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInst;
HMENU hMenu;
LV_ITEM lvi;
int i;
switch (message) {
case WM_CREATE:
hInst = ((LPCREATESTRUCT)lParam) -> hInstance;
hMenu = GetMenu(hwnd);
InitCommonControls();
hList = CreateWindowEx(NULL, TEXT("SysListView32"), NULL, LVS_REPORT | WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, NULL, hInst, NULL);
InsertColumn();
SendMessage(hList, LVM_SETTEXTCOLOR, 0, #ffffff);
SendMessage(hList, LVM_SETBKCOLOR, 0, #646464);
SendMessage(hList, LVM_SETTEXTBKCOLOR, 0, #000000);
Refresh();
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDREFRESH:
Refresh();
break;
case IDINSERT:
if (DialogBox(hInst, TEXT("StudInfo"), hwnd, InsertDlgProc)) InvalidateRect(hwnd, NULL, TRUE);
break;
case IDEDIT:
lvi.iItem = SendMessage(hList, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
if (-1 == lvi.iItem) {
MessageBox(hwnd, "请选择一条记录!", "Edit Info", MB_OK | MB_ICONWARNING);
break;
}
lvi.mask = LVIF_TEXT;
lvi.cchTextMax = 20;
for (i = 0; i < 10; ++i) {
lvi.iSubItem = i;
lvi.pszText = buf[i];
SendMessage(hList, LVM_GETITEM, 0, (long)&lvi);
}
if (DialogBox(hInst, TEXT("StudInfo"), hwnd, EditDlgProc)) InvalidateRect(hwnd, NULL, TRUE);
break;
case IDDELETE:
lvi.iItem = SendMessage(hList, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
if (-1 == lvi.iItem) {
MessageBox(hwnd, "请选择一条记录!", "Delete Info", MB_OK | MB_ICONWARNING);
break;
}
SendMessage(hList, LVM_DELETEITEM, lvi.iItem, 0);
lvi.mask = LVIF_TEXT;
lvi.iSubItem = 0;
lvi.pszText = buf[0];
lvi.cchTextMax = 20;
SendMessage(hList, LVM_GETITEM, 0, (long)&lvi);
sprintf(query, "DELETE FROM student WHERE id = %s", buf[0]);
ODBCConnect(hwnd);
ODBCQuery(query);
ODBCQueryEnd();
ODBCDisconnect(hwnd);
break;
case IDEXIT:
SendMessage(hwnd, WM_CLOSE, 0, 0);
break;
case IDABOUT:
MessageBox(hwnd, "Students Info Administration System Developed by anonymous", "About", MB_OK | MB_ICONINFORMATION);
}
break;
case WM_SIZE:
MoveWindow(hList, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}