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


When the user changes the OLE item’s size or location when editing it, the OLE item appears changed in the Container program. However, when the OLE item is first inserted, we have yet to get its size directly from the OLE server, and we do that in the OnInsertObject() function, which is part of our container program’s View class.

The Container Program’s View

When the user inserts a new OLE item into the container’s view, we get the item’s true size and set the rect member correctly. When the new item is inserted, it’s made the currently selected OLE item, and a pointer to that item is stored in the m_pSelection member of the view. We add a new function to the OLE item’s class, SetSize(), to get the item’s true size and update the rect member correctly. We call SetSize() in OnInsertObject() right after the new OLE item has been inserted in the code in ContainerView.cpp.

void CContainerView::OnInsertObject()
{
    // Invoke the standard Insert Object dialog box to obtain information
    //  for new CContainerCntrItem object.
    COleInsertDialog dlg;
    if (dlg.DoModal(COleInsertDialog::DocObjectsOnly) != IDOK)
        return;

    BeginWaitCursor();

    CContainerCntrItem* pItem = NULL;
    TRY
    {
        // Create new item connected to this document.
        CContainerDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        pItem = new CContainerCntrItem(pDoc);
        ASSERT_VALID(pItem);

        // Initialize the item from the dialog data.
        if (!dlg.CreateItem(pItem))
            AfxThrowMemoryException();  // any exception will do
        ASSERT_VALID(pItem);

        // If item created from class list (not from file) then launch
        //  the server to edit the item.
        if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
            pItem->DoVerb(OLEIVERB_SHOW, this);

        ASSERT_VALID(pItem);

        // As an arbitrary user interface design, this sets the selection
        //  to the last item inserted.

        // TODO: reimplement selection as appropriate for your application

        m_pSelection = pItem;   // set selection to last inserted item

        m_pSelection->SetSize();                                  ⇐
              .
              .
              .

}

Now we create the SetSize() OLE item function in the OLE item’s class, CContainerCntrItem.

// CntrItem.h : interface of the CContainerCntrItem class
//
    .
    .
    .
// Attributes
public:
    CContainerDoc* GetDocument()
        { return (CContainerDoc*)COleClientItem::GetDocument(); }
    CContainerView* GetActiveView()
        { return (CContainerView*)COleClientItem::GetActiveView(); }

    CRect rect;
    void SetSize();                                                ⇐
        .
        .
        .

We add the SetSize() function to the end of CContainerCntrItem().

void CContainerCntrItem::SetSize()
{

}
To get the OLE item's new size, we can use its GetExtent() function, which
fills a CSize object:
void CContainerCntrItem::SetSize()
{
    CSize size;                                                     ⇐
    GetExtent(&size);                                               ⇐
        .
        .
        .

}

Unfortunately, this item’s size is returned in the usual units OLE items are measured inHIMETRIC units (HIMETRIC units are 0.01 millimeter), not in pixels. Because of this, we create a NULL device context and convert to pixels using the CDC HIMETRICtoDP() function.

void CContainerCntrItem::SetSize()
{
    CSize size;
    GetExtent(&size);

    CClientDC nullDC(NULL);                                         ⇐
    nullDC.HIMETRICtoDP(&size);                                     ⇐
        .
        .
        .

Finally, we update the rect member with the new size.

void CContainerCntrItem::SetSize()
{
    CSize size;
    GetExtent(&size);

    CClientDC nullDC(NULL);
    nullDC.HIMETRICtoDP(&size);

    rect.right = rect.left + size.cx;                              ⇐
    rect.bottom = rect.top + size.cy;                              ⇐

}

When the user inserts a new OLE item, that item appears with the size the OLE server has given it.

The last step in handling the OLE item’s true size is to modify OnDraw(), which currently draws the OLE item using a default size; we update that code to make use of the item’s true size this way, commenting out the original line of code:

void CContainerView::OnDraw(CDC* pDC)
{
    CContainerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    // TODO: add draw code for native data here
    // TODO: also draw all OLE items in the document

    // Draw the selection at an arbitrary position.  This code should be
    //  removed once your real drawing code is implemented.  This position
    //  corresponds exactly to the rectangle returned by CContainerCntrItem,
    //  to give the effect of in-place editing.

    // TODO: remove this code when final draw code is complete.

    if (m_pSelection == NULL)
    {
        POSITION pos = pDoc->GetStartPosition();
        m_pSelection = (CContainerCntrItem*)pDoc->GetNextClientItem(pos);
    }
    if (m_pSelection != NULL)
        //m_pSelection->Draw(pDC, CRect(10, 10, 210, 210));
        m_pSelection->Draw(pDC, m_pSelection->rect);                ⇐

}

Now we’ve handled the OLE item’s actual size and kept up with it as it changes sizes. There are two more operations to enable here: clicking outside the OLE item to deactivate it and double-clicking it to open it for editing. Let’s add those now.

Handling the Mouse

Our Container program can open OLE items in place for editing, just as any OLE container can. When the user clicks outside the OLE item, we want to deactivate that item if it’s open for editing, and we do that by adding OnLButtonDown() to our view.

void CContainerView::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default

}

From the view, we can reach the OLE items embedded in the document if we get a pointer to the document.

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

We want to determine whether any OLE item is being selected with the mouse, so we set the pointer to the currently selected OLE, m_pSelection, to NULL.

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

    m_pSelection = NULL;                                            ⇐
       .
       .
       .


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.