- #include <stdio.h>
- #include <windows.h>
- #include <CommCtrl.h>
- #include "resource.h"
- #pragma comment(lib,"comctl32.lib")
- HINSTANCE g_hInst;
- HWND g_hMainWnd;
- HWND hListView;
- void InsertListviewItem(int nPos, WCHAR * wszStr,WCHAR * wszSecond, WCHAR * wszThrid);
- void CreateListView();
- INT_PTR CALLBACK DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_INITDIALOG:
- g_hMainWnd = hWnd;
- CreateListView();
- break;
- case WM_COMMAND:
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_CLOSE:
- PostQuitMessage(0);
- break;
- default:
- return FALSE;
- }
- return TRUE;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
- {
- g_hInst = hInstance;
- g_hMainWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_CLIENT_DIALOG), GetDesktopWindow(), DlgProc);
- if (g_hMainWnd == 0)
- {
- return -1;
- }
- RECT wndRect;
- GetWindowRect(g_hMainWnd, &wndRect);
- int nScreenX = GetSystemMetrics(SM_CXSCREEN);
- int nScreeny = GetSystemMetrics(SM_CYSCREEN);
- SetWindowPos(g_hMainWnd, HWND_TOP, nScreenX / 2 - (wndRect.right - wndRect.left) / 2,
- nScreeny / 2 - (wndRect.bottom - wndRect.top) / 2, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
- void CreateListView()
- {
- hListView = GetDlgItem(g_hMainWnd, IDC_LISTView);
- LONG lStyle = GetWindowLong(hListView, GWL_STYLE);//获取当前窗口style
- lStyle &= ~LVS_TYPEMASK;
- lStyle |= LV_VIEW_DETAILS; //这个样式可以有好几种,LV_VIEW_DETAILS表示详细,LV_VIEW_ICON表示大图标,LV_VIEW_SMALLICON表示小图标,LV_VIEW_LIST表示列表,LV_VIEW_TILE表示平铺
- ListView_SetExtendedListViewStyle(hListView, LVS_EX_FULLROWSELECT); //增加获取当行样式
- SetWindowLong(hListView, GWL_STYLE, lStyle);//设置style
- HMODULE hShell = LoadLibraryA("shell32.dll"); //windows的系统图标全部放在了shell32.dll这个动态链接库里,我们可以加载这个dll来获取图标
- HICON hIcon = LoadIcon(hShell, MAKEINTRESOURCE(17)); //17表示的是打印机的图标,至于其他图标的号码上网搜索一下就有了
- HIMAGELIST hImageList = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 0);
- ImageList_AddIcon(hImageList, hIcon);
- ListView_SetImageList(hListView, hImageList, LVSIL_SMALL); //当lStyle为LV_VIEW_SAMLLICON和LV_VIEW_TILE时,第三个参数应为LVSIL_NORMAL
- LVCOLUMN col;
- col.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
- col.cx = 30;
- col.iSubItem = 0;
- col.pszText = L"ID";
- ListView_InsertColumn(hListView, 0, &col);
- col.cx = 100;
- col.iSubItem = 0;
- col.pszText = L"SecTest";
- ListView_InsertColumn(hListView, 1, &col);
- col.cx = 100;
- col.iSubItem = 0;
- col.pszText = L"ThiTest";
- ListView_InsertColumn(hListView, 2, &col);
- InsertListviewItem(0, L"1", L"test", L"TEST");
- InsertListviewItem(1, L"2", L"test", L"TEST");
- InsertListviewItem(2, L"3", L"test", L"TEST");
- InsertListviewItem(3, L"4", L"test", L"TEST");
- InsertListviewItem(4, L"5", L"test", L"TEST");
- InsertListviewItem(5, L"6", L"test", L"TEST");
- InsertListviewItem(6, L"7", L"test", L"TEST");
- InsertListviewItem(7, L"8", L"test", L"TEST");
- }
- void InsertListviewItem(int nPos, WCHAR * wszFirst,WCHAR * wszSecond,WCHAR * wszThrid)
- {
- LVITEM lvi;
- lvi.mask = LVIF_TEXT | LVIF_IMAGE;
- lvi.iImage = 0;
- lvi.pszText = L"";
- lvi.cchTextMax = 1024;
- lvi.iItem = nPos;
- lvi.iSubItem = 0;
- ListView_InsertItem(hListView, &lvi);
- ListView_SetItemText(hListView, nPos, 0, wszFirst);
- lvi.iSubItem = 1;
- ListView_SetItemText(hListView, nPos, 1, wszSecond);
- lvi.iSubItem = 2;
- ListView_SetItemText(hListView, nPos, 2, wszThrid);
- return;
- }
程序运行截图: