- #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] = 200; // 设置第三列的宽度
- /*
- typedef struct _LV_COLUMN {
- UINT mask; //结构成员有效性屏蔽位
- int fmt; //表列对齐方式
- int cx; //表列的象素宽度
- LPTSTR pszText; //表列的表头名
- int cchTextMax; //表列名的文本长度
- int iSubItem; //与表列关联的子表项索引号
- } LV_COLUMN;
- */
- lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
- lvC.fmt = LVCFMT_LEFT;
- for(index = 0; index < 3; index++)
- {
- lvC.cx = iiWidth[index]; // 宽度 60 100 200
- lvC.pszText = szColumnHeader[index]; // 每列的名字
- lvC.iSubItem = index; // 索引 0 1 2 给出了3列 索引
- 【1】
-
- 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;
- /*
- 两种方法:
- 一,已知具体的插入项目
- m_pList->InsertItem (iIndex, LPSTR_TEXTCALLBACK);
- 二,只知道项目总数,iCount为列表项目总数
- SetItemCountEx (iCount, LVSICF_NOINVALIDATEALL);
- 或者
- SendMessage(LVM_SETITEMCOUNT, (WPARAM)iCount, (LPARAM)LVSICF_NOINVALIDATEALL);
- */
- case LVN_GETDISPINFO :
- lvd = (LV_DISPINFO FAR*)lParam;
- if((((LPNMHDR)lParam)->hwndFrom == hWndListView))
- {
- switch(lvd->item.iSubItem)
- // 【1】
- {
- case 0:
- lvd->item.pszText = ListItem[lvd->item.iItem].szItemNr; // 0 1
- printf("%s %s(Line %d): \n",__FILE__,__FUNCTION__,__LINE__);
- break;
- case 1:
- lvd->item.pszText = ListItem[lvd->item.iItem].szItem;
- printf("%s %s(Line %d): \n",__FILE__,__FUNCTION__,__LINE__);
- break;
- case 2:
- lvd->item.pszText = ListItem[lvd->item.iItem].szItemDescription;
- printf("%s %s(Line %d): \n",__FILE__,__FUNCTION__,__LINE__);
- 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";
- INITCOMMONCONTROL*** icex;
- wndclass.style = 0;
- wndclass.lpfnWndProc = WndProc;
- wndclass.cbCl***tra = 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(INITCOMMONCONTROL***);
- icex.dwICC = ICC_LISTVIEW_CLASSES;
- InitCommonControl***(&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 0;
- }
阅读(1260) | 评论(0) | 转发(0) |