惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - 张尹

Windows管道技术简述 内核printf源代码分析 获得控制台程序的输出 如何在控制台程序中读取WORD文档的内容? - 张尹 - 博客园 简单封装的一个彩色进度条 C语言编程技巧汇萃(包含大量实用函数) 一个编程函数集(包含大量C实用函数) MD5算法说明及源码 C51 怎样将1个子程序段定位在1个固定的地址位置? 动态真彩工具栏 一个实用的 CToolBar 扩展类 CToolBarEx 一步步制作真彩工具条 [转载精品]C++程序员经常问的11个问题 关于CRichEditCtrl中文字颜色的控制 如何制作Windows自定义边框的对话框 MFC下窗口分割和文字输出的实现 VC通用控件编程之CSlider控件 CListCtrl使用详解 - 张尹 一个不错的算法
让ListBox控件支持拖动
张尹 · 2007-01-17 · via 博客园 - 张尹

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

  }