Du skal vist nok implementere funktionen \"OnDragOver\" på den klasse som er et DropTarget. Den sender et dataobject med, som nok er det du skal bruge...
\"vist nok\" er vist lidt vag. En COleDropTarget har følgende \"overrideables\" :
OnDragEnter Called when the cursor first enters the window. OnDragLeave Called when the cursor is dragged out of the window. OnDragOver Called repeatedly when the cursor is dragged over the window. OnDragScroll Called to determine whether the cursor is dragged into the scroll region of the window. OnDrop Called when data is dropped into the window, default handler. OnDropEx Called when data is dropped into the window, initial handler.
Jeg har fundet det her, måske det kan bruges, ellers skal jeg se dit kode :
MFC Support for OLE Drag-and-Drop Most of the work in writing OLE drag-and-drop code lies in implementing the COM objects. Fortunately, MFC will implement them for you. The same COleDataSource class that provides data objects for OLE clipboard operations works with OLE drag-and-drop, too. COleDropSource provides a handy implementation of the drop source object, and COleDropTarget provides the drop target object. Very often, you don\'t even have to instantiate COleDropSource yourself because COleDataSource does it for you. You will have to instantiate COleDropTarget, but you usually do that simply by adding a COleDropTarget member variable to the application\'s view class.
Suppose you\'d like to transfer a text string using OLE drag-and-drop in an MFC application. Here\'s how to do it using a global memory block as the storage medium:
DROPEFFECT de = ods.DoDragDrop (DROPEFFECT_MOVE | DROPEFFECT_COPY)
if (de == DROPEFFECT_MOVE) { // Delete the string from the document. }
This code is strikingly similar to the code presented earlier in this chapter that used COleDataSource to place a text string on the OLE clipboard. Other than the fact that the COleDataSource object is created on the stack rather than on the heap (which is correct because, in this case, the object doesn\'t need to outlive the function that created it), the only real difference is that COleDataSource::DoDragDrop is called instead of COleDataSource::SetClipboard. COleDataSource::DoDragDrop is a wrapper around the API function of the same name. In addition to calling ::DoDragDrop for you, it also creates the COleDropSource object whose IDropSource interface pointer is passed to ::DoDragDrop.
If you\'d rather create your own COleDropSource object, you can do so and pass it by address to COleDataSource::DoDragDrop in that function\'s optional third parameter. The only reason to create this object yourself is if you want to derive a class from COleDropSource and use it instead of COleDropSource. Programmers occasionally derive from COleDropSource and override its GiveFeedback and QueryContinueDrag member functions to provide custom responses to the IDropSource methods of the same names.
MFC makes acting as a target for OLE drag-and-drop data transfers relatively easy, too. The first thing you do is add a COleDropTarget data member to the application\'s view class:
// In CMyView\'s class declaration COleDropTarget m_oleDropTarget;
Then, in the view\'s OnCreate function, you call COleDropTarget::Register and pass in a pointer to the view object:
m_oleDropTarget.Register (this);
Finally, you override the view\'s OnDragEnter, OnDragOver, OnDragLeave, and OnDrop functions or some combination of them. These CView functions are coupled to the similarly named IDropTarget methods. For example, when the drop target object\'s IDropTarget::Drop method is called, COleDropTarget::OnDrop calls your view\'s OnDrop function. To respond to calls to IDropTarget::Drop, you simply override CView::OnDrop.
Here\'s an example that demonstrates how to override OnDragEnter, OnDragOver, and OnDrop in a CScrollView-derived class to make the view a drop target for text. OnDragLeave isn\'t overridden in this example because nothing special needs to be done when it\'s called. Notice that a preallocated COleDataObject is provided in each function\'s parameter list. This COleDataObject wraps the IDataObject pointer passed to the drop target\'s IDropTarget methods:
BOOL CMyView::OnDrop (COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point) { CScrollView::OnDrop (pDataObject, dropEffect, point); HANDLE hData = pDataObject->GetGlobalData (CF_TEXT); if (hData != NULL) { // Copy the string from the global memory block.
. . . ::GlobalFree (hData); return TRUE; // Drop succeeded. } return FALSE; // Drop failed. }
This code looks a lot like the non-MFC version presented in the previous section. OnDragEnter and OnDragOver call COleDataObject::IsDataAvailable through the pointer provided in their parameter lists to determine whether text is available. If the answer is no, both functions return DROPEFFECT_NONE to indicate that they won\'t accept the drop. The drop source, in turn, will probably display a \"no-drop\" cursor. If text is available, OnDragEnter and OnDragOver return either DROPEFFECT_MOVE or DROPEFFECT_COPY, depending on whether the Ctrl key is down. OnDrop uses COleDataObject::GetGlobalData to retrieve the data when a drop occurs.
Jeg tror desuden, at du skal sørge for, at kopiere det \"droppede\" over i en \"lokal\" streng. Når du kalder GlobalFree frigiver du jo memory, som igen kan overskrives...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.