![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
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 documents GetStartPosition() function, then loop over all the items by calling the documents 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 items 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 weve handled single clicks to close open items, we need to handle double clicks to open items in the first place. Handling Double ClicksWe 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 were able to embed OLE items in containers. Theres more to add in OLE programmingfor example, you could let the user position the new item in the view using the mousebut weve 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.
|
![]() |
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. |