Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146873
  • 博文数量: 30
  • 博客积分: 587
  • 博客等级: 中士
  • 技术积分: 245
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-17 20:57
文章分类

全部博文(30)

文章存档

2012年(30)

分类:

2012-04-23 18:58:55

原文地址:window表格 作者:dingzerong


 

点击(此处)折叠或打开

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <commctrl.h>

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

  5. #define ID_LISTVIEW 1

  6. typedef struct
  7. {
  8.    char szItemNr[8];
  9.    char szItem[32];
  10.    char szItemDescription[32];
  11. }Item;

  12. BOOL AdjustListView(HWND hList, LV_ITEM *lv, int iItems)
  13. {
  14.    int i = iItems;
  15.    int iRet;
  16.    ListView_DeleteAllItems(hList);

  17.    lv->mask = LVIF_TEXT;
  18.    lv->iSubItem = 0;
  19.    lv->pszText = LPSTR_TEXTCALLBACK;

  20.    while(i > 0)
  21.    {
  22.       iRet = ListView_InsertItem(hList, lv);
  23.       i--;
  24.    }
  25.    return TRUE;
  26. }


  27. LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  28. {
  29.    static CREATESTRUCT *cs;
  30.    static HWND hWndListView;
  31.    static HFONT hFont;
  32.    static LV_ITEM lv;
  33.    static Item ListItem[3];
  34.    static char szColumnHeader[3][12] = {"Number", "Item", "Description"};
  35.    int index;
  36.    int iiWidth[3];
  37.    LV_COLUMN lvC;
  38.    RECT rect;

  39.    LV_DISPINFO *lvd;
  40.    NMHDR *hdr;

  41.    switch (iMsg)
  42.    {
  43.    case WM_CREATE :
  44.       cs = (CREATESTRUCT *)lParam;

  45.       hFont = CreateFont(11, 0, 0, 0, 500, FALSE, FALSE, FALSE,
  46.          DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
  47.          CLIP_CHARACTER_PRECIS, PROOF_QUALITY,
  48.          FF_DONTCARE, "MS SANS SERIF");

  49.       hWndListView = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW,
  50.          "", WS_VISIBLE | WS_CHILD | LVS_REPORT | LVS_SHOWSELALWAYS,
  51.          0, 0, 0, 0, hwnd,(HMENU)ID_LISTVIEW, cs->hInstance, NULL);

  52.       ListView_SetExtendedListViewStyle(hWndListView, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

  53.       iiWidth[0] = 60; // 设置第一列的宽度
  54.       iiWidth[1] = 100; // 设置第二列的宽度
  55.       iiWidth[2] = 200; // 设置第三列的宽度



  56. /*
  57. typedef struct _LV_COLUMN {
  58.     UINT mask;        //结构成员有效性屏蔽位
  59.     int fmt;        //表列对齐方式

  60.     int cx;            //表列的象素宽度
  61.     LPTSTR pszText; //表列的表头名
  62.     int cchTextMax; //表列名的文本长度
  63.     int iSubItem;    //与表列关联的子表项索引号
  64. } LV_COLUMN;
  65. */

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

  68.       for(index = 0; index < 3; index++)
  69.       {
  70.          lvC.cx = iiWidth[index]; // 宽度 60 100 200
  71.          lvC.pszText = szColumnHeader[index]; // 每列的名字
  72.          lvC.iSubItem = index;                 // 索引 0 1 2 给出了3列 索引        

  73. 【1】
  74.          
  75.          ListView_InsertColumn(hWndListView,index,&lvC);    //
  76.       }

  77.       // Add some items here
  78.       strcpy(ListItem[0].szItemNr, "1");                    //行索引
  79.       strcpy(ListItem[0].szItem, "Cat");
  80.       strcpy(ListItem[0].szItemDescription, "Pet");

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

  84.      /*
  85.      */
  86.       AdjustListView(hWndListView, &lv, index);
  87.       break;

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

  92.    case WM_NOTIFY :
  93.       switch(((LPNMHDR)lParam)->code)
  94.       {
  95.       case NM_DBLCLK : //捕抓, 被点击两次的信息
  96.          hdr = (NMHDR FAR*)lParam;
  97.          if(hdr->hwndFrom == hWndListView)
  98.          {
  99.             index = ListView_GetNextItem(hWndListView, -1, LVNI_SELECTED);
  100.             if(index != -1)
  101.             {
  102.                MessageBox(hwnd, ListItem[index].szItem, "Doubleclicked on this item", MB_OK);
  103.             }
  104.          }
  105.          break;

  106. /*
  107. 两种方法:
  108. ,已知具体的插入项目
  109. m_pList->InsertItem (iIndex, LPSTR_TEXTCALLBACK);

  110. ,只知道项目总数,iCount为列表项目总数
  111. SetItemCountEx (iCount, LVSICF_NOINVALIDATEALL);
  112. 或者
  113. SendMessage(LVM_SETITEMCOUNT, (WPARAM)iCount, (LPARAM)LVSICF_NOINVALIDATEALL);
  114. */
  115.     case LVN_GETDISPINFO :
  116.          lvd = (LV_DISPINFO FAR*)lParam;
  117.          if((((LPNMHDR)lParam)->hwndFrom == hWndListView))
  118.          {
  119.             switch(lvd->item.iSubItem)                                                    

  120. //    【1】
  121.             {
  122.             case 0:
  123.                lvd->item.pszText = ListItem[lvd->item.iItem].szItemNr; // 0 1
  124.              printf("%s %s(Line %d): \n",__FILE__,__FUNCTION__,__LINE__);
  125.                break;
  126.             case 1:
  127.                lvd->item.pszText = ListItem[lvd->item.iItem].szItem;
  128.              printf("%s %s(Line %d): \n",__FILE__,__FUNCTION__,__LINE__);
  129.                break;
  130.             case 2:
  131.                lvd->item.pszText = ListItem[lvd->item.iItem].szItemDescription;
  132.              printf("%s %s(Line %d): \n",__FILE__,__FUNCTION__,__LINE__);
  133.                break;
  134.             }
  135.             break;
  136.          }
  137.       }
  138.       break;

  139.    case WM_CLOSE :
  140.       DestroyWindow(hwnd);
  141.       break;

  142.    case WM_DESTROY :
  143.       DeleteObject(hFont);
  144.       PostQuitMessage(0);
  145.       break;
  146.    }
  147.    return DefWindowProc(hwnd, iMsg, wParam, lParam);
  148. }

  149. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
  150. {
  151.    HWND hwnd;
  152.    MSG msg;
  153.    WNDCLASS wndclass;
  154.    char szAppName[] = "ListviewApp";
  155.    INITCOMMONCONTROL*** icex;

  156.    wndclass.style = 0;
  157.    wndclass.lpfnWndProc = WndProc;
  158.    wndclass.cbCl***tra = 0;
  159.    wndclass.cbWndExtra = 0;
  160.    wndclass.hInstance = hInstance;
  161.    wndclass.hIcon = LoadIcon(hInstance, szAppName);
  162.    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  163.    wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
  164.    wndclass.lpszMenuName = szAppName;
  165.    wndclass.lpszClassName = szAppName;
  166.    RegisterClass(&wndclass);

  167.    icex.dwSize = sizeof(INITCOMMONCONTROL***);
  168.    icex.dwICC = ICC_LISTVIEW_CLASSES;
  169.    InitCommonControl***(&icex);

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

  173.    while (GetMessage(&msg, NULL, 0, 0))
  174.    {
  175.       TranslateMessage(&msg);
  176.       DispatchMessage(&msg);
  177.    }
  178.    return 0;
  179. }


 

阅读(1260) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~