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

📄 cntritem.cpp

📁 教你怎么用VC++做ActiveX控件
💻 CPP
字号:
// CntrItem.cpp : implementation of the CAxClientCntrItem class
//

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CAxClientCntrItem implementation

IMPLEMENT_SERIAL(CAxClientCntrItem, COleClientItem, 0)

CAxClientCntrItem::CAxClientCntrItem(CAxClientDoc* pContainer)
	: COleClientItem(pContainer)
{
	// TODO: add one-time construction code here
	m_ptPos=CPoint(0,0);	//	当前位置
	m_sizeCur=CSize(0,0);	//	当前大小
	m_sizeOrg=CSize(0,0);	//	原始大小
	
}

CAxClientCntrItem::~CAxClientCntrItem()
{
	// TODO: add cleanup code here
	
}

void CAxClientCntrItem::OnChange(OLE_NOTIFICATION nCode, DWORD dwParam)
{
	ASSERT_VALID(this);

	COleClientItem::OnChange(nCode, dwParam);
	switch(nCode)
	{
		case OLE_CHANGED:
			UpdateExtent();
			GetDocument()->UpdateAllViews(NULL);
			break;
		case OLE_CHANGED_ASPECT:
		case OLE_CHANGED_STATE:
			GetDocument()->UpdateAllViews(NULL);
			break;
	}

}

BOOL CAxClientCntrItem::OnChangeItemPosition(const CRect& rectPos)
{
	CAxClientView* pView = GetActiveView();
	if (pView == NULL)
		return FALSE;
	ASSERT_VALID(pView);

	CRect rc = rectPos;
//	pView->ClientToDoc(rc);
	CClientDC dc(pView);
	pView->OnPrepareDC(&dc);
	dc.DPtoLP(&rc); // convert device rect to logical rect

	CRect rectCur=CRect(m_ptPos,m_sizeCur);
	if (rc != rectCur)
	{
		// invalidate old item
		GetDocument()->UpdateAllViews(NULL);
		// update to new rectangle
//		SetRect(rc);
		m_ptPos = rc.TopLeft();
		m_sizeCur=rc.Size();

		GetDocument()->SetModifiedFlag();
		CSize sizeExtent;
		GetCachedExtent(&sizeExtent);
//		SetBaseSize(sizeExtent);
		m_sizeOrg=sizeExtent;

		// and invalidate new
		GetDocument()->UpdateAllViews(NULL);
	}
	return COleClientItem::OnChangeItemPosition(rectPos);
}

void CAxClientCntrItem::OnGetItemPosition(CRect& rPosition)
{
	ASSERT_VALID(this);

	// TODO: return correct rectangle (in pixels) in rPosition
//	rPosition.SetRect(10, 10, 210, 210);
	if (m_sizeCur == CSize(0,0))
		UpdateExtent();

	// 根据对象的当前位置和大小设置矩形
	rPosition = CRect(m_ptPos,m_sizeCur);

	//	将矩形从逻辑坐标转换为设备坐标
	CAxClientView* pView = GetActiveView();
	ASSERT_VALID(pView);
	CClientDC dc(pView);
	pView->OnPrepareDC(&dc);
	dc.LPtoDP(&rPosition);
	rPosition.NormalizeRect();

}

void CAxClientCntrItem::OnActivate()
{
    // Allow only one inplace activate item per frame
    CAxClientView* pView = GetActiveView();
    ASSERT_VALID(pView);
    COleClientItem* pItem = GetDocument()->GetInPlaceActiveItem(pView);
    if (pItem != NULL && pItem != this)
        pItem->Close();
    
    COleClientItem::OnActivate();
}

void CAxClientCntrItem::OnDeactivateUI(BOOL bUndoable)
{
	COleClientItem::OnDeactivateUI(bUndoable);

    // Hide the object if it is not an outside-in object
    DWORD dwMisc = 0;
    m_lpObject->GetMiscStatus(GetDrawAspect(), &dwMisc);
    if (dwMisc & OLEMISC_INSIDEOUT)
        DoVerb(OLEIVERB_HIDE, NULL);
}

void CAxClientCntrItem::Serialize(CArchive& ar)
{
	ASSERT_VALID(this);

	// Call base class first to read in COleClientItem data.
	// Since this sets up the m_pDocument pointer returned from
	//  CAxClientCntrItem::GetDocument, it is a good idea to call
	//  the base class Serialize first.
	COleClientItem::Serialize(ar);

	// now store/retrieve data specific to CAxClientCntrItem
	if (ar.IsStoring())
	{
		// TODO: add storing code here
		ar<<m_ptPos;	//	当前位置
		ar<<m_sizeCur;	//	当前大小
		ar<<m_sizeOrg;	//	原始大小
	}
	else
	{
		// TODO: add loading code here
		ar>>m_ptPos;	//	当前位置
		ar>>m_sizeCur;	//	当前大小
		ar>>m_sizeOrg;	//	原始大小
	}
}

BOOL CAxClientCntrItem::UpdateExtent()
{
	// 得到对象大小
	CSize size;
	if (!GetCachedExtent(&size))
		return FALSE;
	//	按照原先的位置和大小进行刷新
	GetDocument()->UpdateAllViews(NULL, 0, this);

	//	如果大小没有发生变化则直接返回
	if (size == m_sizeOrg) 
		return FALSE;
	
	// 纪录对象的大小
	if (m_sizeOrg == CSize(0,0))
	{
		CSize sizeNew(MulDiv(size.cx, 10, 254),  MulDiv(size.cy, 10, 254));
		m_sizeCur=sizeNew;
	}
	else
	{
		if (!IsInPlaceActive() && size != m_sizeOrg)
		{
			// data changed and not inplace, so scale up rect as well
			CSize sizeCur = m_sizeCur;
			sizeCur.cx = MulDiv(sizeCur.cx, size.cx, m_sizeOrg.cx);
			sizeCur.cy =  MulDiv(-sizeCur.cy, size.cy, m_sizeOrg.cy);
			m_sizeCur=sizeCur;
		}
	}
	m_sizeOrg=size;

	//	按照新的位置和大小进行刷新
	GetDocument()->UpdateAllViews(NULL, 0, this);
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CAxClientCntrItem diagnostics

#ifdef _DEBUG
void CAxClientCntrItem::AssertValid() const
{
	COleClientItem::AssertValid();
}

void CAxClientCntrItem::Dump(CDumpContext& dc) const
{
	COleClientItem::Dump(dc);
}
#endif

/////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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