Usually we will display our information list through the ListBox control, then we can select our entry information through the mouse, but the ListBox control in the VC is not supported. Perhaps we sometimes need to change our list of lists, adapt to our requirements, and below is the implementation method.
Design ideas:
1. If you select an entry and drag it through the left mouse button, we will record the location and entry string of the currently selected entry through the variable record, and the depot of this entry.
2. When the mouse moves to the position you want to move, let's disconnect the left button. At this time, we will insert the previously selected entry here, and delete the entry of the original position.
Implementation steps:
1. Define a class CMYListBox extended from the CListbox class, the code is analyzed.
2. Define our list control variables through the new class.
Code analysis:
// MyListBox.h: header file // // CMyListBox windowclass 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! : // #inclu de "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_MAPEND_MESSAGE_MAP () // CMyListBox message handlers void CMyListBox :: OnLButtonDown (UINT nFlags, CPoint point) {CListBox :: OnLButtonDown (nFlags, point); file: // check if An entry, at this time, it will be wrong.
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)) / / Determine the mouse to move to the appropriate location {m_newposition = getcursel (); if (m_newposition