Solved

COleDropSource - How to implement

Posted on 1998-07-06
6
920 Views
Last Modified: 2008-02-01
I have written a control derived from CWnd and I'm trying to implement drag and drop between two instances of it.

I have tried adding a COleDropTarget derived member variable and calling member.Register(this) in the control's OnCreate handler. For some reason the Register function returns 0 (failure) and the overidden OnDrag functions don't get called!

Any ideas? According to the documentation, I can use a COleDropTarget in a non-CView derived class. Why is failing? What must I do?

Ignore that question - I figured it out - It was the AfxOleInit that was missing.

One more question...

How do you implement the COleDataSource part.
My data will be a text string for now - Max length about 250-500 characters.

I only need CF_TEXT format to start, but I'd like to know how to use my own data format too.

Cheers
0
Comment
Question by:richweed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 100 total points
ID: 1167241
I don't understand why you need to implement COleDropSource class. It wraps IDropInterface that allows you get feedback during drag and drop operation. You need to declare on stack where you want to implement drag and drop COleDataObject class instance. Then you need to create data object. Allocate memory block large enogh for your text data with GlobalAlloc. Lock it with GlobalLock. Copy text to allocated memory. Then call COleDataObject::CachGlobalData passing handle to global memro you allocated and locked. Next step is call to COleDataObject::DoDragDrop. Last default parameter is pointer to COleDropSource class. you can derive you own class from COleDropSource and override its virtual functions which will be called during drag and drop. DoDragDrop enters modal loop until drop occurs or drag and drop operation is cancelled.
Finally you need to unlock and free memory with GlobalUnlock and GlobalFree. If you want to use your own format you need first to register it with COleDataObject::RegisterClipboardFormat
0
 

Author Comment

by:richweed
ID: 1167242
Galkin
I'm not using a text string anymore, but my FVInfo data structure instead. (IMAGEBARINFO FVInfo)
Is this code correct?

      COleDataSource MyDataSource;
      HGLOBAL hMyBlock = GlobalAlloc(GMEM_FIXED, sizeof (IMAGEBARINFO));
      IMAGEBARINFO* pMyBlock = (IMAGEBARINFO*) GlobalLock(hMyBlock);

      CopyMemory(pMyBlock,&FVInfo,sizeof(FVInfo)); //Is this correct?
      MyDataSource.CacheGlobalData(m_cfIB_CBFormat,hMyBlock);
      MyDataSource.DoDragDrop(DROPEFFECT_COPY);

      GlobalUnlock(hMyBlock);
      GlobalFree(hMyBlock);

Do I use the same code for normal copy/paste operations? If not, how is it different? (besides not calling dodragdrop)
0
 
LVL 7

Expert Comment

by:galkin
ID: 1167243
Yes, you allocate global memory with GlobalAlloc for your structure, lock it with GlobalLock. given a pointer to allocated memory you copy your structure. Then you register custom clipboard format with RegisterClipboardFormat. After that you call CacheGlobalData and DoDragDrop.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:galkin
ID: 1167244
You code looks correct
0
 

Author Comment

by:richweed
ID: 1167245
Here is my drop code:

      HGLOBAL hMyBlock = pDataObject->GetGlobalData(m_cfIB_CBFormat);
      LPVOID pMyBlock = GlobalLock(hMyBlock);
      CopyMemory(&I,pMyBlock,sizeof(I));
      GlobalUnlock(hMyBlock);

Is thist correct? Do I have to free the memory block myself or is it done by the DataObject?



What about Copy/Paste Clipboard operations?

      COleDataSource MyDataSource;
      HGLOBAL hMyBlock = GlobalAlloc(GMEM_FIXED, sizeof (x));
      LPVOID pMyBlock = GlobalLock(hMyBlock);

      CopyMemory(pMyBlock,&x,sizeof(x));
      MyDataSource.CacheGlobalData(m_cfIB_CBFormat,hMyBlock);
      MyDataSource.SetClipboard;

      GlobalUnlock(hMyBlock);
      GlobalFree(hMyBlock);   Is it correct to free the memory block now?

0
 
LVL 7

Expert Comment

by:galkin
ID: 1167246
your code is correct and you have to unlock and free global memory
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to print into std::string like sprintf using C++11? 12 117
C++ mouse_event mouse look 7 111
Beginner to Unreal Engine 4 5 105
Indy 10 not Receiving UDP broadcast 3 43
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

756 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