To create a ListView control specify WC_LISTVIEW as the window class and register the class by specifying the ICC_LISTVIEW_CLASSES bit flag in the accompanying INITCOMMONCONTROLSEX structure.
To create and manipulate columns, use the LVCOLUMN structure and to specify the item attributes use the LVITEM structure. The Listview details view describes the way in which an item and its subitems are displayed in a grid with column headers. To display items in a details view, first set up the appropriate columns. The first column corresponds to the item’s caption, and the following columns correspond to its subitems. Without the first column, no items will be displayed in the remaining columns.
If an event occurs in the listview control, the WM_NOTIFY message is sent to the parent window. The lParam contains a pointer to an NMHDR structure that contains the notification code and additional information.
Header items are inserted and managed by sending messages such as HDM_INSERTITEM, HDM_DELETEITEM, and HDM_SETITEM. By default, each section displays a text label and a separator; however, specifying the HDS_BUTTONS window style causes each header section to behave like a push button, allowing the application to respond to user clicks on the
#include <windows.h> #include <commctrl.h> #define ID_LISTVIEW 100 #define ID_STATIC 101 #pragma comment(lib, "comctl32.lib") HWND hwndStatic; HWND hWndListView; void SetStaticBoxContents(); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; MSG msg; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT("myWindowClass"); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&wc); CreateWindowEx( WS_EX_CLIENTEDGE, TEXT("myWindowClass"), TEXT("ListView Window"), WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 440, 220, NULL, NULL, hInstance, NULL); while(GetMessage(&msg,NULL,0,0)>0) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: { INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(INITCOMMONCONTROLSEX); InitCtrls.dwICC = ICC_LISTVIEW_CLASSES; InitCommonControlsEx(&InitCtrls); hwndStatic = CreateWindow( TEXT("static"), NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | SS_LEFT, 25, 130, 150, 20, hwnd, (HMENU)ID_STATIC, NULL, NULL); hWndListView = CreateWindow( WC_LISTVIEW, NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT | LVS_SHOWSELALWAYS | LVS_EDITLABELS, 25, 10, 301, 104, hwnd, (HMENU)ID_LISTVIEW, (HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE), NULL); ListView_SetExtendedListViewStyle( hWndListView, LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_GRIDLINES); // create columns LVCOLUMN lvc; lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; lvc.fmt = LVCFMT_LEFT; lvc.iSubItem = 0; lvc.cx = 100; lvc.pszText = TEXT("Country"); ListView_InsertColumn( hWndListView, 0, &lvc); lvc.iSubItem = 1; lvc.cx = 100; lvc.pszText = TEXT("Population"); ListView_InsertColumn( hWndListView, 1, &lvc); lvc.iSubItem = 2; lvc.cx = 100; lvc.pszText = TEXT("Area"); ListView_InsertColumn( hWndListView, 2, &lvc); // insert rows LVITEM lv; lv.mask = LVIF_TEXT; lv.state = 0; lv.stateMask = 0; lv.iItem = 0; lv.iSubItem = 0; lv.pszText = TEXT("France"); ListView_InsertItem( hWndListView, &lv); ListView_SetItemText( hWndListView, 0, 1, TEXT("65,548,899")); ListView_SetItemText( hWndListView, 0, 2, TEXT("547,557")); lv.iItem = 1; lv.pszText = TEXT("UK"); ListView_InsertItem( hWndListView, &lv); ListView_SetItemText( hWndListView, 1, 1, TEXT("68,562,806")); ListView_SetItemText( hWndListView, 1, 2, TEXT("241,930")); lv.iItem = 2; lv.pszText = TEXT("Italy"); ListView_InsertItem( hWndListView, &lv); ListView_SetItemText( hWndListView, 2, 1, TEXT("59,000,000")); ListView_SetItemText( hWndListView, 2, 2, TEXT("294,140")); lv.iItem = 3; lv.pszText = TEXT("Germany"); ListView_InsertItem( hWndListView, &lv); ListView_SetItemText( hWndListView, 3, 1, TEXT("84,292,371")); ListView_SetItemText( hWndListView, 3, 2, TEXT("348,560")); break; } case WM_NOTIFY: { LPNMHDR nmhdr; nmhdr = (LPNMHDR)lParam; if(nmhdr->idFrom == ID_LISTVIEW) { switch(nmhdr->code) { case LVN_ITEMCHANGED: if(GetFocus()==hWndListView) SetStaticBoxContents(); break; case NM_CLICK: SetStaticBoxContents(); break; } } break; } case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,msg,wParam,lParam); } return 0; } void SetStaticBoxContents() { LVITEM lvi; TCHAR buf[100]; int iPos = ListView_GetNextItem( hWndListView, -1, LVNI_SELECTED); if(iPos >= 0) { ZeroMemory(&lvi,sizeof(LVITEM)); lvi.mask = LVIF_TEXT; lvi.iItem = iPos; lvi.iSubItem = 0; lvi.pszText = buf; lvi.cchTextMax = 100; ListView_GetItem( hWndListView, &lvi); SetWindowText( hwndStatic, buf); } }