home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Now we loop over all the OLE items in the document to see whether one has been clicked by the mouse.

To loop over all the OLE items, we get a POSITION object calling the document’s GetStartPosition() function, then loop over all the items by calling the document’s GetNextItem() function.

void CContainerView::OnLButtonDown(UINT nFlags, CPoint point)
{
    CContainerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    m_pSelection = NULL;

    POSITION position = pDoc->GetStartPosition();                   ⇐

    while (position != NULL)                                        ⇐
    {
        CContainerCntrItem* CandidateItem = (CContainerCntrItem*)
pDoc->GetNextItem(position);                                        ⇐
               .
               .
               .

    }

We check whether the mouse location is in the current OLE item’s rect member using the CRect function PtInRect(). If the user did indeed click an OLE item, we put a pointer to that item into m_pSelection.

void CContainerView::OnLButtonDown(UINT nFlags, CPoint point)
{
    CContainerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    m_pSelection = NULL;

    POSITION position = pDoc->GetStartPosition();

    while (position != NULL)
    {
        CContainerCntrItem* CandidateItem = (CContainerCntrItem*)
pDoc->GetNextItem(position);
        if(CandidateItem->rect.PtInRect(point))                     ⇐
            m_pSelection = CandidateItem;                           ⇐
    }
    .
    .
    .

If, on the other hand, the user did not click any OLE item (that is, m_pSelection is left NULL), we want to deactivate the currently active item (that is, the item open for in-place editing) if there is one. We can find the currently active item with GetIn-PlaceActiveItem(). If an item is currently active, we deactivate it with Deactivate().

void CContainerView::OnLButtonDown(UINT nFlags, CPoint point)
{
    CContainerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    m_pSelection = NULL;

    POSITION position = pDoc->GetStartPosition();

    while (position != NULL)
    {
        CContainerCntrItem* CandidateItem = (CContainerCntrItem*)
pDoc->GetNextItem(position);
        if(CandidateItem->rect.PtInRect(point))
            m_pSelection = CandidateItem;
    }

    if(m_pSelection == NULL){                                       ⇐
        COleClientItem* ActiveItem = GetDocument()-
>GetInPlaceActiveItem(this);                                        ⇐
        if(ActiveItem != NULL)                                      ⇐
            ActiveItem->Deactivate();                               ⇐
    }

    CView::OnLButtonDown(nFlags, point);

}

Now that we’ve handled single clicks to close open items, we need to handle double clicks to open items in the first place.

Handling Double Clicks

We want to open an item for editing when the user double-clicks it. When you double-click an item, OnLButtonDown() is called for the first click, so m_pSelection is already set. All we need to do is to open that item with the OLE DoVerb() function if an item was indeed double-clicked.

void CContainerView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
    if(m_pSelection != NULL)                                        ⇐
        m_pSelection->DoVerb(OLEIVERB_SHOW, this);                  ⇐

    CView::OnLButtonDblClk(nFlags, point);

}

Here we use the OLE “verb” OLEIVERB_SHOW, which opens an item for in-place editing. If you want to open the item in the Server program, you would use OLEIVERB_OPEN instead.

Now that we have handled double clicks, run the Container program and select the Insert New Object item from the Edit menu. We use the Insert Object box to insert a new Microsoft Excel worksheet in the container, as shown in Figure 12.3.

The Excel worksheet is open for in-place editing, and we are able to enter text in it, as shown in Figure 12.3. Now click outside the OLE item in the view to deactivate that item, as shown in Figure 12.4. Double-clicking it again opens it for editing. Our Container program is a success, and we’re able to embed OLE items in containers. There’s more to add in OLE programming—for example, you could let the user position the new item in the view using the mousebut we’ve gotten a very good start.

The code for this example, CntrItem.h and CntrItem.cpp, appears in Listing 12.1; the code for ContainerView.h and ContainerView.cpp appears in Listing 12.2.


Figure 12.3  Embedding a Microsoft Excel worksheet in our OLE container.


Figure 12.4  Our new OLE item.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.