//listview control example
#include <windows.h>
#include <commctrl.h>
#define ID_LISTVIEW 100
#define ID_STATIC 101
#pragma comment(lib, "comctl32.lib") //adds link to control control DLL
HWND hwndStatic,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;
//Registering the Window Class
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:
{
//load common control class ICC_LISTVIEW_CLASSES from the dynamic-link library (DLL)
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwICC = ICC_LISTVIEW_CLASSES;
InitCtrls.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCommonControlsEx(&InitCtrls);
hwndStatic=CreateWindow(TEXT("static"), NULL,WS_BORDER | WS_CHILD | WS_VISIBLE | SS_LEFT,25, 130, 100, 20,hwnd, (HMENU) 1, NULL, NULL);//static control
// Create the list view window.
hWndListView=CreateWindow(WC_LISTVIEW,NULL,WS_CHILD |WS_BORDER | LVS_REPORT| LVS_SHOWSELALWAYS | LVS_EX_GRIDLINES|LVS_EDITLABELS,25,10,301,104,hwnd,(HMENU)ID_LISTVIEW, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),NULL);
ShowWindow(hWndListView, SW_SHOWNORMAL);
//Sets extended styles for list view controls
ListView_SetExtendedListViewStyle(hWndListView, LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP|LVS_EX_GRIDLINES|LVS_OWNERDATA|LVS_SORTASCENDING );//set listview characeristics
//create listview column structure and insert 3 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);
//create listview item structure and add contents
LVITEM lv ;
lv.iSubItem = 0;
lv.iItem = 0;
lv.state = 0;
lv.stateMask = 0;
lv.iSubItem = 0;
//add 1st line
ListView_InsertItem(hWndListView, &lv);
ListView_SetItemText(hWndListView, 0, 0,TEXT("France"));
ListView_SetItemText(hWndListView, 0, 1,TEXT("65,548,899"));
ListView_SetItemText(hWndListView, 0, 2,TEXT("547,557"));
//add 2nd line
ListView_InsertItem(hWndListView, &lv);
ListView_SetItemText(hWndListView, 0, 0,TEXT("UK"));
ListView_SetItemText(hWndListView, 0, 1,TEXT("68,562,806"));
ListView_SetItemText(hWndListView, 0, 2,TEXT("241,930"));
//add 3rd line
ListView_InsertItem(hWndListView, &lv);
ListView_SetItemText(hWndListView, 0, 0,TEXT("Italy"));
ListView_SetItemText(hWndListView, 0, 1,TEXT("68,562,806"));
ListView_SetItemText(hWndListView, 0, 2,TEXT("294,140 "));
//add 4th line
ListView_InsertItem(hWndListView, &lv);
ListView_SetItemText(hWndListView, 0, 0,TEXT("Germany"));
ListView_SetItemText(hWndListView, 0, 1,TEXT("84,292,371"));
ListView_SetItemText(hWndListView, 0, 2,TEXT("348,560"));
}
break;
//The WM_NOTIFY message is sent by a common control to its parent window
case WM_NOTIFY:
{
HWND wndfocus=GetFocus();
//The NMHDR structure contains the handle and ID of the control sending the message and the notification code
switch (((LPNMHDR)lParam)->code)
{
//checks if liveview item is changed
case LVN_ITEMCHANGED:
//checks if the item changed is the current listview and whether its has focus
if (((LPNMHDR)lParam)->idFrom == ID_LISTVIEW &&wndfocus==hWndListView)
{
SetStaticBoxContents();
}
break;
}
NMHEADER * header_item = (NMHEADER *)lParam;
//Notifies a header control's parent window that the user clicked the listview header
if ( header_item->hdr.code == HDN_ITEMCLICK )
{
switch ( header_item ->iItem)
{
case 0:
MessageBox( NULL, TEXT("header 1"), TEXT("header 1 clicked"), MB_OK );
break;
case 1:
MessageBox( NULL, TEXT("header 2"), TEXT("header 2 clicked"), MB_OK );
break;
case 2:
MessageBox( NULL, TEXT("header 3"), TEXT("header 3 clicked"), MB_OK );
break;
}
}
//NM_CLICK is sent by a list-view control when the user clicks an item with the left mouse button.
if ( header_item->hdr.code == NM_CLICK )
{
SetStaticBoxContents();
}
return 0;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
//set value of static box to selected value in listview
void SetStaticBoxContents()
{
LVITEMA lvi;
TCHAR buf[20];
int iPos = ListView_GetNextItem(hWndListView, -1, LVNI_SELECTED);
if (iPos>=0)
{
lvi.iItem=iPos;
lvi.mask=LVIF_TEXT;
lvi.pszText = buf;
lvi.iSubItem = 0;
lvi.cchTextMax = 10;
ListView_GetItem(hWndListView,&lvi);
SetWindowText(hwndStatic, buf);
}
}
Last Updated: 15 September 2022