How drag and drop of files is done?

后端 未结 6 1730
刺人心
刺人心 2021-01-13 07:29

as in winamp or vlc player, how to do a file drag and drop? i mean i want to know what sort of coding goes into application? i want to know for c++

6条回答
  •  死守一世寂寞
    2021-01-13 08:27

    With com:

    Create a class that public extends IDropTarget

    Register your class for drops. Do this in WM_CREATE

    RegisterDragDrop(hwnd,static_cast(pointer_to_your_class));
    

    In your class you need to override a couple of functions since they are pure virtual:

    virtual HRESULT STDMETHODCALLTYPE DragEnter( 
            /* [unique][in] */ __RPC__in_opt IDataObject *pDataObj,
            /* [in] */ DWORD grfKeyState,
            /* [in] */ POINTL pt,
            /* [out][in] */ __RPC__inout DWORD *pdwEffect) = 0;
    
    virtual HRESULT STDMETHODCALLTYPE DragOver( 
        /* [in] */ DWORD grfKeyState,
        /* [in] */ POINTL pt,
        /* [out][in] */ __RPC__inout DWORD *pdwEffect) = 0;
    
    virtual HRESULT STDMETHODCALLTYPE DragLeave( void) = 0;
    
    virtual HRESULT STDMETHODCALLTYPE Drop( 
        /* [unique][in] */ __RPC__in_opt IDataObject *pDataObj,
        /* [in] */ DWORD grfKeyState,
        /* [in] */ POINTL pt,
        /* [out][in] */ __RPC__inout DWORD *pdwEffect) = 0;
    

    Each of those functions will get called when those events occur, i.e. when someone pass the mouse in your window with a file DragEnter on your class will get called.

    You will also need to implement a couple more functions that IDropTarget extends, check out IUnknown in your MSDN.

    Then you need to query IDataObject param to get the data:

    FORMATETC fdrop = {CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
    
    if (SUCCEEDED(pDataObj->QueryGetData(&fdrop)) ){
        STGMEDIUM stgMedium = {0};
        stgMedium.tymed = TYMED_HGLOBAL;
        HRESULT hr = pDataObj->GetData(&fdrop, &stgMedium);
        if (SUCCEEDED(hr))
        {
            HGLOBAL gmem = stgMedium.hGlobal;
            HDROP hdrop = (HDROP)GlobalLock(gmem);
            UINT numOfFiles =  DragQueryFile( (HDROP) hdrop,
                                0xFFFFFFFF,
                               NULL,
                                0
                            );
    
            TCHAR buffer[MAX_PATH];
            for( int i=0;i

    Cheers!

提交回复
热议问题