Win32 ListView控件基本使用
程序员文章站
2024-03-01 11:55:40
...
首先看一下Windows公共控件库,参阅;
https://blog.csdn.net/bcbobo21cn/article/details/106287676
https://blog.csdn.net/bcbobo21cn/article/details/106289420
这几个公共控件库的控件,基本使用原理是一样的;
首先创建,添加列头;完成之后如下;
设置各部位颜色,使用SendMessage实现;
插入具体项目信息;
代码;
#include <windows.h>
#include <commctrl.h>
#include "resource.h"
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
TCHAR szClassName[] = TEXT("listviewDemo");
HWND hListview;
struct STUDENTINFO
{
char name[15];
int age;
char dept[20];
char job[20];
};
int WINAPI
WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
hInst = hThisInstance;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = MAKEINTRESOURCE (IDC_LISTVIEWDEMO);
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
TEXT("listviewDemo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
300,
500,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rt;
int ret;
int i;
struct STUDENTINFO stu[6] = {
{ "无忌", "20", "技术部", "工程师" },
{ "三丰", "80", "总经理", "总经理" },
{ "远桥", "40", "技术部", "经理" },
{ "敏敏", "18", "客服部", "经理" },
{ "芷若", "18", "行政部", "经理" },
{ "小昭", "16", "行政部", "前台" }
};
switch (message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_ABOUT:
MessageBox (hwnd, TEXT ("listviewDemo v1.0\nCopyright (C) 2020\n by bo"),
TEXT ("listviewDemo"), MB_OK | MB_ICONINFORMATION);
break;
case IDM_EXIT:
DestroyWindow(hwnd);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
break;
case WM_CREATE:
hListview = CreateWindowEx(NULL, TEXT("SysListView32"), NULL, LVS_REPORT | WS_CHILD | WS_VISIBLE, 0, 0,
300, 400, hwnd, NULL, hInst, NULL);
LV_COLUMN lvc;
lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.pszText = "姓名";
lvc.cx = 80;
SendMessage(hListview, LVM_INSERTCOLUMN, 0, (long)&lvc);
lvc.pszText = "年龄";
lvc.cx = 80;
SendMessage(hListview, LVM_INSERTCOLUMN, 1, (long)&lvc);
lvc.mask = LVCF_TEXT | LVCF_WIDTH;
lvc.pszText = "部门";
lvc.cx = 80;
SendMessage(hListview, LVM_INSERTCOLUMN, 2, (long)&lvc);
lvc.pszText = "职务";
lvc.cx = 80;
SendMessage(hListview, LVM_INSERTCOLUMN, 3, (long)&lvc);
SendMessage(hListview, LVM_SETTEXTCOLOR, 0, RGB(255, 255, 255));
SendMessage(hListview, LVM_SETBKCOLOR, 0, RGB(135, 160, 135));
SendMessage(hListview, LVM_SETTEXTBKCOLOR, 0, RGB(60, 100, 130));
LVITEM vitem;
vitem.mask = LVIF_TEXT;
for (i = 0; i < 6; i++)
{
//先添加项再设置子项内容
vitem.pszText = stu[i].name;
vitem.iItem = i;
vitem.iSubItem = 0;
ListView_InsertItem(hListview, &vitem);
// 设置子项
vitem.iSubItem = 1;
vitem.pszText = stu[i].age;
ListView_SetItem( hListview, &vitem);
vitem.iSubItem = 2;
vitem.pszText = stu[i].dept;
ListView_SetItem(hListview, &vitem);
vitem.iSubItem = 3;
vitem.pszText = stu[i].job;
ListView_SetItem(hListview, &vitem);
}
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rt);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
先用CreateWindowEx创建控件,类名为"SysListView32";创建风格这些枚举值可以网上查;
其他操作都是通过定义相应结构体对象并赋值,然后用SendMessage实现;
ListView_InsertItem,ListView_SetItem,这两个是预定义宏,它们也是调用SendMessage,传递结构体地址;
添加列头用结构体LV_COLUMN,添加具体信息用结构体LVITEM;
工程;
资源和头文件;
#include "resource.h"
#include <windows.h>
/////////////////////////////////////////////////////////////////////////////
//
// Menu
//
IDC_LISTVIEWDEMO MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", IDM_EXIT
END
POPUP "&Help"
BEGIN
MENUITEM "&About ...", IDM_ABOUT
END
END
#define IDM_EXIT 10001
#define IDM_ABOUT 10002
#define IDC_LISTVIEWDEMO 10101
#define IDD_ABOUTBOX 10102
结构体第二个成员,int age,赋值时赋值为 "20",这么也构建出了exe;可以自己改;
推荐阅读
-
Win32 ListView控件基本使用
-
Android App开发中RecyclerView控件的基本使用教程
-
Android控件之使用ListView实现时间轴效果
-
Android App开发中RecyclerView控件的基本使用教程
-
实例讲解Android app开发中ListView的基本使用及优化
-
ASP.Net基本控件之TextBox控件使用介绍
-
android基本控件ToggleButton&Switch使用指南
-
android基本控件ToggleButton&Switch使用指南
-
iOS应用开发中视图控件UIWindow的基本使用教程
-
Android ListView控件使用方法