IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

MFC Discussion :

Drag & Drop dans un CTreeCtrl


Sujet :

MFC

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    147
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 147
    Par défaut Drag & Drop dans un CTreeCtrl
    Salut,

    J'ai franchement du mal avec le drag & drop, mais j'ai fini par trouvé ce que je cherchai : faire un drag & drop dans un CTreeCtrl.
    Il a fallu recoller les morceaux parce que tout le code était éparpillé (je remercie quand même l'auteur de ces lignes de codes parce que ça marche).
    Donc pour tous ceux qui auraient le même problème je met le code qui marche :

    Dans le .h

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    protected:
    	CImageList*	m_pDragImage;
    	BOOL		m_bLDragging;
    	HTREEITEM	m_hitemDrag,m_hitemDrop;
     
    	HTREEITEM CopyItem( HTREEITEM hItem, HTREEITEM htiNewParent, 
                                            HTREEITEM htiAfter = TVI_LAST );
     
    	virtual void OnItemCopied( HTREEITEM hItem, HTREEITEM hNewItem );
     
    	HTREEITEM CopyBranch( HTREEITEM htiBranch, HTREEITEM htiNewParent, 
                                                    HTREEITEM htiAfter = TVI_LAST);
    Dans le .cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    CMyTreeCtrl::CMyTreeCtrl()
    {
    	m_pDragImage = NULL;
    	m_hitemDrag = NULL;
    	m_hitemDrop = NULL;
    	m_bLDragging = FALSE;
    }
     
     
    // CopyItem             - Copies an item to a new location
    // Returns              - Handle of the new item
    // hItem                - Item to be copied
    // htiNewParent         - Handle of the parent for new item
    // htiAfter             - Item after which the new item should be created
    HTREEITEM CMyTreeCtrl::CopyItem( HTREEITEM hItem, HTREEITEM htiNewParent, 
                                            HTREEITEM htiAfter /*= TVI_LAST*/ )
    {
            TV_INSERTSTRUCT         tvstruct;
            HTREEITEM                       hNewItem;
            CString                         sText;
     
            // get information of the source item
            tvstruct.item.hItem = hItem;
            tvstruct.item.mask = TVIF_CHILDREN | TVIF_HANDLE | 
                                    TVIF_IMAGE | TVIF_SELECTEDIMAGE;
            GetItem(&tvstruct.item);  
            sText = GetItemText( hItem );
     
            tvstruct.item.cchTextMax = sText.GetLength();
            tvstruct.item.pszText = sText.LockBuffer();
     
            // Insert the item at proper location
            tvstruct.hParent = htiNewParent;
            tvstruct.hInsertAfter = htiAfter;
            tvstruct.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
            hNewItem = InsertItem(&tvstruct);
            sText.ReleaseBuffer();
     
            // Now copy item data and item state.
            SetItemData( hNewItem, GetItemData( hItem ));
            SetItemState( hNewItem, GetItemState( hItem, TVIS_STATEIMAGEMASK ), 
                                                            TVIS_STATEIMAGEMASK );
     
            // Call virtual function to allow further processing in derived class
            OnItemCopied( hItem, hNewItem );
     
            return hNewItem;
    }
     
     
    void CMyTreeCtrl::OnItemCopied(HTREEITEM /*hItem*/, HTREEITEM /*hNewItem*/ )
    {
            // Virtual function 
    }
     
     
    // CopyBranch           - Copies all items in a branch to a new location
    // Returns              - The new branch node
    // htiBranch            - The node that starts the branch
    // htiNewParent - Handle of the parent for new branch
    // htiAfter             - Item after which the new branch should be created
    HTREEITEM CMyTreeCtrl::CopyBranch( HTREEITEM htiBranch, HTREEITEM htiNewParent, 
                                                    HTREEITEM htiAfter /*= TVI_LAST*/ )
    {
            HTREEITEM hChild;
     
            HTREEITEM hNewItem = CopyItem( htiBranch, htiNewParent, htiAfter );
            hChild = GetChildItem(htiBranch);
            while( hChild != NULL)
            {
                    // recursively transfer all the items
                    CopyBranch(hChild, hNewItem);  
                    hChild = GetNextSiblingItem( hChild );
            }
            return hNewItem;
    }
     
    void CMyTreeCtrl::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
    	*pResult = 0;
     
    	m_hitemDrag = pNMTreeView->itemNew.hItem;
    	m_hitemDrop = NULL;
     
    	m_pDragImage = CreateDragImage(m_hitemDrag);  // get the image list for dragging
    	// CreateDragImage() returns NULL if no image list
    	// associated with the tree view control
    	if( !m_pDragImage )
    		return;
     
    	m_bLDragging = TRUE;
    	m_pDragImage->BeginDrag(0, CPoint(-15,-15));
    	POINT pt = pNMTreeView->ptDrag;
    	ClientToScreen( &pt );
    	m_pDragImage->DragEnter(NULL, pt);
    	SetCapture();
    }
     
    void CMyTreeCtrl::OnMouseMove(UINT nFlags, CPoint point) 
    {
    	HTREEITEM	hitem;
    	UINT		flags;
     
    	if (m_bLDragging)
    	{
    		POINT pt = point;
    		ClientToScreen( &pt );
    		CImageList::DragMove(pt);
    		if ((hitem = HitTest(point, &flags)) != NULL)
    		{
    			CImageList::DragShowNolock(FALSE);
    			SelectDropTarget(hitem);
    			m_hitemDrop = hitem;
    			CImageList::DragShowNolock(TRUE);
    		}
    	}
     
    	CTreeCtrl::OnMouseMove(nFlags, point);
    }
     
    void CMyTreeCtrl::OnLButtonUp(UINT nFlags, CPoint point) 
    {
    	CTreeCtrl::OnLButtonUp(nFlags, point);
     
    	if (m_bLDragging)
    	{
    		m_bLDragging = FALSE;
    		CImageList::DragLeave(this);
    		CImageList::EndDrag();
    		ReleaseCapture();
     
    		delete m_pDragImage;
     
    		// Remove drop target highlighting
    		SelectDropTarget(NULL);
     
    		if( m_hitemDrag == m_hitemDrop )
    			return;
     
    		// If Drag item is an ancestor of Drop item then return
    		HTREEITEM htiParent = m_hitemDrop;
    		while( (htiParent = GetParentItem( htiParent )) != NULL )
    		{
    			if( htiParent == m_hitemDrag ) return;
    		}
     
    		Expand( m_hitemDrop, TVE_EXPAND ) ;
     
    		HTREEITEM htiNew = CopyBranch( m_hitemDrag, m_hitemDrop, TVI_LAST );
    		DeleteItem(m_hitemDrag);
    		SelectItem( htiNew );
    	}
     
    }

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Mars 2008
    Messages
    16
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Mars 2008
    Messages : 16
    Par défaut
    Salut !

    J'aimerais avoir un peu plus de précisions à propos de ton code.
    Si j'ai bien compris, ces deux fichiers représentent une classe CMyTreeCtrl et on fait hériter CTreeCtrl de cette nouvelle classe?
    Est-on d'accord sur le fait qu'il manque des définitions de fonctions a ton .h ? Du genre void OnLButtonUp(UINT nFlags, CPoint point); ou void OnLButtonUp(UINT nFlags, CPoint point); ?

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    147
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 147
    Par défaut
    Tout à fait.
    Je n'ai mis que l'essentiel, le reste étant très facile à faire.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème de Drag'n'Drop dans un SWF embarqué
    Par Jay00 dans le forum Flex
    Réponses: 5
    Dernier message: 13/08/2007, 14h26
  2. Drag and drop dans un input
    Par kriekbellevue dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 25/12/2006, 20h55
  3. [VB.net] Drag and drop dans une Treeview
    Par gégécap dans le forum Windows Forms
    Réponses: 2
    Dernier message: 19/10/2006, 10h05
  4. [VB.NET]Drag and Drop dans une Listview
    Par gégécap dans le forum Windows Forms
    Réponses: 5
    Dernier message: 23/08/2006, 18h41
  5. Drag and drop dans un TTreeView
    Par BigBenQ dans le forum C++Builder
    Réponses: 3
    Dernier message: 07/10/2005, 14h57

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo