Solved

Drag & drop between views: a better way needed

Posted on 2006-06-21
12
630 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:mrwad99
  • 6
  • 5
12 Comments
 
LVL 22

Expert Comment

by:mahesh1402
Comment Utility
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
 
LVL 19

Author Comment

by:mrwad99
Comment Utility
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
 
LVL 22

Assisted Solution

by:mahesh1402
mahesh1402 earned 250 total points
Comment Utility
>>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
 
LVL 22

Expert Comment

by:mahesh1402
Comment Utility
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
 
LVL 22

Expert Comment

by:mahesh1402
Comment Utility
>>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
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 250 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:mahesh1402
Comment Utility
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
 
LVL 22

Expert Comment

by:mahesh1402
Comment Utility
Read as  'WindowFromPoint' and NOT GetWindowFromPoint
0
 
LVL 19

Author Comment

by:mrwad99
Comment Utility
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
 
LVL 19

Author Comment

by:mrwad99
Comment Utility
Will be back to this question soon, thanks again for the patience :)
0
 
LVL 19

Author Comment

by:mrwad99
Comment Utility
Coming back to this by the end of the week...
0
 
LVL 19

Author Comment

by:mrwad99
Comment Utility
There will be a follow up to this posted shortly.

Thanks for the help.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now