Let ListBox control support

zhaozj2021-02-08  213

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 setcurseel (m_newposition); file: // Set the deputy value of the mobile entry, if delete or add a record, the deputy value with strings Mobile SETITEMDATA (M_NewPosition, M_ItemData); Trace ("% D% 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); T Race ("% D% 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));}}

转载请注明原文地址:https://www.9cbs.com/read-2006.html

New Post(0)