exampleview.cpp

来自「vc150例源码」· C++ 代码 · 共 446 行

CPP
446
字号
// ExampleView.cpp : implementation of the CExampleView class
//

#include "stdafx.h"
#include "Example.h"

#include "ExampleDoc.h"
#include "CntrItem.h"
#include "ExampleView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CExampleView

IMPLEMENT_DYNCREATE(CExampleView, CView)

BEGIN_MESSAGE_MAP(CExampleView, CView)
	//{{AFX_MSG_MAP(CExampleView)
	ON_WM_DESTROY()
	ON_WM_SETFOCUS()
	ON_WM_SIZE()
	ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
	ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_SETCURSOR()
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
	ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
	ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR, OnUpdateEditClear)
	ON_COMMAND(ID_EDIT_CUT, OnEditCut)
	ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExampleView construction/destruction

CExampleView::CExampleView()
{
	m_pSelection = NULL;
	// TODO: add construction code here

}

CExampleView::~CExampleView()
{
}

BOOL CExampleView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CExampleView drawing

void CExampleView::OnDraw(CDC* pDC)
{
	CExampleDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// 遍历OLE项
	POSITION pos=pDoc->GetStartPosition();
	while(pos!=NULL)
	{
		// 显示OLE项本身
		CExampleCntrItem * pCurItem;
		pCurItem=(CExampleCntrItem*) pDoc->GetNextClientItem(pos);
		pCurItem->Draw(pDC,pCurItem->m_rect);
		// 显示跟踪矩形
		CRectTracker tracker;
		SetupTracker(pCurItem,&tracker);
		tracker.Draw(pDC);
	}
}

void CExampleView::OnInitialUpdate()
{
	CView::OnInitialUpdate();


	// TODO: remove this code when final selection model code is written
	m_pSelection = NULL;    // initialize selection

}

/////////////////////////////////////////////////////////////////////////////
// CExampleView printing

BOOL CExampleView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CExampleView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CExampleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CExampleView::OnDestroy()
{
	// Deactivate the item on destruction; this is important
	// when a splitter view is being used.
   CView::OnDestroy();
   COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
   {
      pActiveItem->Deactivate();
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
   }
}


/////////////////////////////////////////////////////////////////////////////
// OLE Client support and commands

BOOL CExampleView::IsSelected(const CObject* pDocItem) const
{
	// The implementation below is adequate if your selection consists of
	//  only CExampleCntrItem objects.  To handle different selection
	//  mechanisms, the implementation here should be replaced.

	// TODO: implement this function that tests for a selected OLE client item

	return pDocItem == m_pSelection;
}

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

	BeginWaitCursor();

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

		// Initialize the item from the dialog data.
		if (!dlg.CreateItem(pItem))
			AfxThrowMemoryException();  // any exception will do
		ASSERT_VALID(pItem);
		
        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
		pDoc->UpdateAllViews(NULL);
	}
	CATCH(CException, e)
	{
		if (pItem != NULL)
		{
			ASSERT_VALID(pItem);
			pItem->Delete();
		}
		AfxMessageBox(IDP_FAILED_TO_CREATE);
	}
	END_CATCH

	EndWaitCursor();
}

// The following command handler provides the standard keyboard
//  user interface to cancel an in-place editing session.  Here,
//  the container (not the server) causes the deactivation.
void CExampleView::OnCancelEditCntr()
{
	// Close any in-place active item on this view.
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL)
	{
		pActiveItem->Close();
	}
	ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}

// Special handling of OnSetFocus and OnSize are required for a container
//  when an object is being edited in-place.
void CExampleView::OnSetFocus(CWnd* pOldWnd)
{
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL &&
		pActiveItem->GetItemState() == COleClientItem::activeUIState)
	{
		// need to set focus to this item if it is in the same view
		CWnd* pWnd = pActiveItem->GetInPlaceWindow();
		if (pWnd != NULL)
		{
			pWnd->SetFocus();   // don't call the base class
			return;
		}
	}

	CView::OnSetFocus(pOldWnd);
}

void CExampleView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL)
		pActiveItem->SetItemRects();
}

/////////////////////////////////////////////////////////////////////////////
// CExampleView diagnostics

#ifdef _DEBUG
void CExampleView::AssertValid() const
{
	CView::AssertValid();
}

void CExampleView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CExampleDoc* CExampleView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CExampleDoc)));
	return (CExampleDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CExampleView message handlers
void CExampleView::SetupTracker(CExampleCntrItem *pItem, CRectTracker *pTracker)
{
	// 设置跟踪矩形的大小
	pTracker->m_rect=pItem->m_rect;
	// 设置跟踪矩形边框的风格
	if(pItem==m_pSelection)
		pTracker->m_nStyle|=CRectTracker::resizeInside;
	if(pItem->GetType()==OT_LINK)
		pTracker->m_nStyle|=CRectTracker::dottedLine;
	else
		pTracker->m_nStyle|=CRectTracker::solidLine;
	if(	pItem->GetItemState()==COleClientItem::openState ||
		pItem->GetItemState()==COleClientItem::activeUIState)
			pTracker->m_nStyle|=CRectTracker::hatchInside;
}

CExampleCntrItem* CExampleView::HitTest(CPoint point)
{
	CExampleCntrItem* pHitItem=NULL;
	CExampleDoc* pDoc=GetDocument();

	POSITION pos=pDoc->GetStartPosition();
	while(pos!=NULL)
	{
		CExampleCntrItem* pItem;
		pItem=(CExampleCntrItem*) pDoc->GetNextClientItem(pos);
		if(pItem->m_rect.PtInRect(point))
		{
			pHitItem=pItem;
		}
	}
	return pHitItem;
}

void CExampleView::SetSelection(CExampleCntrItem *pItem)
{
	if(pItem==NULL || pItem!=m_pSelection)
	{
		COleClientItem* pCurActive;
		pCurActive=GetDocument()->GetInPlaceActiveItem(this);
		if(pCurActive!=NULL && pCurActive!=pItem)
			pCurActive->Close();
	}
	m_pSelection=pItem;
	Invalidate();
}

void CExampleView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CExampleCntrItem* pHitItem=HitTest(point);
	SetSelection(pHitItem);
	if(pHitItem==NULL)
		return;
	// 显示跟踪矩形
	CRectTracker tracker;
	SetupTracker(pHitItem,&tracker);
	UpdateWindow();

	if(tracker.Track(this,point))
	{
		Invalidate();
		pHitItem->m_rect=tracker.m_rect;
		GetDocument()->SetModifiedFlag();
	}
	CView::OnLButtonDown(nFlags, point);
}

void CExampleView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	OnLButtonDown(nFlags,point);
	if(m_pSelection)
	{
		if(::GetKeyState(VK_CONTROL)>=0)
		{
			m_pSelection->DoVerb(OLEIVERB_PRIMARY,this);
		}
		else
		{
			m_pSelection->DoVerb(OLEIVERB_OPEN,this);
		}
	}
	
	CView::OnLButtonDblClk(nFlags, point);
}

BOOL CExampleView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if(pWnd==this && m_pSelection!=NULL)
	{
		CRectTracker tracker;
		SetupTracker(m_pSelection,&tracker);
		if(tracker.SetCursor(this,nHitTest))
		{
			return TRUE;
		}
	}
	return CView::OnSetCursor(pWnd, nHitTest, message);
}

void CExampleView::OnEditCopy() 
{
	// TODO: Add your command handler code here
	if(m_pSelection!=NULL)
		m_pSelection->CopyToClipboard();
}

void CExampleView::OnUpdateEditCopy(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(m_pSelection!=NULL);
}

void CExampleView::OnEditClear() 
{
	// TODO: Add your command handler code here
	if(m_pSelection!=NULL)
	{
		m_pSelection->Delete();
		m_pSelection=NULL;
		GetDocument()->SetModifiedFlag();
		GetDocument()->UpdateAllViews(NULL);
	}
}

void CExampleView::OnUpdateEditClear(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(m_pSelection!=NULL);
}

void CExampleView::OnEditCut() 
{
	// TODO: Add your command handler code here
	OnEditCopy();
	OnEditClear();
}

void CExampleView::OnUpdateEditCut(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(m_pSelection!=NULL);
}

void CExampleView::OnEditPaste() 
{
	// TODO: Add your command handler code here
	CWaitCursor cursor;

	CExampleCntrItem* pItem = NULL;
	TRY
	{
		// 建立与文档相关的新OLE项
		CExampleDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);
		pItem = new CExampleCntrItem(pDoc);
		ASSERT_VALID(pItem);

		// 从剪贴板中获得数据初始化新的OLE项
		if (!pItem->CreateFromClipboard())
			AfxThrowMemoryException();    
			// 抛出例外
		ASSERT_VALID(pItem);

		// 更新显示
		m_pSelection=pItem;
		pDoc->UpdateAllViews(NULL);
	}
	CATCH(CException, e)
	{
		// 捕捉例外
		if (pItem != NULL)
		{
			ASSERT_VALID(pItem);
			pItem->Delete();
		}
		AfxMessageBox(IDP_FAILED_TO_CREATE);
	}
	END_CATCH
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?