Drag and Drop Image

I've implemented a drag and drop in my application.  I use a standard cursor to show that you are in a drag - drop mode.  

But I would like to display a image of the items being dragged,  simalar to how Explorer does it.  Can someone point me in the correct direction.

Thanks
Scott
sabrogAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
DanRollinsConnect With a Mentor Commented:
Here ya go!

//---------------------------------
// pass in a window and a rect (in window coords) and a bitmap
// returns with the bitmap containing the image from that part of that window
//
void CreateBitmapFromWindowRect( CWnd* pWnd, CRect* prc, CBitmap* pcBmp )
{
     CWindowDC     dc( pWnd );
     CDC           memDC;
     CRect          rect;

     memDC.CreateCompatibleDC( &dc );
     pWnd->GetWindowRect(rect);

     pcBmp->CreateCompatibleBitmap( &dc, rect.Width(),rect.Height() );

     CBitmap* pOldBitmap = memDC.SelectObject( pcBmp );
     memDC.BitBlt( 0,0, prc->Width(),prc->Height(), &dc, prc->left, prc->top, SRCCOPY);

     memDC.SelectObject(pOldBitmap);
}

//--------------------------------------------
// example usage.  You will need to fire on a different
// event (i.e., start of drag of your custom header control)
// and do other customization for your needs

void CMyDlg::OnWhatever( ... whatever ... )
{
     CHeaderCtrl* pHC= m_ctlList.GetHeaderCtrl();

     int nClmNum= 1;   // you must set this based upon the clm that is being dragged
     int nHigh=   60;  // set this to the height of the grid control    

     //---------- here I calculate the location of the
     //---------- rectangle to use as the drag image
     CRect rcHdr;  
     pHC->GetItemRect( nClmNum, &rcHdr );

     CBitmap cBmp;
     CRect rcBmp;

     rcBmp.top= 0;
     rcBmp.left=  rcHdr.left;
     rcBmp.right= rcHdr.right + 2; // cosmetic tweak
     rcBmp.bottom= nHigh;    
     
     CreateBitmapFromWindowRect( &m_ctlList, &rcBmp, &cBmp );

     //------------------- delete the old drag image and create the new
     if( m_pImgListDrag ) {
          delete m_pImgListDrag;
     }
     m_pImgListDrag= new CImageList();

     m_pImgListDrag->Create( rcBmp.Width(), rcBmp.Height(), ILC_MASK, 1, 1);
     m_pImgListDrag->Add( &cBmp, RGB( 255,255,255) ); // make white transparent!

     //--------- you do the rest... this is what i used for testing
     
     m_pImgListDrag->BeginDrag( 0, CPoint(8, 8) );
     m_pImgListDrag->DragEnter( GetDesktopWindow(), pNMListView->ptAction );

     m_fDragging= TRUE;
     SetCapture (); // capture all mouse messages
}

//----------------- more of my testing code.
//----- yours will be different

void CD01Dlg::OnMouseMove(UINT nFlags, CPoint point)
{
     if ( m_fDragging ) {
          CPoint pt(point);
          CWnd* pwndDraggingOver= WindowFromPoint(pt);
          ClientToScreen( &pt );
          m_pImgListDrag->DragMove(pt);            // move the drag image
          m_pImgListDrag->DragShowNolock( FALSE ); // unlock window updates

          m_pImgListDrag->DragShowNolock( TRUE ); // show only the drag updates
     }
     
     CDialog::OnMouseMove(nFlags, point);
}

=-=-=-=-=-=-=-
You will need to set up the rectangle for the call into the CreateBitmapFromWindowRect() fn.  Note that it expects Window coordinates rather than screen coordinates (I think it is easier for you this way)

I did not do anything to make the drag image look 'dim' It looked great as is.  I guess the dimming is being done automatically by the DragMove or DragShowNoLock fn... I saw no need to monkey with it.

-- Dan
0
 
Roshan DavisCommented:
get the code from MSDN

http://www.microsoft.com/msj/0898/wicked0898.htm

GOOD LUCK
0
 
sabrogAuthor Commented:
that's not quite what i need.  The problem is creating the drag image.  They use the function CreateDragImage, which is a function of cTreeCtrl.  

I'm dragging from a custom grid control so i need to create dynamic Bitmap to add to the CImageList from a specified rect of the screen.

Scott
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
DanRollinsCommented:
Is it possible that the 'custom grid control' is derived from CListView?  If so, you can send it the LVM_CREATEDRAGIMAGE message.

Otherwise, you will need to create a bitmap of the text that you want to drag.  Then create a persistent  CImageList that contains just that one bitmap.  Then call
 
      m_cImgListDrag.SetDragCursorImage(0,...)

Then continue with the normal dragging operations.
      m_cImgListDrag.BeginDrag(...)
      m_cImgListDrag.DragEnter(...)
...etc.

Do you need help creating the bitmap?  Raise the points to 200 and I'll work out the details for you.  I'll provide a function that accepts a string of text and creates a bitmap that contains that text.

-- Dan
0
 
sabrogAuthor Commented:
Yes creating the bitmap is what i need help with. I understand how to use the CImageList and move the image around, getting that image is what I'm not sure on

The dragging is for moving columns around, not items in the list box.  For the bitmap i want to show the header cell and 4-5 rows of text.  Can we create a bitmap of a specified rect on the screen, to include the look of the header and not just text?

unfortunately the list box is not derived from CListView, it's totally custom.

Thanks
Scott
0
 
DanRollinsCommented:
Just so that I understand it...

The user has clicked on a header 'button' at the top of the grid and is now dragging that header around the screen.  You want to show a shadowy image that looks kind of like the image of the header button and the data in the several visible columns that are directly below that header.

So you should be satisfied with a drag image that looks like a particular rectangle on the screen.  

It will be your responsibility to identify the desired rectangle in global screen coordinates.  I will capture the contents of that rectangle and dither it so that it looks kind of shadowy.  I'll put the result into a CImageList so that it will work with the normal drag-and-drop handling.  You will implement all of the drag-and-drop code.  You will award me an A for my efforts and not throw in a bunch of unrelated post-solution questions.

Are we agreed?
0
 
sabrogAuthor Commented:
Sounds good to me!  Can the white area behind the text be transparent?

Scott
0
 
sabrogAuthor Commented:
Thank you, works great!

Scott
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.