home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / listview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  3.7 KB  |  140 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : listview.cpp                                                         //
  10. //  Description: KListView class implementation                                      //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_EXTRA_LEAN
  16. #define WIN32_LEAN_AND_MEAN
  17.  
  18. #include <windows.h>
  19. #include <assert.h>
  20. #include <tchar.h>
  21.  
  22. #include "listview.h"
  23.  
  24.     
  25. void KListView::FromDlgItem(HWND hWnd, int id)
  26. {
  27.     m_hWnd = GetDlgItem(hWnd, id);
  28.     nRow   = -1;
  29. }
  30.  
  31.  
  32. HWND KListView::Create(HWND hParent, int id, int left, int top, int width, int height, HINSTANCE hInst)
  33. {
  34.     m_hWnd = CreateWindowEx(0, WC_LISTVIEW, _T(""), WS_CHILD | WS_VISIBLE | LVS_REPORT | WS_BORDER, 
  35.                 left, top, width, height, hParent, (HMENU) id, hInst, NULL);
  36.     nRow   = -1;
  37.  
  38.     return m_hWnd;
  39. }
  40.  
  41.  
  42. void KListView::AddIcon(int iImageList, HINSTANCE hInst, int iIcon)
  43. {
  44.     HIMAGELIST * phImage;
  45.  
  46.     switch (iImageList)
  47.     {
  48.         case LVSIL_NORMAL: 
  49.             phImage = & hImage_Normal;
  50.             break;
  51.  
  52.         case LVSIL_SMALL:
  53.             phImage = & hImage_Small;
  54.             break;
  55.  
  56.         case LVSIL_STATE:
  57.             phImage = & hImage_State;
  58.             break;
  59.  
  60.         default:
  61.             assert(FALSE);
  62.             return;
  63.     }
  64.  
  65.     if (*phImage==NULL)
  66.     {
  67.         int size = (iImageList==LVSIL_SMALL) ? 16 : 32;
  68.  
  69.         *phImage = ImageList_Create(size, size, ILC_COLOR4 | ILC_MASK, 1, 1);
  70.         ListView_SetImageList(m_hWnd, *phImage, iImageList);
  71.     }
  72.  
  73.     ImageList_AddIcon(*phImage, LoadIcon(hInst, MAKEINTRESOURCE(iIcon)));
  74. }
  75.  
  76.  
  77. void KListView::AddColumn(int col, int width, const TCHAR *title, BOOL left)
  78. {
  79.     LV_COLUMN c;
  80.  
  81.     c.mask     = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
  82.     c.fmt      = left ? LVCFMT_LEFT : LVCFMT_RIGHT;
  83.     c.cx       = width;
  84.     c.pszText  = (TCHAR *) title;
  85.     c.iSubItem = col;
  86.                     
  87.     ListView_InsertColumn(m_hWnd, col, &c);
  88. }
  89.  
  90.  
  91. void KListView::AddItem(int col, int value, int iImage)
  92. {
  93.     TCHAR temp[32];
  94.  
  95.     wsprintf(temp, _T("%d"), value);
  96.  
  97.     AddItem(col, temp, iImage);
  98. }
  99.     
  100.  
  101. void KListView::AddItem(int col, const TCHAR *text, int iImage)
  102. {
  103.     LV_ITEM item;
  104.  
  105.     if (iImage>=0)
  106.         item.mask = LVIF_TEXT | LVIF_IMAGE;
  107.     else
  108.         item.mask = LVIF_TEXT;
  109.  
  110.     // if column is 0, adding a row
  111.     if (col==0)
  112.         nRow ++;
  113.  
  114.     item.pszText  = (TCHAR *) text;
  115.     item.iItem    = nRow;
  116.     item.iSubItem = col;
  117.     item.iImage   = iImage;
  118.         
  119.     if (col==0)
  120.         ListView_InsertItem(m_hWnd, &item);
  121.     else
  122.         ListView_SetItem(m_hWnd, &item);
  123. }
  124.  
  125.  
  126. void KListView::SetItem(int row, int col, int value)
  127. {
  128.     LV_ITEM item;
  129.  
  130.     TCHAR mess[32];
  131.     wsprintf(mess, _T("%d"), value);
  132.  
  133.     item.mask     = LVIF_TEXT;
  134.     item.pszText  = mess;
  135.     item.iItem    = row;
  136.     item.iSubItem = col;
  137.         
  138.     ListView_SetItem(m_hWnd, &item);
  139. }
  140.