Solved

Drag and Drop Image

Posted on 2002-03-06
8
532 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:sabrog
  • 4
  • 3
8 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
Comment Utility
get the code from MSDN

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

GOOD LUCK
0
 

Author Comment

by:sabrog
Comment Utility
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
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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
 

Author Comment

by:sabrog
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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
 

Author Comment

by:sabrog
Comment Utility
Sounds good to me!  Can the white area behind the text be transparent?

Scott
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 200 total points
Comment Utility
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
 

Author Comment

by:sabrog
Comment Utility
Thank you, works great!

Scott
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

9 Experts available now in Live!

Get 1:1 Help Now