#include <Windows.h>
#include <CommCtrl.h>
#include <windowsx.h>
#include "resource.h"
#pragma comment(lib, "Comctl32.lib")
// 启用视觉样式
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
// 用于测试的结构体
struct STUDENTINFO
{
WCHAR Name[15];
WCHAR Age[3];
WCHAR Address[50];
};
HINSTANCE hgAppInst; //全局变量,实例句柄
HIMAGELIST hImgListSm; //ImageList的句柄
HIMAGELIST hImgListLg;
int WINAPI wWinMain(HINSTANCE hTheApp,
HINSTANCE hPrevApp,
LPWSTR lpCmd,
int nShow)
{
hgAppInst = hTheApp;
// 创建对话框作为主窗口
HWND hdlg = CreateDialog(hTheApp, MAKEINTRESOURCE(IDD_MY), GetDesktopWindow(), (DLGPROC)DlgProc);
if (!hdlg)
{
return 0;
}
// 显示对话框
ShowWindow(hdlg, nShow);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND hListview = NULL;//ListView的句柄
HICON hicon;
HWND hcbb = NULL;//Combobox的句柄
switch (msg)
{
case WM_INITDIALOG:
{
// 获取ListView控件的句柄
hListview = GetDlgItem(hDlg, IDC_LV);
// 设置ListView的列
LVCOLUMN vcl;
vcl.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
// 第一列
vcl.pszText = L"姓名";//列标题
vcl.cx = 90;//列宽
vcl.iSubItem = 0;//子项索引,第一列无子项
ListView_InsertColumn(hListview, 0, &vcl);
// 第二列
vcl.pszText = L"年龄";
vcl.cx = 90;
vcl.iSubItem = 1;//子项索引
ListView_InsertColumn(hListview, 1, &vcl);
// 第三列
vcl.pszText = L"地址";
vcl.cx = 200;
vcl.iSubItem = 2;
ListView_InsertColumn(hListview, 2, &vcl);
// 初始化ImageList
hImgListSm = ImageList_Create(GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON),ILC_MASK,1,0);
hImgListLg = ImageList_Create(GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),ILC_MASK,1,0);
hicon = LoadIcon(hgAppInst,MAKEINTRESOURCE(IDI_ITE));
// 添加图标
ImageList_AddIcon(hImgListSm, hicon);
ImageList_AddIcon(hImgListLg, hicon);
DestroyIcon(hicon);
// 将ListView与ImageList关联
ListView_SetImageList(hListview, hImgListLg, LVSIL_NORMAL);
ListView_SetImageList(hListview, hImgListSm, LVSIL_SMALL);
// 初始化ComboBox,以选择视图
hcbb = GetDlgItem(hDlg, IDC_CBVIEW);
ComboBox_AddString(hcbb, L"大图标");
ComboBox_AddString(hcbb, L"小图标");
ComboBox_AddString(hcbb, L"列表");
ComboBox_AddString(hcbb, L"详细");
}
return 0;
case WM_SYSCOMMAND:
if (wParam == SC_CLOSE)
{
// 销毁ImageList
ImageList_Destroy(hImgListLg);
ImageList_Destroy(hImgListSm);
DestroyWindow(hListview);//不再需要
DestroyWindow(hDlg);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_COMMAND:
{
// GetDlgItem可以通过ID号取出对话框中的控件的句柄
hListview = GetDlgItem(hDlg, IDC_LV);
switch (LOWORD(wParam))
{
case IDC_BTNADD:
{
STUDENTINFO stu[ ] = {
{ L"小刘", L"20", L"火星" },
{ L"老赵", L"21", L"木星" },
{ L"小胡", L"30", L"水星" },
{ L"老高", L"32", L"山沟一号" },
{ L"黄牛", L"24", L"不知哪个星球来的" },
{ L"王七", L"28", L"超人之乡" }
};
//求出数组中元素的个数
int arrCount = (int)(sizeof(stu) / sizeof(stu[0]));
LVITEM vitem;
vitem.mask = LVIF_TEXT | LVIF_IMAGE;
vitem.iImage = 0;
for (int i = 0; i < arrCount; 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].Address;
ListView_SetItem(hListview, &vitem);
}
}
break;
case IDC_BTNCLEAR:
// 清除ListView中的所有项
ListView_DeleteAllItems(hListview);
break;
case IDC_CBVIEW:
if (HIWORD(wParam) == CBN_SELCHANGE)
{
// 当前选择项的索引
int index = SendMessage((HWND)lParam, CB_GETCURSEL, 0, 0);
// 根据选择设置视图
DWORD lvView;
switch (index)
{
case 0:
lvView = LV_VIEW_ICON;
break;
case 1:
lvView = LV_VIEW_SMALLICON;
break;
case 2:
lvView = LV_VIEW_LIST;
break;
case 3:
lvView = LV_VIEW_DETAILS;
break;
default:
lvView = LV_VIEW_DETAILS;
break;
}
ListView_SetView(GetDlgItem(hDlg, IDC_LV), lvView);
}
break;
}
}
return 0;
}
return (INT_PTR)FALSE;
}