home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / listhdr / mlistctl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  5.0 KB  |  175 lines

  1. // MyListCtrl.cpp : implementation file
  2. //
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include "stdafx.h"
  15. #include "listhdr.h"
  16. #include "mlistctl.h"
  17. //#include "listcpg.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CMyListCtrl
  27.  
  28. CMyListCtrl::CMyListCtrl()
  29. {
  30.     m_bDragging = FALSE;
  31.     m_pimageListDrag = NULL;
  32. }
  33.  
  34. CMyListCtrl::~CMyListCtrl()
  35. {
  36. }
  37.  
  38.  
  39. BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
  40.     //{{AFX_MSG_MAP(CMyListCtrl)
  41.     ON_NOTIFY_REFLECT(LVN_BEGINDRAG, OnBeginDrag)
  42.     ON_NOTIFY_REFLECT(LVN_BEGINRDRAG, OnBeginDrag)
  43.     ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndLabelEdit)
  44.     ON_WM_MOUSEMOVE()
  45.     ON_WM_LBUTTONUP()
  46.     ON_WM_RBUTTONUP()
  47.     //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CMyListCtrl message handlers
  52. void CMyListCtrl::OnBeginDrag(LPNMHDR pnmhdr, LRESULT *pResult)
  53. {
  54.     CPoint          ptItem, ptAction, ptImage;
  55.     NM_LISTVIEW     *pnmListView = (NM_LISTVIEW *)pnmhdr;
  56.  
  57.     ASSERT(!m_bDragging);
  58.     m_bDragging = TRUE;
  59.     m_iItemDrag = pnmListView->iItem;
  60.     ptAction = pnmListView->ptAction;
  61.     GetItemPosition(m_iItemDrag, &ptItem);  // ptItem is relative to (0,0) and not the view origin
  62.     GetOrigin(&m_ptOrigin);
  63.  
  64.     ASSERT(m_pimageListDrag == NULL);
  65.     m_pimageListDrag = CreateDragImage(m_iItemDrag, &ptImage);
  66.     m_sizeDelta = ptAction - ptImage;   // difference between cursor pos and image pos
  67.     m_ptHotSpot = ptAction - ptItem + m_ptOrigin;  // calculate hotspot for the cursor
  68.     m_pimageListDrag->DragShowNolock(TRUE);  // lock updates and show drag image
  69.     m_pimageListDrag->SetDragCursorImage(0, m_ptHotSpot);  // define the hot spot for the new cursor image
  70.     m_pimageListDrag->BeginDrag(0, CPoint(0, 0));
  71.     ptAction -= m_sizeDelta;
  72.     m_pimageListDrag->DragEnter(this, ptAction);
  73.     m_pimageListDrag->DragMove(ptAction);  // move image to overlap original icon
  74.     SetCapture();
  75. }
  76.  
  77. void CMyListCtrl::OnMouseMove(UINT nFlags, CPoint point)
  78. {
  79.     long        lStyle;
  80.     int         iItem;
  81.     LV_ITEM     lvitem;
  82.  
  83.     lStyle = GetWindowLong(m_hWnd, GWL_STYLE);
  84.     lStyle &= LVS_TYPEMASK;  // drag will do different things in list and report mode
  85.     if (m_bDragging)
  86.     {
  87.         ASSERT(m_pimageListDrag != NULL);
  88.         m_pimageListDrag->DragMove(point - m_sizeDelta);  // move the image
  89.         if ((iItem = HitTest(point)) != -1)
  90.         {
  91.             m_iItemDrop = iItem;
  92.             m_pimageListDrag->DragLeave(this); // unlock the window and hide drag image
  93.             if (lStyle == LVS_REPORT || lStyle == LVS_LIST)
  94.             {
  95.                 lvitem.iItem = iItem;
  96.                 lvitem.iSubItem = 0;
  97.                 lvitem.mask = LVIF_STATE;
  98.                 lvitem.stateMask = LVIS_DROPHILITED;  // highlight the drop target
  99.                 SetItem(&lvitem);
  100.             }
  101.  
  102.             point -= m_sizeDelta;
  103.             m_pimageListDrag->DragEnter(this, point);  // lock updates and show drag image
  104.         }
  105.     }
  106.  
  107.     CListCtrl::OnMouseMove(nFlags, point);
  108. }
  109.  
  110.  
  111. void CMyListCtrl::OnButtonUp(CPoint point)
  112. {
  113.     if (m_bDragging)  // end of the drag operation
  114.     {
  115.         long        lStyle;
  116.         CString     cstr;
  117.  
  118.         lStyle = GetWindowLong(m_hWnd, GWL_STYLE) & LVS_TYPEMASK;
  119.         m_bDragging = FALSE;
  120.  
  121.         ASSERT(m_pimageListDrag != NULL);
  122.         m_pimageListDrag->DragLeave(this);
  123.         m_pimageListDrag->EndDrag();
  124.         delete m_pimageListDrag;
  125.         m_pimageListDrag = NULL;
  126.  
  127.         // The drop target's sub-item text is replaced by the dragged item's
  128.         // main text
  129.         if (lStyle == LVS_REPORT && m_iItemDrop != m_iItemDrag)
  130.         {
  131.             cstr = GetItemText(m_iItemDrag, 0);
  132.             SetItemText(m_iItemDrop, 1, cstr);
  133.         }
  134.  
  135.         //the character string "**" is added to the end of the drop target's main text
  136.         if (lStyle == LVS_LIST && m_iItemDrop != m_iItemDrag)
  137.         {
  138.             cstr = GetItemText(m_iItemDrop, 0);
  139.             cstr += _T("**");
  140.             SetItemText(m_iItemDrop, 0, cstr);
  141.         }
  142.  
  143.           // move the icon
  144.         if (lStyle == LVS_ICON || lStyle == LVS_SMALLICON)
  145.         {
  146.             point -= m_ptHotSpot;  // the icon should be drawn exactly where the image is
  147.             point += m_ptOrigin;
  148.             SetItemPosition(m_iItemDrag, point);  // just move the dragged item
  149.         }
  150.  
  151.         ::ReleaseCapture();
  152.     }
  153. }
  154.  
  155. void CMyListCtrl::OnLButtonUp(UINT nFlags, CPoint point)
  156. {
  157.     OnButtonUp(point);
  158.     CListCtrl::OnLButtonUp(nFlags, point);
  159. }
  160.  
  161. void CMyListCtrl::OnRButtonUp(UINT nFlags, CPoint point)
  162. {
  163.     OnButtonUp(point);
  164.     CListCtrl::OnRButtonUp(nFlags, point);
  165. }
  166.  
  167. void CMyListCtrl::OnEndLabelEdit(LPNMHDR pnmhdr, LRESULT *pLResult)
  168. {
  169.     LV_DISPINFO  *plvDispInfo = (LV_DISPINFO *)pnmhdr;
  170.     LV_ITEM      *plvItem = &plvDispInfo->item;
  171.  
  172.     if (plvItem->pszText != NULL)
  173.         SetItemText(plvItem->iItem, plvItem->iSubItem, plvItem->pszText);
  174. }
  175.