The AppWizard-provided starter application initially supports only one embedded object. This part of the tutorial adds support for multiple objects by implementing hit testing and selection. Hit testing determines which of the multiple objects lies at a given point.
In the next two procedures, you’ll use ClassView to add two helper functions to class CContainerView
: HitTestItems
and SetSelection
. These functions implement hit testing and selection.
To implement hit testing
CContainerView
class icon.The Add Member Function dialog box appears.
CContainerItem*
. HitTestItems(CPoint point)
ClassWizard adds the declaration to the header file and creates a starter definition in the implementation file.
HitTestItems
: CContainerDoc* pDoc = GetDocument();
CContainerItem* pItemHit = NULL;
POSITION pos = pDoc->GetStartPosition();
while (pos != NULL)
{
CContainerItem* pItem = (CContainerItem*)pDoc->GetNextItem(pos);
if (pItem->m_rect.PtInRect(point))
pItemHit = pItem;
}
return pItemHit; // return top item at point
To implement selection
void
.SetSelection(CContainerItem* pItem)
SetSelection
:// close in-place active item
if (pItem == NULL || m_pSelection != pItem)
{
COleClientItem* pActiveItem
= GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL && pActiveItem != pItem)
pActiveItem->Close();
}
Invalidate();
m_pSelection = pItem;
The above implementation is “lazy” in that it invalidates the entire client area of the view whenever the selection changes. In Container Step 2, this implementation is replaced with smarter invalidation.