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

📄 axclientview.cpp

📁 教你怎么用VC++做ActiveX控件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// AxClientView.cpp : implementation of the CAxClientView class
//

#include "stdafx.h"
#include "AxClient.h"

#include "AxClientDoc.h"
#include "CntrItem.h"
#include "AxClientView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAxClientView

IMPLEMENT_DYNCREATE(CAxClientView, CScrollView)

BEGIN_MESSAGE_MAP(CAxClientView, CScrollView)
	//{{AFX_MSG_MAP(CAxClientView)
	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_SETCURSOR()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	ON_COMMAND(ID_EDIT_DEL, OnEditDel)
	ON_UPDATE_COMMAND_UI(ID_EDIT_DEL, OnUpdateEditDel)
	ON_COMMAND(ID_EDIT_CUT, OnEditCut)
	ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
	ON_COMMAND(ID_EDIT_PASTE_SPECIAL, OnEditPasteSpecial)
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_SPECIAL, OnUpdateEditPasteSpecial)
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
	ON_COMMAND(ID_EDIT_PASTE_LINK, OnEditPasteLink)
	ON_COMMAND(ID_OLE_EDIT_PROPERTIES, OnOleEditProperties)
	ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_PROPERTIES, OnUpdateOleEditProperties)
	ON_WM_RBUTTONDOWN()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

CLIPFORMAT CAxClientView::m_cfObjectDescriptor=NULL;

/////////////////////////////////////////////////////////////////////////////
// CAxClientView construction/destruction

CAxClientView::CAxClientView()
{
	m_pSelection = NULL;
	// TODO: add construction code here
	if (m_cfObjectDescriptor == NULL)
		m_cfObjectDescriptor =
			(CLIPFORMAT)::RegisterClipboardFormat(_T("Object Descriptor"));

	m_pSelection = NULL;

}

CAxClientView::~CAxClientView()
{
}

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

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CAxClientView drawing

void CAxClientView::OnDraw(CDC* pDC)
{
	CAxClientDoc* 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 CAxClientCntrItem,
	//  to give the effect of in-place editing.

	// TODO: remove this code when final draw code is complete.
/*
	if (m_pSelection == NULL)
	{
		POSITION pos = pDoc->GetStartPosition();
		m_pSelection = (CAxClientCntrItem*)pDoc->GetNextClientItem(pos);
	}
	if (m_pSelection != NULL)
		m_pSelection->Draw(pDC, CRect(10, 10, 210, 210));
*/
	POSITION pos=pDoc->GetStartPosition();
	while(pos!=NULL)
	{
		CAxClientCntrItem* pItem=(CAxClientCntrItem*)pDoc->GetNextItem(pos);
		if(pItem == NULL) 
			break;

		//	绘制对象
		CRect rect=CRect(pItem->m_ptPos,pItem->m_sizeCur);
		pItem->Draw(pDC,rect);

		//	绘制对象的跟踪矩形
		CRectTracker tracker;
		SetupTracker(&tracker,pItem);
		tracker.Draw(pDC);
	}
}

void CAxClientView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();


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

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = sizeTotal.cy = 100;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

/////////////////////////////////////////////////////////////////////////////
// CAxClientView printing

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

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

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

void CAxClientView::OnDestroy()
{
	// Deactivate the item on destruction; this is important
	// when a splitter view is being used.
   CScrollView::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 CAxClientView::IsSelected(const CObject* pDocItem) const
{
	// The implementation below is adequate if your selection consists of
	//  only CAxClientCntrItem 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 CAxClientView::OnInsertObject()
{
	// Invoke the standard Insert Object dialog box to obtain information
	//  for new CAxClientCntrItem object.
	COleInsertDialog dlg;
	if (dlg.DoModal() != IDOK)
		return;

	BeginWaitCursor();

	CAxClientCntrItem* pItem = NULL;
	TRY
	{
		// Create new item connected to this document.
		CAxClientDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);
		pItem = new CAxClientCntrItem(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
		
		SetSelection(pItem);
		
		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 CAxClientView::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 CAxClientView::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;
		}
	}

	CScrollView::OnSetFocus(pOldWnd);
}

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

void CAxClientView::SetupTracker(CRectTracker* pTracker, CAxClientCntrItem* pItem,
	CRect* pTrueRect)
{
	ASSERT(pTracker != NULL);
	ASSERT(pItem != NULL);

	pTracker->m_rect = CRect(pItem->m_ptPos,pItem->m_sizeCur);
//	DocToClient(pTracker->m_rect);
	CClientDC dc(this);
	OnPrepareDC(&dc);
	dc.LPtoDP(&pTracker->m_rect);
	pTracker->m_rect.NormalizeRect();

	// set minimum size for our OLE items
	pTracker->m_sizeMin.cx = 8;
	pTracker->m_sizeMin.cy = 8;

	pTracker->m_nStyle = 0;

	// setup resize handles if item is selected
	if (pItem == m_pSelection)
		pTracker->m_nStyle |= CRectTracker::resizeInside;

	// put correct border depending on item type
	if (pItem->GetType() == OT_LINK)
		pTracker->m_nStyle |= CRectTracker::dottedLine;
	else
		pTracker->m_nStyle |= CRectTracker::solidLine;

	// put hatching over the item if it is currently open
	if (pItem->GetItemState() == COleClientItem::openState ||
		pItem->GetItemState() == COleClientItem::activeUIState)
	{
		pTracker->m_nStyle |= CRectTracker::hatchInside;
	}

	if (pTrueRect != NULL)
		pTracker->GetTrueRect(pTrueRect);
}

/////////////////////////////////////////////////////////////////////////////
// CAxClientView diagnostics

#ifdef _DEBUG
void CAxClientView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CAxClientView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CAxClientView message handlers

BOOL CAxClientView::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(&tracker,m_pSelection);
		if(tracker.SetCursor(this,nHitTest))
			return TRUE;
	}
	
	return CScrollView::OnSetCursor(pWnd, nHitTest, message);
}

CAxClientCntrItem* CAxClientView::HitTestItems(CPoint point)
{
	CAxClientDoc* pDoc=GetDocument();
	CAxClientCntrItem* pItemHit=NULL;
	
	POSITION pos=pDoc->GetStartPosition();
	while (pos!=NULL)
	{
		CAxClientCntrItem* pItem=(CAxClientCntrItem*)pDoc->GetNextItem(pos);
		if(pItem == NULL) 
			break;
		CRect rect=CRect(pItem->m_ptPos,pItem->m_sizeCur);
		if(rect.PtInRect(point)) 
		{
			pItemHit=pItem;
			return pItemHit;
		}
	}
	return pItemHit;
}

void CAxClientView::SetSelection(CAxClientCntrItem * pItem)
{
	if(pItem==NULL||m_pSelection!=pItem)
	{
		COleClientItem* pActiveItem=
			GetDocument()->GetInPlaceActiveItem(this);
		if(pActiveItem!=NULL&&pActiveItem!=pItem)
			pActiveItem->Close();
	}

	if(m_pSelection!=pItem)
	{
		if(m_pSelection!=NULL)
		{
			CRectTracker tracker;
			SetupTracker(&tracker,m_pSelection);
			CRect rect;
			tracker.GetTrueRect(rect);
			InvalidateRect(rect);
		}
		m_pSelection=pItem;
		if(m_pSelection!=NULL)
		{
			CRectTracker tracker;
			SetupTracker(&tracker,m_pSelection);
			CRect rect;
			tracker.GetTrueRect(rect);
			InvalidateRect(rect);
		}
	}

}

void CAxClientView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CAxClientCntrItem* pItemHit = NULL;

	pItemHit=HitTestItems(point);
	SetSelection(pItemHit);

	if(pItemHit!=NULL)
	{
		CRectTracker tracker;
		SetupTracker(&tracker,pItemHit);
		UpdateWindow();
		if(tracker.Track(this,point))
		{

⌨️ 快捷键说明

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