olecontainview.cpp
来自「Visual_C++.NET实用编程百例」· C++ 代码 · 共 429 行
CPP
429 行
// oleContainView.cpp : ColeContainView 类的实现
//
#include "stdafx.h"
#include "oleContain.h"
#include "oleContainDoc.h"
#include "CntrItem.h"
#include "oleContainView.h"
#include ".\olecontainview.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// ColeContainView
IMPLEMENT_DYNCREATE(ColeContainView, CView)
BEGIN_MESSAGE_MAP(ColeContainView, 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_LBUTTONDOWN()
ON_WM_MBUTTONDBLCLK()
ON_WM_LBUTTONDBLCLK()
ON_WM_SETCURSOR()
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_COMMAND(ID_EDIT_CUT, OnEditCut)
ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
END_MESSAGE_MAP()
// ColeContainView 构造/析构
ColeContainView::ColeContainView()
{
m_pSelection = NULL;
// TODO: 在此处添加构造代码
}
ColeContainView::~ColeContainView()
{
}
BOOL ColeContainView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
// 样式
return CView::PreCreateWindow(cs);
}
// ColeContainView 绘制
void ColeContainView::OnDraw(CDC* pDC)
{
if (!pDC)
return;
ColeContainDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
POSITION pos=pDoc->GetStartPosition ();
while(pos!=NULL)
{
ColeContainCntrItem* pCtrItem;
pCtrItem=(ColeContainCntrItem*)pDoc->GetNextClientItem(pos);
pCtrItem->Draw (pDC,pCtrItem->m_rect );
CRectTracker track;
SetupTracker(pCtrItem,&track);
track.Draw(pDC);
}
// TODO: 在此处为本机数据添加绘制代码
// TODO: 同时绘制文档中的所有 OLE 项
// 在任意位置绘制选定项。一旦
// 实现了真正的绘制代码后,应移除此代码。此位置
// 与 ColeContainCntrItem 返回的矩形完全对应,
//从而产生就地编辑的效果。
}
void ColeContainView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: 写入最终选择模式代码之后移除此代码
m_pSelection = NULL; // 初始化选定内容
}
// ColeContainView 打印
BOOL ColeContainView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void ColeContainView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 打印前添加额外的初始化
}
void ColeContainView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 打印后添加清除过程
}
void ColeContainView::OnDestroy()
{
// 停用处于析构中的项;这在
// 使用拆分器视图时非常重要
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
{
pActiveItem->Deactivate();
ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}
CView::OnDestroy();
}
// OLE 客户支持和命令
BOOL ColeContainView::IsSelected(const CObject* pDocItem) const
{
// 如果选定内容只包括
// ColeContainCntrItem 对象,则以下实现就足够了。若要处理其他选择机制,
// 则应替换此处的实现
// TODO: 实现对所选 OLE 客户项进行测试的函数
return pDocItem == m_pSelection;
}
void ColeContainView::OnInsertObject()
{
// 调用标准的“插入对象”对话框以获得有关
// 新 ColeContainCntrItem 对象的信息
COleInsertDialog dlg;
if (dlg.DoModal() != IDOK)
return;
BeginWaitCursor();
ColeContainCntrItem* pItem = NULL;
TRY
{
// 创建与此文档相连接的新项
ColeContainDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pItem = new ColeContainCntrItem(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 ColeContainView::OnCancelEditCntr()
{
// 关闭此视图中的任何就地活动项。
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL)
{
pActiveItem->Close();
}
ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
}
// 在就地编辑一个对象时,容器
// 需要对 OnSetFocus 和 OnSize 进行特殊处理
void ColeContainView::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 ColeContainView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
if (pActiveItem != NULL)
pActiveItem->SetItemRects();
}
void ColeContainView::OnFilePrint()
{
//默认情况下,要求活动文档使用 IOleCommandTarget 打印自身。
//如果不需要此行为,
//请移除对 COleDocObjectItem::DoDefaultPrinting 调用。
//如果由于某种原因调用失败,则将尝试使用 IPrint 接口打印
//该文档对象。
CPrintInfo printInfo;
ASSERT(printInfo.m_pPD != NULL);
if (S_OK == COleDocObjectItem::DoDefaultPrinting(this, &printInfo))
return;
CView::OnFilePrint();
}
// ColeContainView 诊断
#ifdef _DEBUG
void ColeContainView::AssertValid() const
{
CView::AssertValid();
}
void ColeContainView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
ColeContainDoc* ColeContainView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ColeContainDoc)));
return (ColeContainDoc*)m_pDocument;
}
#endif //_DEBUG
// ColeContainView 消息处理程序
void ColeContainView::SetupTracker(ColeContainCntrItem* pItem, CRectTracker* pTrack)
{
pTrack->m_rect =pItem->m_rect ;
if(pItem==m_pSelection)
pTrack->m_nStyle |=CRectTracker::resizeInside ;
if(pItem->GetType ()==OT_LINK)
pTrack->m_nStyle |=CRectTracker::dottedLine ;
else
pTrack->m_nStyle |=CRectTracker::solidLine ;
if(pItem->GetItemState ()==COleClientItem::openState ||pItem->GetItemState ()==COleClientItem::activeUIState )
pTrack->m_nStyle |=CRectTracker::hatchInside ;
}
ColeContainCntrItem* ColeContainView::Hit(CPoint point)
{
ColeContainCntrItem* pHit=NULL;
ColeContainDoc* pDoc=GetDocument();
POSITION pos=pDoc->GetStartPosition();
while(pos!=NULL)
{
ColeContainCntrItem* pItem;
pItem=(ColeContainCntrItem*)pDoc->GetNextClientItem (pos);
if(pItem->m_rect.PtInRect (point))
pHit=pItem;
}
return pHit;
}
void ColeContainView::SetSelect(ColeContainCntrItem* 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 ColeContainView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
ColeContainCntrItem* pHit=Hit(point);
SetSelect(pHit);
if(pHit==NULL)
return;
CRectTracker track;
SetupTracker(pHit,&track);
UpdateWindow();
if(track.Track (this,point))
{
Invalidate();
pHit->m_rect =track.m_rect ;
GetDocument()->SetModifiedFlag ();
}
CView::OnLButtonDown(nFlags, point);
}
void ColeContainView::OnMButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CView::OnMButtonDblClk(nFlags, point);
}
void ColeContainView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
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 ColeContainView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if(pWnd==this &&m_pSelection!=NULL)
{
CRectTracker track;
SetupTracker(m_pSelection,&track);
if(track.SetCursor (this,nHitTest))
return TRUE;
}
return CView::OnSetCursor(pWnd, nHitTest, message);
}
void ColeContainView::OnEditCopy()
{
if(m_pSelection!=NULL)
m_pSelection->CopyToClipboard ();
}
void ColeContainView::OnEditPaste()
{
// TODO: 在此添加命令处理程序代码
CWaitCursor cursor;
ColeContainCntrItem* pItem=NULL;
TRY
{
ColeContainDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
pItem=new ColeContainCntrItem(pDoc);
ASSERT_VALID(pItem);
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("创建失败");
}
END_CATCH
}
void ColeContainView::OnEditCut()
{
// TODO: 在此添加命令处理程序代码
OnEditCopy();
OnEditClear();
}
void ColeContainView::OnEditClear()
{
// TODO: 在此添加命令处理程序代码
if(m_pSelection!=NULL)
{
m_pSelection->Delete ();
m_pSelection=NULL;
GetDocument()->SetModifiedFlag ();
GetDocument()->UpdateAllViews (NULL);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?