Step 5: Support Click-and-Drag

Click & Drag is the standard way that Visual Basic places controls on a client area. CComDesignerToolbox provides a method with which a designer can obtain the clicked item as well as a method to let the container know that the clicked item has been consumed.

The following are the steps to support Click & Drag in a designer. Note that the designer does not have to inherit from IDesignerDropTarget.

  1. Set the toolbox support parameter in CComDesignerToolbox to either ISUPPORT_CLICKDRAG or ISUPPORT_BOTH.

  2. Handle Click & Drag operations.

You can find out whether a click and drag operation has occurred by monitoring mouse messages. CComDesignerToolbox provides easy click & drag validation through the CComDesignerToolbox::GetSelectedToolboxItem method.

This method is called when the Click & Drag conditions have been met. It checks with the IDesignerToolboxSite interface to see which item has been selected, then validates that item against the items in the Toolbox Item map, returning a TOOLBOXITEM structure that contains the item (if the Clicked item was valid). For example:

if(m_bCaughtLBD)
{
   hr = GetSelectedToolboxItem(&m_ptbItem);
   if(hr == S_OK)
   {
      //--------------------------------------------------
      // If a valid toolbox item is selected, then indicate
      // that we are currently dragging and draw the
      // first focus rectangle.
      //--------------------------------------------------
      m_bMouseDragging = TRUE;
      DrawFocusRect(GetDC(), &m_focusRect);
   }
   //--------------------------------------------------
   // For any single Click&Drag operation we only need
   // to verify the current toolbox item ONCE, so
   // set this flag to FALSE.
   //--------------------------------------------------
   m_bCaughtLBD = FALSE;
}

After handling the item (in this case, by drawing it on the designer surface), the designer calls CComDesignerToolbox::ObjectHasBeenPicked() to let the container know that it has successfully consumed the clicked item:

case WM_LBUTTONUP:
. . .
if(m_bMouseDragging)
{
//--------------------------------------------------
   // Call this method to notify the container that we
   // have processed a successful Click&Drag operation.
   //--------------------------------------------------
   if(SUCCEEDED(hr))
      ObjectHasBeenPicked();
}