Drag & drop between views: a better way needed

Ah hello.

I have an app that supports drag/drop from a tree control.  I also have a part of the same app that supports drag drop from a list view into the tree control.

What I want is some sort of generic treeview/listview that supports dragging and dropping.  Currently, in the list view, I do this:

void CFileView::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{      
      m_bIsDragging = TRUE;
      // Create drag image and display it
}

void CFileView::OnMouseMove(UINT nFlags, CPoint point)
{
      UINT uFlags;

      m_bSafeDropTarget = FALSE;

      if (m_bIsDragging)
      {
            CPoint pt(point);    
            ClientToScreen(&pt);

            CMyTargetView* pTargetView = NULL;
            m_pDragImage->DragMove(pt);
            m_pDropWnd = WindowFromPoint(pt);

            if ((m_pDropWnd->IsKindOf (RUNTIME_CLASS (CMyTargetView)))) // CMyTargetView is CTreeView derived
            {
                  // Valid drop window, handle this
                  m_bSafeDropTarget = TRUE;

                  // etc...
            }
            else
            {
                  // Invalid drop window, handle this.  
                  // Maybe display some sort of cursor to indicate invalid drop target etc                        }
      }
}

I don't like this; all the code is bundled up, and what if I want to add different drop target types ?

There must be a better way of doing this.

Please can someone point me at an example, or list some better code.

TIA
LVL 19
mrwad99Asked:
Who is Participating?
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
The message I proposed (in the previous question) was to determine if the recipient was a valid drop target.  (I have a situation for example that some items can NOT be dropped onto the root item or vice versa - ONLY to the root item).



Generic routines - this technique should work and be fairly general.

class CDragDropTree : public CTreeCtrl, public CMyDragObject
{
....

and
class CDragDropList : public CListCtrl, public CMyDragObject
{
....


now you implement a suite of functions in you class CMyDragObject that give you the common functionality you desire.
0
 
mahesh1402Commented:
Refer following link 'Enhanced Drag and Drop between CListCtrl and CTreeCtrl'
http://www.codeguru.com/cpp/controls/treeview/dragdrop/comments.php/c697/?thread=7800 <====

you may also have look at no. of other articles here regarding this :
http://www.codeguru.com/cpp/controls/treeview/dragdrop/article.php/c697/
http://www.codeguru.com/cpp/controls/treeview/dragdrop/

-MAHESH
0
 
mrwad99Author Commented:
MAHESH

Thanks.  Those links really deal with dragging and dropping between the same tree control.  Although they give some nice pointers on separating out common functionality, that is not really what I am looking for.

I need to deal with dragging from *one tree control onto another*.  Not dragging and dropping within the *same* control.

I asked a similar question before at http:Q_21313497.html - but left it alone back then as another task came up.  Andy mentions passing a custom message to the drop target via SendMesssage.

What do you think of this ?

TIA
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
mahesh1402Connect With a Mentor Commented:
>>I need to deal with dragging from *one tree control onto another*.

Better to implement 'OLE Drag and Drop' based on COleDropTarget class.
Have look at this for reference 'Drag text between edit controls'
http://www.codeguru.com/cpp/controls/editctrl/article.php/c535/ <==

Also 'Drag and Drop between Tree controls'
http://www.codeguru.com/cpp/controls/treeview/dragdrop/article.php/c659/

-MAHESH
0
 
mahesh1402Commented:
I need to deal with dragging from *one tree control onto another*.  Not dragging and dropping within the *same* control.<<

Actually sample 'MFCDRAG' shows how to implement drag and drop between a CListCtrl and CTreeCtrl

http://support.microsoft.com/kb/q148738/ <== CListCtrl to CTreeCtrl, CListCtrl to CListCtrl, CTreeCtrl to CListCtrl, CTreeCtrl to CTreeCtrl ...

-MAHESH
0
 
mahesh1402Commented:
>>I want to add different drop target types ?

Detect Begin Drag in the Tree Control. There you initialise whatever you need to for your drag and drop operation.
Then you give mouse control to the parent. In your main class you'll have your OnMouseMove handler, which, if dragging is in progress, will look after the actual dragging. In the ::OnLButtonUp handler, again, if dragging is active, first release the mouse by ReleaseCapture( ) then check if the mouse is positioned over the relevant destination control. If not, do nothing or give some feedback; if it is over the desired control, you should have that control handle the event.

So relay the event to the control using SendMessage or else simply call a routine defined in the control's class that handles the drop operation.

-MAHESH
0
 
mahesh1402Commented:
mrwad99,
One more thing when you drop target and you want to verify its a valid window you may effectively use 'GetWindowFromPoint()' function... something like  :

void CMyView::OnLButtonUp((UINT nFlags, CPoint point)
{
      ....

     //GET THE WINDOW UNDER THE DROP POINT
        GetApp()->cDropPoint = point;
        ClientToScreen(&GetApp()->cDropPoint);
        GetApp()->cpDropWnd = WindowFromPoint(GetApp()->cDropPoint);

     //Verification code  
        .....
}

Refer :
Drag and drop between views in a splitter window
http://www.codeguru.com/cpp/w-d/splitter/article.php/c1545/ <=== this demonstrate drag & drop operation within an explorer style project splitter window with left hand treeView and right hand listView !

-MAHESH
0
 
mahesh1402Commented:
Read as  'WindowFromPoint' and NOT GetWindowFromPoint
0
 
mrwad99Author Commented:
Thanks for the info so far.  I have not finished with this yet, so will leave it open until I have all the info I need.  
0
 
mrwad99Author Commented:
Will be back to this question soon, thanks again for the patience :)
0
 
mrwad99Author Commented:
Coming back to this by the end of the week...
0
 
mrwad99Author Commented:
There will be a follow up to this posted shortly.

Thanks for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.