Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Drag and drop in CTreeCtrl

626 views
Skip to first unread message

Tony Drumm

unread,
Jan 2, 1999, 3:00:00 AM1/2/99
to
I am trying to use the drag and drop features of a CTreeCtrl to allow the user
to move child items from one parent to another. I actually have this working,
but I am not getting a drag image as the MFC docs leads me to believe I should.

Here are some snipets.

This begins the drag operation:

void DialogTree::OnBegindragItemTree(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;

// Make the item under the cursor the selected item if it isn't already.
POINT CursorPosition( pNMTreeView->ptDrag );
mDragItem = mItemTree.HitTest( CursorPosition );
if( !mDragItem )
return;

mItemTree.SelectItem( mDragItem );

// Create the image list for the drag operation.
mDragImageList = mItemTree.CreateDragImage( mDragItem );

mDragImageList->BeginDrag( 0, CPoint( 0, 0 ) );
SetCapture();

*pResult = 0;
} // OnBegindragItemTree


Here's the mouse movement code:

void DialogTree::OnMouseMove(UINT nFlags, CPoint point)
{
// Check if we are performing a drag operation.
if( mDragImageList )
{
mDragImageList->DragMove( point );
point -= mTreeWindowOffset;

UINT Flags;
mDropItem = mItemTree.HitTest( point, &Flags );

// Test for a valid drop target - must be one of the top-level items (i.e.
has no parent).
if( mDropItem
&& ( Flags & TVHT_ONITEM )
&& !mItemTree.GetParentItem( mDropItem ) )
{
// Show the item as a drop target.
mItemTree.SelectDropTarget( mDropItem );
return;
}
// Set drop item to null to indicate no valid drop item.
mDropItem = 0;
}

// If not a drag operation, just handle using the default.
CDialog::OnMouseMove(nFlags, point);
} // OnMouseMove


Finally, here is the code to handle the left button raise:

void DialogTree::OnLButtonUp(UINT nFlags, CPoint point)
{
// Check if we are performing a drag operation.
if( mDragImageList )
{
if( mDropItem )
{
// ... move the item from original parent to new parent...
}

// Finish the drag operation and destroy the drag image list.
mDragImageList->EndDrag();
delete mDragImageList;
mDragImageList = 0;
mItemTree.SelectItem( mDropItem );
mDropItem = 0;
ReleaseCapture();
return;
}
CDialog::OnLButtonUp(nFlags, point);
} // OnLButtonUp

As I said, this has the desired effect, and I can see the highlight move around
with the cursor, but I get no drag image. Note that I don't disable the cursor
as the examples show, since I then have nothing to follow visually.

Tony

Nathan Troxler

unread,
Jan 4, 1999, 3:00:00 AM1/4/99
to
I have not done this by myselft (yet), but I just compared with some code in
MSJournal 6/98 (German edition).

See comment below:

I guess the point to show the drag image is in nirwana!?
(I just solved a similar problem yesterday: My FormView seemed not
to get message "OnNotify" from a CTreeCtrl: Then I dedected, that
I calculatet the hit-Test point false. So the function returned thinking
the click was not over CTreeCtrl!!! Cost me about 2 days!)

Best regards

Nathan Troxler


Tony Drumm wrote in message <368E4756...@ibm.net>...


>I am trying to use the drag and drop features of a CTreeCtrl to allow the
user
>to move child items from one parent to another. I actually have this
working,
>but I am not getting a drag image as the MFC docs leads me to believe I
should.
>
>Here are some snipets.
>
>This begins the drag operation:
>
>void DialogTree::OnBegindragItemTree(NMHDR* pNMHDR, LRESULT* pResult)
>{
> NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
>
> // Make the item under the cursor the selected item if it isn't already.
> POINT CursorPosition( pNMTreeView->ptDrag );
> mDragItem = mItemTree.HitTest( CursorPosition );
> if( !mDragItem )
> return;
>
> mItemTree.SelectItem( mDragItem );
>
> // Create the image list for the drag operation.
> mDragImageList = mItemTree.CreateDragImage( mDragItem );
>

==>
Rect rect;
GetItemRect(hItem, rect, TRUE);

CPoint client(0,0);
Client to Sceen(&client);
GetWindowRect(rect);
point.x += client.x - rect.left;
point.y += client.y - rect.top;

> mDragImageList->BeginDrag( 0, CPoint( 0, 0 ) );

==> mDragImageList->DragEnter((this, point);

dgob...@erols.com

unread,
Jan 22, 1999, 3:00:00 AM1/22/99
to
CImageList::DragEnter and DragLeave is all you need. Pass the address of your
tree control to each and (0,0) to DragEnter in your BeginDrag handler and the
point parameter to DragEnter in OnMouseMove.

for example.

void x::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{
...
m_pDragImageList->BeginDrag( 0, CPoint(0,0));
m_pDragImageList->DragEnter( &m_Tree, CPoint(0,0));
...
}

void x::OnMouseMove(UINT nFlags, CPoint point)
{
if( m_bDragging)
{
// Allow tree to update itself (clear drop target stuff)
m_pDragImageList->DragLeave( &m_Tree);

// Drag the item to the current position of the mouse cursor.
m_pDragImageList->DragMove(point);

...

// Tree updated, so continue dragging
m_pDragImageList->DragEnter( &m_Tree, point);
}

COleControl::OnMouseMove(nFlags, point);
}

void x::OnLButtonUp(UINT nFlags, CPoint point)
{
If( m_bDragging)
{
m_pDragImageList->EndDrag();
delete m_pDragImageList;
m_pDragImageList = NULL;
...
}

COleControl::OnLButtonUp(nFlags, point);
}

<<Dan>>

In article <368E4756...@ibm.net>,


Tony Drumm <Tony...@ibm.net> wrote:
> I am trying to use the drag and drop features of a CTreeCtrl to allow the user
> to move child items from one parent to another. I actually have this working,
> but I am not getting a drag image as the MFC docs leads me to believe I
should.
>
> Here are some snipets.
>
> This begins the drag operation:
>
> void DialogTree::OnBegindragItemTree(NMHDR* pNMHDR, LRESULT* pResult)
> {
> NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
>
> // Make the item under the cursor the selected item if it isn't already.
> POINT CursorPosition( pNMTreeView->ptDrag );
> mDragItem = mItemTree.HitTest( CursorPosition );
> if( !mDragItem )
> return;
>
> mItemTree.SelectItem( mDragItem );
>
> // Create the image list for the drag operation.
> mDragImageList = mItemTree.CreateDragImage( mDragItem );
>

> mDragImageList->BeginDrag( 0, CPoint( 0, 0 ) );

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

0 new messages