ListView

>> Thursday, October 29, 2009



#include <windows.h>
#include <stdio.h>
#include <commctrl.h>

#pragma comment(lib, "comctl32.lib")

#define ID_LISTVIEW 1

typedef struct
{
char szItemNr[8];
char szItem[32];
char szItemDescription[32];
}
Item;

BOOL AdjustListView(HWND hList, LV_ITEM *lv, int iItems)
{
int i = iItems;
int iRet;
ListView_DeleteAllItems(hList);

lv->mask = LVIF_TEXT;
lv->iSubItem = 0;
lv->pszText = LPSTR_TEXTCALLBACK;

while (i > 0)
{
iRet = ListView_InsertItem(hList, lv);
i--;
}
return TRUE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
static CREATESTRUCT *cs;
static HWND hWndListView;
static HFONT hFont;
static LV_ITEM lv;
static Item ListItem[3];
static char szColumnHeader[3][12] =
{
"Number", "Item", "Description"
};
int index;
int iiWidth[3];
LV_COLUMN lvC;
RECT rect;

LV_DISPINFO *lvd;
NMHDR *hdr;

switch (iMsg)
{
case WM_CREATE:
cs = (CREATESTRUCT*)lParam;
hFont = CreateFont(11, 0, 0, 0, 500, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_CHARACTER_PRECIS, PROOF_QUALITY, FF_DONTCARE,
"MS SANS SERIF");
hWndListView = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, "",
WS_VISIBLE | WS_CHILD | LVS_REPORT | LVS_SHOWSELALWAYS, 0, 0, 0, 0,
hwnd, (HMENU)ID_LISTVIEW, cs->hInstance, NULL);

ListView_SetExtendedListViewStyle(hWndListView, LVS_EX_FULLROWSELECT |
LVS_EX_GRIDLINES);

iiWidth[0] = 60;
iiWidth[1] = 100;
iiWidth[2] = 400;

lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvC.fmt = LVCFMT_LEFT;

for (index = 0; index < 3; index++)
{
lvC.iSubItem = index;
lvC.cx = iiWidth[index];
lvC.pszText = szColumnHeader[index];
ListView_InsertColumn(hWndListView, index, &lvC);
}

// Add some items here
strcpy(ListItem[0].szItemNr, "1");
strcpy(ListItem[0].szItem, "Cat");
strcpy(ListItem[0].szItemDescription, "Pet");

strcpy(ListItem[1].szItemNr, "2");
strcpy(ListItem[1].szItem, "Dog");
strcpy(ListItem[1].szItemDescription, "Pet");

AdjustListView(hWndListView, &lv, index);
break;

case WM_SIZE:
GetClientRect(hwnd, &rect);
MoveWindow(hWndListView, 3, 3, rect.right - 6, rect.bottom - 6, 1);
break;

case WM_NOTIFY:
switch (((LPNMHDR)lParam)->code)
{
case NM_DBLCLK:
hdr = (NMHDR FAR*)lParam;
if (hdr->hwndFrom == hWndListView)
{
index = ListView_GetNextItem(hWndListView, - 1, LVNI_SELECTED);
if (index != - 1)
{
MessageBox(hwnd, ListItem[index].szItem,
"Doubleclicked on this item", MB_OK);
}
}
break;

case LVN_GETDISPINFO:
lvd = (LV_DISPINFO FAR*)lParam;
if ((((LPNMHDR)lParam)->hwndFrom == hWndListView))
{
switch (lvd->item.iSubItem)
{
case 0:
lvd->item.pszText = ListItem[lvd->item.iItem].szItemNr;
break;
case 1:
lvd->item.pszText = ListItem[lvd->item.iItem].szItem;
break;
case 2:
lvd->item.pszText = ListItem[lvd->item.iItem].szItemDescription;
break;
}
break;
}
}
break;

case WM_CLOSE:
DestroyWindow(hwnd);
break;

case WM_DESTROY:
DeleteObject(hFont);
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine,
int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
char szAppName[] = "ListviewApp";
INITCOMMONCONTROLSEX icex;

wndclass.style = 0;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, szAppName);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName;
RegisterClass(&wndclass);

icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex);

hwnd = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, 600, 250, NULL, NULL, hInstance, szCmdLine);

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}

0 comments: