CListCtrl



  • Hallo
    Es ist schon eine Weile her dass ich mit der MFC unterwegs war, daher hoffe ich das ihr mir etwas auf die Sprünge helfen könnt.(..wie war das doch ? 😉 )

    Also ich habe ein ListCtrl in denen die Einträge verschoben werden können, also
    die Positionen getauscht werden. Wie bekomme ich nun heraus welcher Eintrag
    an Position 1, welcher an Postion 2 etc steht.

    Bei der Initialisierung steht z.B. "Bild1.jpg" an erster Stelle in der Liste,
    nun verschiebe ich "Bild1.jpg" ans Ende der Liste und setze "Bild25.jpg" an erste
    Stelle. Beim auslesen der Liste via:

    for ( int i = 0; i < m_ImageList.GetItemCount(); ++ i )
    {
       m_ImageList.SetSelectionMark(i);
       // einfache Hilfsfunktion ...
       ::AfxMessageBox( m_Utilitys->GetFilename(GetListFilename( i )));
    }
    

    spuckt er beharrlich die Einträge der Initialisierung aus, also wieder:
    "Bild1.jpg", "Bild2.jpg" ....."Bild25.jpg" wieder an letzter Stelle. 😮

    Mmmmhhhhhh ........... 😕



  • nun verschiebe ich "Bild1.jpg" ans Ende der Liste und setze "Bild25.jpg" an erste 
     Stelle.
    

    Wie? Mit welchem Code?



  • ..mit einer von CListCtrl abgeleiteten Klasse:

    /////////////////////////////////////////////////////////////////////////////
    // CListCtrlEnh
    
    CListCtrlEnh::CListCtrlEnh()
    {
    	m_bDragging = FALSE;
    	m_pimageListDrag = NULL;
    	m_oldDNDStyle = NULL;
    }
    
    CListCtrlEnh::~CListCtrlEnh()
    {
    }
    
    BEGIN_MESSAGE_MAP(CListCtrlEnh, CListCtrl)
    	//{{AFX_MSG_MAP(CListCtrlEnh)
    		// NOTE - the ClassWizard will add and remove mapping macros here.
    	ON_NOTIFY_REFLECT(LVN_BEGINDRAG, OnBeginDrag)
    	ON_NOTIFY_REFLECT(LVN_BEGINRDRAG, OnBeginDrag)
    	ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndLabelEdit)
    	ON_WM_MOUSEMOVE()
    	ON_WM_LBUTTONUP()
    	ON_WM_RBUTTONUP()
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CListCtrlEnh message handlers
    
    // get the index of the first selected item
    int CListCtrlEnh::GetFirstSelectedItem()
    {
    	POSITION pos = GetFirstSelectedItemPosition();
    	if (!pos)
    		return -1;
    	return GetNextSelectedItem(pos);
    }
    
    // insert column with the string ID
    /*
    int CListCtrlEnh::InsertColumn(int nCol, UINT nIDColumnHeading, int nFormat, int nWidth, int nSubItem)
    {
    	CString col;
    	col.LoadString(nIDColumnHeading);
    	return CListCtrl::InsertColumn(nCol, col, nFormat, nWidth, nSubItem);
    }
    
    void CListCtrlEnh::UpdateColumnWidths()
    {
    	SetRedraw(FALSE);
    	int width = (GetItemCount() ? LVSCW_AUTOSIZE : LVSCW_AUTOSIZE_USEHEADER);
    	for (int i = 0; i < GetHeaderCtrl()->GetItemCount(); i++)
    		SetColumnWidth(i, width);
    	SetRedraw();
    }
    */
    
    void CListCtrlEnh::BuildSelectedArray(CDWordArray &dwArray)
    {
    	POSITION pos = GetFirstSelectedItemPosition();
    	while (pos)
    		dwArray.Add(GetNextSelectedItem(pos));
    }
    
    ////////////////////////
    void CListCtrlEnh::OnBeginDrag(LPNMHDR pnmhdr, LRESULT* /*pResult*/)
    {
    	CPoint          ptItem, ptAction, ptImage;
    	NM_LISTVIEW     *pnmListView = (NM_LISTVIEW *)pnmhdr;
    
    	m_oldDNDStyle = GetExtendedStyle();
    	if (m_oldDNDStyle != 0) SetExtendedStyle(0);	// styles model original icon DND behavior
    
    	ASSERT(!m_bDragging);
    	m_bDragging = TRUE;
    	m_iItemDrag = pnmListView->iItem;
    	ptAction = pnmListView->ptAction;
    	GetItemPosition(m_iItemDrag, &ptItem);  // ptItem is relative to (0,0) and not the view origin
    	GetOrigin(&m_ptOrigin);
    
    	ASSERT(m_pimageListDrag == NULL);
    	m_pimageListDrag = CreateDragImage(m_iItemDrag, &ptImage);
    	m_sizeDelta = ptAction - ptImage;   // difference between cursor pos and image pos
    	m_ptHotSpot = ptAction - ptItem + m_ptOrigin;  // calculate hotspot for the cursor
    	m_pimageListDrag->DragShowNolock(TRUE);  // lock updates and show drag image
    	m_pimageListDrag->SetDragCursorImage(0, CPoint(0, 0));// define the hot spot for the new cursor image
    	m_pimageListDrag->BeginDrag(0, CPoint(0, 0));
    
    	ptAction -= m_sizeDelta;
    	m_pimageListDrag->DragEnter(this, ptAction);
    	m_pimageListDrag->DragMove(ptAction);  // move image to overlap original icon
    	m_iItemDrop = -1;// no drop target
    	SetCapture();
    }
    
    void CListCtrlEnh::OnMouseMove(UINT nFlags, CPoint point)
    {
    	long        lStyle;
    	int         iItem;
    	LV_ITEM     lvitem;
    
    	lStyle = GetWindowLong(m_hWnd, GWL_STYLE);
    	lStyle &= LVS_TYPEMASK;  // drag will do different things in list and report mode
    
    	if (m_bDragging)
    	{
    		ASSERT(m_pimageListDrag != NULL);
    		m_pimageListDrag->DragMove(point - m_sizeDelta);  // move the image 
    		if ((iItem = HitTest(point)) != -1)
    		{
    			lvitem.mask = LVIF_STATE;
    			lvitem.stateMask = LVIS_DROPHILITED;  // highlight the drop target
    			if (m_iItemDrop != -1)  // remove the drophighlighted from previous item
    			{
    				m_pimageListDrag->DragLeave(this);
    				lvitem.iItem = m_iItemDrop;
    				lvitem.iSubItem = 0;
    				lvitem.state = 0;	// sets drophighlighted to FALSE  
    
    				SetItem(&lvitem);
    			}
    
    			m_iItemDrop = iItem;
    			m_pimageListDrag->DragLeave(this); // unlock the window and hide drag image
    
    			if (lStyle == LVS_REPORT || lStyle == LVS_LIST)
    			{
    				lvitem.iItem = iItem;
    				lvitem.iSubItem = 0;
    				lvitem.state = LVIS_DROPHILITED;	// sets the drophighlighted
    
    				SetItem(&lvitem);
    				UpdateWindow();
    			}
    			point -= m_sizeDelta;
    			m_pimageListDrag->DragEnter(this, point);  // lock updates and show drag image
    		}
    	}
    	CListCtrl::OnMouseMove(nFlags, point);
    }
    
    void CListCtrlEnh::OnButtonUp(CPoint point)
    {
    	if (m_bDragging)  // end of the drag operation
    	{
    		long        lStyle;
    		CString     cstr;
    
    		lStyle = GetWindowLong(m_hWnd, GWL_STYLE) & LVS_TYPEMASK;
    		if (m_oldDNDStyle != 0) SetExtendedStyle(m_oldDNDStyle);
    
    		m_bDragging = FALSE;
    		ASSERT(m_pimageListDrag != NULL);
    		m_pimageListDrag->DragLeave(this);
    
    		SetItemState(m_iItemDrop, 0, LVIS_DROPHILITED);	// remove the drophighlighted from last highlighted target
    
    		m_pimageListDrag->EndDrag();
    		delete m_pimageListDrag;
    		m_pimageListDrag = NULL;
    
    		// The drop target's sub-item text is replaced by the dragged item's
    		// main text
    		if (lStyle == LVS_REPORT && m_iItemDrop != m_iItemDrag)
    		{
    			cstr = GetItemText(m_iItemDrag, 0);
    			SetItemText(m_iItemDrop, 1, cstr);
    		}
    
    		//the character string "**" is added to the end of the drop target's main text
    		if (lStyle == LVS_LIST && m_iItemDrop != m_iItemDrag)
    		{
    			cstr = GetItemText(m_iItemDrop, 0);
    			cstr += _T("**");
    			SetItemText(m_iItemDrop, 0, cstr);
    		}
    
    		  // move the icon
    		if (lStyle == LVS_ICON || lStyle == LVS_SMALLICON)
    		{
    			point -= m_ptHotSpot;  // the icon should be drawn exactly where the image is
    			point += m_ptOrigin;
    			SetItemPosition(m_iItemDrag, point);  // just move the dragged item
    		}
    
    		::ReleaseCapture();
    	}
    }
    
    void CListCtrlEnh::OnLButtonUp(UINT nFlags, CPoint point)
    {
    	OnButtonUp(point);
    	CListCtrl::OnLButtonUp(nFlags, point);
    }
    
    void CListCtrlEnh::OnRButtonUp(UINT nFlags, CPoint point)
    {
    	OnButtonUp(point);
    	CListCtrl::OnRButtonUp(nFlags, point);
    }
    
    void CListCtrlEnh::OnEndLabelEdit(LPNMHDR pnmhdr, LRESULT* /*pLResult*/)
    {
    	LV_DISPINFO  *plvDispInfo = (LV_DISPINFO *)pnmhdr;
    	LV_ITEM      *plvItem = &plvDispInfo->item;
    
    	if (plvItem->pszText != NULL)
    		SetItemText(plvItem->iItem, plvItem->iSubItem, plvItem->pszText);
    }
    


    • Du schreibst bereits, es geht um MFC, warum hast du den Thread dann nicht im MFC Forum erstellt?
    • Du solltest deinen Code auf das Wesentliche (die Problemstelle) beschränken und den richtigen BB-Code zur Hervorhebung der Sprache wählen.
    • Du brauchst Hilfe?


  • Dieser Thread wurde von Moderator/in SeppJ aus dem Forum C++ (auch C++0x und C++11) in das Forum MFC (Visual C++) verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.


Anmelden zum Antworten