Step 4: Support Drag-and-Drop

Your designer can support dragging and dropping of toolbox items onto its surface by supporting the IDropTarget interface.

  1. Inherit from IDesignerToolboxImpl.

    The IDesignerToolboxImpl class provides default support for the IDesignerToolbox interface. To use this class, set up inheritance for your designer use the COM_INTERFACE_ENTRY macro in the COM_ENTRY_MAP to map IDesignerToolbox to IDesignerToolboxImpl. See the Active Template Library for details.

  2. Inherit from IDesignerDropTargetImpl.

    Default drop target support for Drag & Drop is available through the IDesignerDropTargetImpl class. Inheriting from this class and adding COM_INTERFACE_ENTRY (IDropTarget) to the COM_ENTRY_MAP supplies default handling and validation of all toolbox items specified in the toolbox item map.

    Use the COM_INTERFACE_ENTRY_IMPL_IID macro to map IDropTarget to IDesignerDropTargetImpl as follows:

    BEGIN_COM_MAP(…)
    …
    COM_INTERFACE_ENTRY_IMPL_IID(IID_IDropTarget,IDesignerDropTarget)
    …
    END_COM_MAP()
    
  3. In addition to inheriting from IDesignerDropTargetImpl, the designer must call CComDesignerToolbox::OnShowDesigner whenever the designer window is revealed or hidden. The OnShowDesigner method registers the window as a drag&drop target when the window is revealed, and removes it as a drag&drop target when the window is hidden.

    The ideal place to call OnShowDesigner is in the WM_SHOWWINDOW handler, as follows:

    LRESULT CShapesDsr::OnShowWindow(UINT nMsg, WPARAM  wParam, LPARAM lParam , BOOL& bHandled )
    {
        HRESULT hr = E_FAIL;
        BOOL fShow = wParam;
        hr = OnShowDesigner(m_hWndCD,fShow);
        if(hr != S_OK)
            return(1);
        else
            return(0);
    }
    

Setting up the toolbox item map, using the IDesignerDropTarget base class, and calling the CComDesignerToolbox::OnShowDesigner method from the WM_SHOWWINDOW override provide drag & drop support for a designer.

See Also

Step 5: Support Click-and-Drag