Avatar billede nolle_k Nybegynder
25. april 2001 - 09:37 Der er 15 kommentarer og
1 løsning

Drag and Drop

Er der nogle, der har erfaring med Drag and Drop med MFC\'s COleDropTarget???

Jeg har problemer med at få fat i den information, der blive Dropped!!!

//Nolle_K
Avatar billede wisen Nybegynder
25. april 2001 - 09:41 #1
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...
Avatar billede nolle_k Nybegynder
25. april 2001 - 09:43 #2
Ja OK!! Jeg er ikke helt idiot!! Så lang er jeg også kommet! Mid problem ligger i at få dataene ud!!!
Avatar billede wisen Nybegynder
25. april 2001 - 09:44 #3
\"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.
Avatar billede privaten Nybegynder
25. april 2001 - 09:44 #4
Jeg har da leget lidt med det, hvad er problemet?
Avatar billede nolle_k Nybegynder
25. april 2001 - 09:46 #5
Jo nu skal i høre!!

BOOL CInfoAreaDropTarget::OnDrop(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)
{
    if(pDataObject->IsDataAvailable(CF_TEXT))
    {
        STGMEDIUM data;           
        //HGLOBAL hData;
       
        if(pDataObject->GetData(CF_TEXT, &data))
        {   
            switch (data.tymed)
            {
                case TYMED_HGLOBAL:
                    {
                        void* str = data.hGlobal;
//                        TRACE((const char) str);
                    }
                    break;
                case TYMED_GDI:
                        break;                   
                case TYMED_MFPICT:
                        break;
                case TYMED_ENHMF :
                        break;
                case TYMED_FILE:
                        break;
                case TYMED_ISTREAM:
                        break;
                case TYMED_ISTORAGE:
                        break;
            }
        }
       
    }
       
       


    return COleDropTarget::OnDrop(pWnd, pDataObject, dropEffect, point);
    //return DROPEFFECT_MOVE;
}


Hvordan får jeg den tekst ud jeg skal bruge?? Jeg dropper foreksempel \"Nik\" men modtager \"|\"!! Jeg fatter intet!!

/
Avatar billede privaten Nybegynder
25. april 2001 - 09:46 #6
Hmmm.. jeg kom vist lidt sent igang og fik afbrudt jeres \"samtale\"...
Avatar billede nolle_k Nybegynder
25. april 2001 - 09:47 #7
I orden!!! Al hjælp er ønsket!!
Avatar billede wisen Nybegynder
25. april 2001 - 10:42 #8
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:

char szText[] = \"Hello, world\";
HANDLE hData = ::GlobalAlloc (GMEM_MOVEABLE, ::lstrlen (szText) + 1)
LPSTR pData = (LPSTR) ::GlobalLock (hData);
::lstrcpy (pData, szText);
::GlobalUnlock (hData);

COleDataSource ods;
ods.CacheGlobalData (CF_TEXT, hData);

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:

DROPEFFECT CMyView::OnDragEnter (COleDataObject* pDataObject,
    DWORD dwKeyState, CPoint point)
{
    CScrollView::OnDragEnter (pDataObject, dwKeyState, point);
    if (!pDataObject->IsDataAvailable (CF_TEXT))
        return DROPEFFECT_NONE;
    return (dwKeyState & MK_CONTROL) ?
        DROPEFFECT_COPY : DROPEFFECT_MOVE;
}

DROPEFFECT CMyView::OnDragOver (COleDataObject* pDataObject,
    DWORD dwKeyState, CPoint point)
{
    CScrollView::OnDragOver (pDataObject, dwKeyState, point);
    if (!pDataObject->IsDataAvailable (CF_TEXT))
        return DROPEFFECT_NONE;
    return (dwKeyState & MK_CONTROL) ?
        DROPEFFECT_COPY : DROPEFFECT_MOVE;
}

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.

Avatar billede nolle_k Nybegynder
25. april 2001 - 12:25 #9
Problemet er at når jeg gør dette så får jeg ikke det jeg dropper!! Det er det jeg synes er mærkeligt!
Avatar billede wisen Nybegynder
25. april 2001 - 12:34 #10
Kan du ikke poste dit kode, så jeg kan se hvad du gør ?
Avatar billede nolle_k Nybegynder
25. april 2001 - 12:44 #11
Har postet det en gang men her er den igen!

BOOL CInfoAreaDropTarget::OnDrop(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)
{
    BOOL ret;
   
    ret = COleDropTarget::OnDrop(pWnd, pDataObject, dropEffect, point);
    if(pDataObject->IsDataAvailable(CF_TEXT))
    {
        HGLOBAL hData;           
       
        if(hData = pDataObject->GetGlobalData(CF_TEXT))
        {   
            void* str = hData;
      ::GlobalFree (hData);
      return TRUE;    // Drop succeeded.
   
        }
       
    }
       
       
    return ret;
   
   
}
Avatar billede nolle_k Nybegynder
25. april 2001 - 14:36 #12
I behøver ikke lede mere venner!

Fand løsningen!

Jeg skal i det kald til der hedder
void* str = hData benytter GlobalLock dvs

char* str = (char*)GlobalLock(hData)
Avatar billede wisen Nybegynder
25. april 2001 - 14:41 #13
Tillykke med det :)

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

/wisen
Avatar billede nolle_k Nybegynder
25. april 2001 - 15:08 #14
Tak!

Og OK!!
Avatar billede wisen Nybegynder
30. april 2001 - 07:58 #15
Husk nu at lukke spg.
Avatar billede nolle_k Nybegynder
30. april 2001 - 08:58 #16
OK!
Avatar billede Ny bruger Nybegynder

Din løsning...

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.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester