通常我们通过ListBox控件来显示我们的信息列表,然后我们可以通过鼠标来选择我们的条目信息,但VC中的ListBox控件是不支持拖动的。也许我们有时需要改变我们的列表顺序,已适应我们的要求,下面是实现的方法。

  设计思路:


  1. 如果通过鼠标左键选中某一条目并拖动,此时我们通过变量记录当前选中条目的位置和条目字符串以及此条目的副值。

  2. 鼠标移动到要移动到的位置后放开左键,此时我们把以前选中的条目插入到此处,同时,删掉原位置的条目。

  实现步骤:

  1. 定义一个从ClistBox类扩展的类CMyListBox,代码下面分析。

  2. 通过新类定义我们的列表控件变量。

  代码分析:


// MyListBox.h : header file
// 
// CMyListBox window

class CMyListBox : public CListBox
{
 
// Construction
 public:
 CMyListBox();

 
// Attributes
 private:
  BOOL m_LButtonDownFlag;
  BOOL m_MouseMoveFlag;
  
int m_OldPosition;
  
int m_NewPosition;
  CString m_DragString;
  DWORD m_ItemData;
 
public:

  
// Operations
 public:

  
// Overrides
  
// ClassWizard generated virtual function overrides
  file://{{AFX_VIRTUAL(CMyListBox)
file://}}AFX_VIRTUAL

 
// Implementation
 public:
  
virtual ~CMyListBox();

  
// Generated message map functions
 protected:
  file:
//{{AFX_MSG(CMyListBox)
  afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  afx_msg 
void OnLButtonUp(UINT nFlags, CPoint point);
  afx_msg 
void OnMouseMove(UINT nFlags, CPoint point);
  
// NOTE - the ClassWizard will add and remove member functions here.
file://}}AFX_MSG

 DECLARE_MESSAGE_MAP()
}
;

file:
//{{AFX_INSERT_LOCATI 
ON}}

 
#endif //  !defined(AFX_MYLISTBOX_H__CF3EDAA5_BBD7_43CD_80CB_A86B65D9A607__INCLUDED_)


 
// MyListBox.cpp : implementation file
 file://

 #include 
"stdafx.h"
 #include 
"sditest.h"
 #include 
"MyListBox.h"
 
 #ifdef _DEBUG
 
#define new DEBUG_NEW
 
#undef THIS_FILE
 
static char THIS_FILE[] = __FILE__;
 
#endif

 
// CMyListBox

 CMyListBox::CMyListBox()
  
{
   m_LButtonDownFlag 
= FALSE;
   m_MouseMoveFlag 
= FALSE;
  }


 CMyListBox::
~CMyListBox()
  
{
 }



 BEGIN_MESSAGE_MAP(CMyListBox, CListBox)
 file:
//{{AFX_MSG_MAP(CMyListBox)
 ON_WM_LBUTTONDOWN()
 ON_WM_LBUTTONUP()
 ON_WM_MOUSEMOVE()
 
// NOTE - the ClassWizard will add and remove mapping macros here.
 file://}}AFX_MSG_MAP
END_MESSAGE_MAP()

 
// CMyListBox message handlers
 void CMyListBox::OnLButtonDown(UINT nFlags, CPoint point) 
  
{
   CListBox::OnLButtonDown(nFlags, point);
   file:
//如果选中一个条目,此时进行处理,否则会出错。
  if(GetCurSel() != -1)
   m_LButtonDownFlag 
= TRUE;
  }


 
void CMyListBox::OnLButtonUp(UINT nFlags, CPoint point) 
  
{
   CListBox::OnLButtonUp(nFlags, point);
   m_LButtonDownFlag 
= FALSE;
   
if(m_MouseMoveFlag)
   
{
    m_MouseMoveFlag 
= FALSE;
    POINT pt;
    ::GetCursorPos(
&pt);
    CRect iRect;
    
this->GetWindowRect(iRect);
    
if(iRect.PtInRect(pt))//确定鼠标移动到了合适的位置
    {
     m_NewPosition 
= GetCurSel();
     
if(m_NewPosition < m_OldPosition)
     
{
      InsertString(m_NewPosition,m_DragString);
      DeleteString(m_OldPosition
+1);
      
this->SetCurSel(m_NewPosition);
      file:
//设置移动条目的副值,如果删除或者添加一条记录,副值会随字符串一起移动
      SetItemData(m_NewPosition,m_ItemData);
      TRACE(
"%d%d%d%d%d%d%d%d",GetItemData(0),GetItemData(1),_
GetItemData(
2),GetItemData(3),GetItemData(4),_
GetItemData(
5),GetItemData(6),GetItemData(7));
}

     
else
      
{
       InsertString(m_NewPosition
+1,m_DragString);
       DeleteString(m_OldPosition);
       
this->SetCurSel(m_NewPosition);
       SetItemData(m_NewPosition,m_ItemData);
       TRACE(
"%d%d%d%d%d%d%d%d",GetItemData(0),GetItemData(1),_
GetItemData(
2),GetItemData(3),GetItemData(4),_
GetItemData(
5),GetItemData(6),GetItemData(7));
}

     }

   }


  }


  
void CMyListBox::OnMouseMove(UINT nFlags, CPoint point) 
   
{
    CListBox::OnMouseMove(nFlags, point);
    
if(m_LButtonDownFlag)
    
{
     m_MouseMoveFlag 
= TRUE;
     m_OldPosition 
= GetCurSel();
     GetText(m_OldPosition,m_DragString);
     
try{
      m_ItemData 
= GetItemData(m_OldPosition);
     }

    
catch()
    
{
     AfxMessageBox(
"Wrong!");
    }


    m_LButtonDownFlag 
= FALSE;
    SetCursor(AfxGetApp()
->LoadCursor(IDC_CURSOR1));
   }

  }

posted on 2007-01-17 02:37  张尹  阅读(376)  评论(0编辑  收藏  举报