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

📄 kyofficeview.cpp

📁 支持在线编辑的活动文档控件
💻 CPP
字号:
// KyOfficeView.cpp : implementation of the CKyOfficeView class
//

#include "stdafx.h"
#include "KyOffice.h"
#include "Resource.h"
#include "KyOfficeDoc.h"
#include "CntrItem.h"
#include "MainFrm.h"
#include "NewDocDlg.h"
#include "KyOfficeCtrl.h"
#include "KyOfficeView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CKyOfficeView

IMPLEMENT_DYNCREATE(CKyOfficeView, CView)

BEGIN_MESSAGE_MAP(CKyOfficeView, CView)
	//{{AFX_MSG_MAP(CKyOfficeView)
	ON_WM_DESTROY()
	ON_WM_SETFOCUS()
	ON_WM_SIZE()
	ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
	//}}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, OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKyOfficeView construction/destruction

CKyOfficeView::CKyOfficeView()
{
	m_pSelection	= NULL;
	// TODO: add construction code here
	_cRef			= 1;
	m_pOfficeCtrl	= NULL;
}

CKyOfficeView::~CKyOfficeView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CKyOfficeView drawing

void CKyOfficeView::OnDraw(CDC* pDC)
{
	CKyOfficeDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	// TODO: also draw all OLE items in the document

	// Draw the selection at an arbitrary position.  This code should be
	//  removed once your real drawing code is implemented.  This position
	//  corresponds exactly to the rectangle returned by CKyOfficeCntrItem,
	//  to give the effect of in-place editing.

	// TODO: remove this code when final draw code is complete.
}

/////////////////////////////////////////////////////////////////////////////
// CKyOfficeView printing

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

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

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

void CKyOfficeView::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 CKyOfficeView::IsSelected(const CObject * pDocItem) const
{
	// The implementation below is adequate if your selection consists of
	//  only CKyOfficeCntrItem 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;
}

// Special handling of OnSetFocus and OnSize are required for a container
//  when an object is being edited in-place.
void CKyOfficeView::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 CKyOfficeView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);
	COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
	if (pActiveItem != NULL)
		pActiveItem->SetItemRects();
}

/////////////////////////////////////////////////////////////////////////////
// CKyOfficeView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CKyOfficeView message handlers

void CKyOfficeView::OnFileClose() 
{
	// TODO: Add your command handler code here
	CWaitCursor	w;
	DoCloseDocument();
}

BOOL CKyOfficeView::DoNew(REFCLSID clsid)
{
	BOOL bRet = FALSE;

	CLSID newclsid = clsid;

	if(newclsid == CLSID_NULL)
	{
		CNewDocDlg dlg;
		if(dlg.DoModal() != IDOK)
			return FALSE;
		else
			newclsid = dlg.GetSelectedClsid();
	}

	BeginWaitCursor();

	//关闭以前打开的文档
	DoCloseDocument();

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

		// Initialize the item from the dialog data.
		if (!pItem->CreateNewItem(newclsid))
			AfxThrowMemoryException();  // any exception will do
		ASSERT_VALID(pItem);

		//郁闷,打开Microsoft PowerPoint文件需要等待,否则会产生异常
		Sleep(250);
		
		pItem->DoVerb(OLEIVERB_SHOW, this);
		ASSERT_VALID(pItem);

		m_pSelection = pItem;   // set selection to last inserted item
		pDoc->UpdateAllViews(NULL);
		pDoc->SetModifiedFlag(FALSE);

		bRet = TRUE;
	}
	CATCH(CException, e)
	{
		if (pItem != NULL)
		{
			ASSERT_VALID(pItem);
			pItem->Delete();
		}
		AfxMessageBox(IDP_FAILED_TO_CREATE);
	}
	END_CATCH

	EndWaitCursor();

	return bRet;
}

BOOL CKyOfficeView::DoOpenDocument(LPCTSTR lpszPathName)
{
	if(lpszPathName == NULL)
		return FALSE;

	BOOL bRet = FALSE;
	
	BeginWaitCursor();

	//关闭以前打开的文档
	DoCloseDocument();

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

		// Initialize the item from the dialog data.
		if (!pItem->CreateFromFile(lpszPathName))
			AfxThrowMemoryException();  // any exception will do
		ASSERT_VALID(pItem);

		//郁闷,打开Microsoft PowerPoint文件需要等待,否则会产生异常
		Sleep(250);
		
		pItem->DoVerb(OLEIVERB_SHOW, this);
		ASSERT_VALID(pItem);

		m_pSelection = pItem;   // set selection to last inserted item
		pDoc->UpdateAllViews(NULL);
		pDoc->SetModifiedFlag(FALSE);

		bRet = TRUE;
	}
	CATCH(CException, e)
	{
		if (pItem != NULL)
		{
			ASSERT_VALID(pItem);
			pItem->Delete();
		}
		AfxMessageBox(IDP_FAILED_TO_CREATE);
	}
	END_CATCH

	EndWaitCursor();

	return bRet;
}

void CKyOfficeView::DoCloseDocument()
{
	if (m_pSelection != NULL)
	{
		//还原Word用户名
		LPOLEINPLACEACTIVEOBJECT lpObject = m_pSelection->m_pInPlaceFrame->m_lpActiveObject;
		MSWord::_DocumentPtr pWordDoc = NULL;
		if(lpObject && SUCCEEDED(lpObject->QueryInterface(IID__Document, (void**)&pWordDoc)))
		{
			CString strUserName = m_pOfficeCtrl->GetOldUserName();
			CComQIPtr<MSWord::_Application> spApp(pWordDoc->Application);
			if(m_pOfficeCtrl && spApp && !strUserName.IsEmpty())
				spApp->PutUserName(_bstr_t(strUserName));
		}

		m_pSelection->Close();
		
		ASSERT_VALID(m_pSelection);
		m_pSelection->Delete();
		m_pSelection = NULL;		
	
		CKyOfficeDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);

		CString strDocName;
		VERIFY(strDocName.LoadString(AFX_IDS_UNTITLED));
		pDoc->SetTitle(strDocName);

		pDoc->EmptyPathName();
		pDoc->SetModifiedFlag(FALSE);

		ShowWindow(SW_SHOW);
		Invalidate();
	}
}

void CKyOfficeView::OnAppAbout() 
{
	// TODO: Add your command handler code here
	CDialog dlg(IDD_DIALOG_ABOUT);
	dlg.DoModal();
}

void CKyOfficeView::OnFilePrint() 
{
	// TODO: Add your command handler code here
	DoFilePrint();
}

void CKyOfficeView::DoFilePrint()
{
	CWaitCursor w;

	CKyOfficeCntrItem * pActiveItem = (CKyOfficeCntrItem*)GetDocument()->GetInPlaceActiveItem(this);
	if(pActiveItem != NULL)
	{
		CPrintInfo pInfo;
		pInfo.m_dwFlags |= PRINTFLAG_PROMPTUSER;

		if(!COleDocObjectItem::OnPreparePrinting(this, &pInfo, FALSE))
			return;

		if(!DoPreparePrinting(&pInfo))
			return;

		COleDocObjectItem::OnPrint(this, &pInfo, FALSE);
	}
}

STDMETHODIMP CKyOfficeView::QueryInterface(REFIID riid, void **ppv)
{
    if(ppv == NULL)
		return E_INVALIDARG;
	
	HRESULT hr = S_OK;

	if(IID_IUnknown == riid || IID_IPreviewCallback == riid)
		*ppv = static_cast<IPreviewCallback*>(this);
	else
	{
		*ppv = NULL;
		hr = E_NOINTERFACE;
	}

	if(NULL != *ppv)
		reinterpret_cast<IUnknown*>(*ppv)->AddRef();

	return hr;
}

STDMETHODIMP_(ULONG) CKyOfficeView::AddRef(void)
{
    return ++_cRef;
}

STDMETHODIMP_(ULONG) CKyOfficeView::Release(void)
{
    return --_cRef;
}

STDMETHODIMP CKyOfficeView::Notify(DWORD wStatus, LONG nLastPage, wchar_t* pwszPreviewStatus)
{

  // The only notification we care about is when the preview is done...
    if (wStatus == NOTIFY_FORCECLOSEPREVIEW)
	{
        ShowWindow(SW_SHOW);
		Invalidate();
	}
    return S_OK;
}

void CKyOfficeView::OnFilePrintPreview() 
{
	// TODO: Add your command handler code here
	DoFilePrintPreview();
}

void CKyOfficeView::DoFilePrintPreview() 
{
	// TODO: Add your command handler code here
	CWaitCursor w;
	
	CKyOfficeCntrItem * pActiveItem = (CKyOfficeCntrItem*)GetDocument()->GetInPlaceActiveItem(this);
	if(pActiveItem != NULL)
	{
		IInplacePrintPreview * pprev = NULL;

		if(SUCCEEDED(pActiveItem->m_lpObject->QueryInterface(IID_IInplacePrintPreview, (void**)&pprev)))
		{
			if (SUCCEEDED(pprev->QueryStatus()))
				pprev->StartPrintPreview(PREVIEWFLAG_MAYBOTHERUSER, NULL, (IPreviewCallback*)this, 1);
			pprev->Release();

			CLSID clsid;
			if ((SUCCEEDED(pActiveItem->m_lpObject->GetUserClassID(&clsid))) &&
				((clsid == CLSID_EXCELWORKBOOK) || (clsid == CLSID_EXCELCHART)))
				ShowWindow(SW_HIDE);
		}
		else
		{
			AfxMessageBox("对不起,此种格式文件的服务器不提供打印预览功能!", MB_OK|MB_ICONERROR);
		}
	}
}

⌨️ 快捷键说明

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