Solved

Drag & drop between views: a better way needed

Posted on 2006-06-21
12
633 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
ID: 16957535
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
ID: 16958991
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
ID: 16959901
>>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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:mahesh1402
ID: 16959969
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
ID: 16960046
>>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
ID: 16962517
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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16969284
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
ID: 16969297
Read as  'WindowFromPoint' and NOT GetWindowFromPoint
0
 
LVL 19

Author Comment

by:mrwad99
ID: 17026916
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
ID: 17071803
Will be back to this question soon, thanks again for the patience :)
0
 
LVL 19

Author Comment

by:mrwad99
ID: 17176809
Coming back to this by the end of the week...
0
 
LVL 19

Author Comment

by:mrwad99
ID: 17241424
There will be a follow up to this posted shortly.

Thanks for the help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
while loop over for loop 7 115
Execute multiple curl cmds with sleep and send output to file 10 104
Line meaning 9 88
How do I get Window Title of all opened process? 4 15
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

821 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