⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exampl~2.cpp

📁 本书所附光盘的内容包含了开发实例的所有程序源码
💻 CPP
字号:
// ExampleView.cpp : CExampleView 类的实现
//
#include "stdafx.h"
#include "Example.h"
#include "ExampleDoc.h"
#include "CntrItem.h"
#include "ExampleView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CExampleView
IMPLEMENT_DYNCREATE(CExampleView, CView)
BEGIN_MESSAGE_MAP(CExampleView, CView)
	ON_WM_DESTROY()
	ON_WM_SETFOCUS()
	ON_WM_SIZE()
	ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
	ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
	ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
END_MESSAGE_MAP()

// CExampleView 构造/销毁
CExampleView::CExampleView()
{
	m_pSelection = NULL;
	// TODO: 在此处添加构造代码
}

CExampleView::~CExampleView()
{
}

BOOL CExampleView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
	// 样式
	return CView::PreCreateWindow(cs);
}

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

	if (m_pSelection == NULL)
	{
		POSITION pos = pDoc->GetStartPosition();
		m_pSelection = (CExampleCntrItem*)pDoc->GetNextClientItem(pos);
	}
	if (m_pSelection != NULL)
	{
		m_pSelection->Draw(pDC, m_pSelection->m_rect);
		// 显示跟踪矩形
		CRectTracker tracker;
		SetupTracker(m_pSelection,&tracker);
		tracker.Draw(pDC);
	}
}

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

	// TODO: 写入最终选择模式代码之后移除此代码
	m_pSelection = NULL;    // 初始化选定内容

}

void CExampleView::OnDestroy()
{
	// 停用处于销毁中的项;这在
	// 使用拆分器视图时非常重要 
   COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
   {
      pActiveItem->Deactivate();
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
   }
   CView::OnDestroy();
}

// OLE 客户支持和命令
BOOL CExampleView::IsSelected(const CObject* pDocItem) const
{
	// TODO: 实现对所选 OLE 客户项进行测试的函数
	return pDocItem == m_pSelection;
}

void CExampleView::OnInsertObject()
{
	// 调用标准的“插入对象”对话框以获得有关
	// 新 CExampleCntrItem 对象的信息
	COleInsertDialog dlg;
	if (dlg.DoModal() != IDOK)
		return;

	BeginWaitCursor();

	CExampleCntrItem* pItem = NULL;
	TRY
	{
		// 创建与此文档相连接的新项
		CExampleDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);
		pItem = new CExampleCntrItem(pDoc);
		ASSERT_VALID(pItem);

		// 通过对话框数据初始化该项
		if (!dlg.CreateItem(pItem))
			AfxThrowMemoryException();  // 任何异常都将导致该结果
		ASSERT_VALID(pItem);
		
        if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
			pItem->DoVerb(OLEIVERB_SHOW, this);

		ASSERT_VALID(pItem);
		// 作为任意用户界面设计,这会将选定内容
		//  设置为插入的最后一项

		// TODO: 重新实现选定内容,使其适合于您的应用程序
		m_pSelection = pItem;   // 将选定内容设置为插入的最后一项
		pDoc->UpdateAllViews(NULL);
	}
	CATCH(CException, e)
	{
		if (pItem != NULL)
		{
			ASSERT_VALID(pItem);
			pItem->Delete();
		}
		AfxMessageBox(IDP_FAILED_TO_CREATE);
	}
	END_CATCH

	EndWaitCursor();
}

void CExampleView::OnCancelEditCntr()
{
	// 关闭此视图中的任何就地活动项。
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL)
	{
		pActiveItem->Close();
	}
	ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}

void CExampleView::OnSetFocus(CWnd* pOldWnd)
{
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL &&
		pActiveItem->GetItemState() == COleClientItem::activeUIState)
	{
		// 如果该项处于同一视图中,则需要将焦点设置到该项
		CWnd* pWnd = pActiveItem->GetInPlaceWindow();
		if (pWnd != NULL)
		{
			pWnd->SetFocus();   // 不要调用基类
			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();
}

void CExampleView::OnFilePrint()
{
	CPrintInfo printInfo;
	ASSERT(printInfo.m_pPD != NULL); 
	if (S_OK == COleDocObjectItem::DoDefaultPrinting(this, &printInfo))
		return;
	CView::OnFilePrint();
}

#ifdef _DEBUG
CExampleDoc* CExampleView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CExampleDoc)));
	return (CExampleDoc*)m_pDocument;
}
#endif //_DEBUG

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;
}

⌨️ 快捷键说明

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