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

📄 containerview.cpp

📁 《Visual C++.NET MFC类库应用详解》程序实例
💻 CPP
字号:
// ContainerView.cpp : CContainerView 类的实现
//

#include "stdafx.h"
#include "Container.h"
#include "afxodlgs.h"

#include "ContainerDoc.h"
#include "CntrItem.h"
#include "ContainerView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CContainerView

IMPLEMENT_DYNCREATE(CContainerView, CView)

BEGIN_MESSAGE_MAP(CContainerView, 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)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_SETCURSOR()
	ON_COMMAND(ID_EDIT_DELETE, OnEditDelete)
	ON_UPDATE_COMMAND_UI(ID_EDIT_DELETE, OnUpdateEditDelete)
END_MESSAGE_MAP()

// CContainerView 构造/销毁

CContainerView::CContainerView()
{
	m_pSelection = NULL;
	// TODO: 在此处添加构造代码

}

CContainerView::~CContainerView()
{
}

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

	return CView::PreCreateWindow(cs);
}

// CContainerView 绘制

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

	// TODO: 在此处为本机数据添加绘制代码
	// TODO: 同时绘制文档中的所有 OLE 项

	// 在任意位置绘制选定项。一旦实现了真正的绘制代码后,
	// 应移除此代码。此位置与 CContainerCntrItem 返回的
	// 矩形完全对应,从而产生就地编辑的效果。

	POSITION pos=pDoc->GetStartPosition();
	while(pos)
	{
		CContainerCntrItem* pItem=
			(CContainerCntrItem*)pDoc->GetNextItem(pos);
		pItem->Draw(pDC,pItem->m_ObRect);
		CRectTracker tracker;
		SetupTracker(&tracker,pItem);
		tracker.Draw(pDC);
	}
/*
	// TODO: 最终绘制代码完成后移除此代码。
	if (m_pSelection != NULL)
	{
		CSize size;
		CRect rect(10, 10, 210, 210);
		
		if (SUCCEEDED(m_pSelection->GetExtent(&size, m_pSelection->m_nDrawAspect)))
		{
			pDC->HIMETRICtoLP(&size);
			rect.right = size.cx + 10;
			rect.bottom = size.cy + 10;
		}
		m_pSelection->Draw(pDC, rect);
	}
*/
}

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

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

}


// CContainerView 打印

BOOL CContainerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CContainerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 打印前添加额外的初始化
}

void CContainerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 打印后添加清除过程
}

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



// OLE 客户支持和命令

BOOL CContainerView::IsSelected(const CObject* pDocItem) const
{
	// 如果选定内容只包括
	// CContainerCntrItem 对象,则以下实现就足够了。若要处理其他选择机制,
	// 则应替换此处的实现

	// TODO: 实现对所选 OLE 客户项进行测试的函数

	return pDocItem == m_pSelection;
}

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

	BeginWaitCursor();

	CContainerCntrItem* pItem = NULL;
	TRY
	{
		// 创建与此文档相连接的新项
		CContainerDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);
		pItem = new CContainerCntrItem(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 CContainerView::OnCancelEditCntr()
{
	// 关闭此视图中的任何就地活动项。
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL)
	{
		pActiveItem->Close();
	}
	ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}

// 在就地编辑一个对象时,容器
//  需要对 OnSetFocus 和 OnSize 进行特殊处理
void CContainerView::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 CContainerView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL)
		pActiveItem->SetItemRects();
}

void CContainerView::OnFilePrint()
{
	//默认情况下,要求活动文档使用 IOleCommandTarget 打印自身。
	//如果不需要此行为,
	//请移除对 COleDocObjectItem::DoDefaultPrinting 调用。
	//如果由于某种原因调用失败,则将尝试使用 IPrint 接口打印
	//该文档对象。
	CPrintInfo printInfo;
	ASSERT(printInfo.m_pPD != NULL); 
	if (S_OK == COleDocObjectItem::DoDefaultPrinting(this, &printInfo))
		return;
	
	CView::OnFilePrint();

}



// CContainerView 诊断

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

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

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


// CContainerView 消息处理程序

CContainerCntrItem* CContainerView::GetHitItem(CPoint point)
{
    CContainerCntrItem* pObjectHit=NULL;
	CContainerDoc* pDoc=GetDocument();
	bool bObjectHit;
	POSITION pos=pDoc->GetStartPosition();
	while(pos)
	{
		CContainerCntrItem* pObject=(CContainerCntrItem *)pDoc->GetNextItem(pos);
		bObjectHit=pObject->m_ObRect.PtInRect(point);
		if(bObjectHit)
			pObjectHit=pObject;
	}
    return pObjectHit;
}

void CContainerView::SetupTracker(CRectTracker* pTracker,CContainerCntrItem* pObject)
{
	pTracker->m_rect=pObject->m_ObRect;
	if(pObject==m_pSelection)
		pTracker->m_nStyle=CRectTracker::resizeInside;
	OLE_OBJTYPE objType=pObject->GetType();
	if(objType==OT_EMBEDDED)
		pTracker->m_nStyle|=CRectTracker::solidLine;
	else if(objType==OT_LINK)
		pTracker->m_nStyle|=CRectTracker::dottedLine;
	UINT objectState=pObject->GetItemState();
	if((objectState==COleClientItem::activeUIState)||
		(objectState==COleClientItem::openState))
		pTracker->m_nStyle|=CRectTracker::hatchInside;

}

void CContainerView::SetAsSelected(CContainerCntrItem* pObject)
{
	CContainerDoc* pDoc=GetDocument();
	if((m_pSelection!=pObject)||(pObject==NULL))
	{
		COleClientItem* pActiveObject=pDoc->GetInPlaceActiveItem(this);
		if((pActiveObject!=pObject)&&(pActiveObject))
			pActiveObject->Close();
	}
	m_pSelection=pObject;
	Invalidate();
}

void CContainerView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
    CContainerCntrItem* pItem=GetHitItem(point);
	SetAsSelected(pItem);
	if(!pItem)
		return;
	CRectTracker tracker;
	SetupTracker(&tracker,pItem);
	UpdateWindow();
	if(!tracker.Track(this,point))
		return;
	Invalidate();
	pItem->m_ObRect=tracker.m_rect;
	CContainerDoc* pDoc=GetDocument();
	pDoc->SetModifiedFlag();

	CView::OnLButtonUp(nFlags, point);
}

void CContainerView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
    OnLButtonUp(nFlags,point);
    if(!m_pSelection)
		return;
	short keyState=GetKeyState(VK_CONTROL);
	long oleVerb;
	if(keyState<0)
		oleVerb=OLEIVERB_OPEN;
	else
		oleVerb=OLEIVERB_PRIMARY;
	m_pSelection->DoVerb(oleVerb,this);

	CView::OnLButtonDblClk(nFlags, point);
}

BOOL CContainerView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
    if((m_pSelection)&&(pWnd==this))
	{
		CRectTracker tracker;
		SetupTracker(&tracker,m_pSelection);
		bool bCaptured;
		bCaptured=(bool)tracker.SetCursor(this,nHitTest);
		if(bCaptured)
			return true;
	}

	return CView::OnSetCursor(pWnd, nHitTest, message);
}

void CContainerView::OnEditDelete()
{
	// TODO: 在此添加命令处理程序代码
	if(m_pSelection)
	{
		m_pSelection->Delete();
		m_pSelection=NULL;
		CContainerDoc* pDoc=GetDocument();
		pDoc->SetModifiedFlag();
		pDoc->UpdateAllViews(NULL);
	}
}

void CContainerView::OnUpdateEditDelete(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
    if(m_pSelection)
		pCmdUI->Enable(TRUE);
	else
		pCmdUI->Enable(FALSE);
}


⌨️ 快捷键说明

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