登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

面包会有的

... ...

 
 
 

日志

 
 

让ListBox控件支持拖动  

2009-07-29 19:32:52|  分类: VC++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |


通常我们通过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_LOCATION}}

 #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));
   }
  }


  实现了上面的代码后,我们就可以在列表框中随便改变我们的条目的顺序了,赶快试一下吧!在例程中弹出关于对话框,在列表中就可以改变顺序了。

  评论这张
 
阅读(584)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018