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

📄 axclientview.cpp

📁 教你怎么用VC++做ActiveX控件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			CRectTracker trackerTemp;
			CRect rect;
			SetupTracker(&trackerTemp,m_pSelection);
			trackerTemp.GetTrueRect(rect);
			InvalidateRect(rect);

			pItemHit->m_ptPos=tracker.m_rect.TopLeft();
			pItemHit->m_sizeCur=tracker.m_rect.Size();

			SetupTracker(&trackerTemp,pItemHit);
			trackerTemp.GetTrueRect(rect);
			InvalidateRect(rect);
		
		}

	}
		
	CScrollView::OnLButtonDown(nFlags, point);
}

void CAxClientView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (m_pSelection != NULL)
	{
		BeginWaitCursor();
		LONG iVerb = OLEIVERB_PRIMARY;
		if (GetKeyState(VK_CONTROL) < 0)
			iVerb = OLEIVERB_OPEN;
		m_pSelection->DoVerb(iVerb, this);
		EndWaitCursor();
	}

	CScrollView::OnLButtonDblClk(nFlags, point);
}

CAxClientCntrItem* CAxClientView::DoPasteItem(BOOL bLink, COleDataObject* pDataObject,
	CPoint* pPoint, CLIPFORMAT cfFormat)
{
	BeginWaitCursor();

	CAxClientCntrItem* pItem = new CAxClientCntrItem(GetDocument());
	ASSERT_VALID(pItem);
	BOOL bAllowAdjust = (pPoint == NULL) ? TRUE : FALSE;

	// use clipboard data if not doing drag/drop
	COleDataObject clipboardData;
	if (pDataObject == NULL)
	{
		clipboardData.AttachClipboard();
		pDataObject = &clipboardData;
	}

	TRY
	{
		if (bAllowAdjust)
		{
			CPoint ptDef(10, 10);
			DoPasteStandard(bLink, pDataObject, &ptDef, pItem, cfFormat);
		}
		else
		{
			DoPasteStandard(bLink, pDataObject, pPoint, pItem, cfFormat);
		}

	}
	CATCH_ALL(e)
	{
		// general cleanup
		TRACE0("failed to embed/link an OLE object\n");
		pItem->Delete();
		pItem = NULL;
	}
	END_CATCH_ALL

	// set the selection with bSafeSelect = TRUE
	SetSelection(pItem);

	// update the document and views
	GetDocument()->SetModifiedFlag();
	GetDocument()->UpdateAllViews(NULL, 0, pItem);      // including this view

	EndWaitCursor();

	return pItem;
}

void CAxClientView::DoPasteStandard(BOOL bLink, COleDataObject* pDataObject,
	CPoint* pPoint, CAxClientCntrItem* pItem, CLIPFORMAT cfFormat)
{
	if (bLink)      // paste link
	{
		if (!pItem->CreateLinkFromData(pDataObject))
			AfxThrowMemoryException();  // any exception will do
	}
	// paste embedded
	else if (!pItem->CreateFromData(pDataObject) &&
		!pItem->CreateStaticFromData(pDataObject, OLERENDER_DRAW, cfFormat))
	{
		AfxThrowMemoryException();      // any exception will do
	}

	// copy the current iconic representation
	FORMATETC fmtetc;
	fmtetc.cfFormat = CF_METAFILEPICT;
	fmtetc.dwAspect = DVASPECT_ICON;
	fmtetc.ptd = NULL;
	fmtetc.tymed = TYMED_MFPICT;
	fmtetc.lindex = 1;
	HGLOBAL hObj = pDataObject->GetGlobalData(CF_METAFILEPICT, &fmtetc);
	if (hObj != NULL)
	{
		pItem->SetIconicMetafile(hObj);
		// the following code is an easy way to free a metafile pict
		STGMEDIUM stgMed;
		memset(&stgMed, 0, sizeof(stgMed));
		stgMed.tymed = TYMED_MFPICT;
		stgMed.hGlobal = hObj;
		ReleaseStgMedium(&stgMed);
	}

	// set the current drawing aspect
	hObj = pDataObject->GetGlobalData(m_cfObjectDescriptor);
	if (hObj != NULL)
	{
		ASSERT(hObj != NULL);
		// got CF_OBJECTDESCRIPTOR ok.  Lock it down and extract size.
		LPOBJECTDESCRIPTOR pObjDesc = (LPOBJECTDESCRIPTOR)GlobalLock(hObj);
		ASSERT(pObjDesc != NULL);
		pItem->SetDrawAspect((DVASPECT)pObjDesc->dwDrawAspect);
		GlobalUnlock(hObj);
		GlobalFree(hObj);
	}

	// set top-left based on point of drop
	if (pPoint != NULL)
		pItem->m_ptPos = *pPoint;

	// get size from drag/drop operation
	CSize size;
	if (GetObjectInfo(pDataObject, &size, NULL) && size.cx != 0 && size.cy != 0)
	{
		// use size obtained from object instead of default
		size.cx = MulDiv(size.cx, 10, 254);
		size.cy = -MulDiv(size.cy, 10, 254);
		pItem->m_sizeCur=size;
		CSize sizeExtent;
		pItem->GetCachedExtent(&sizeExtent);
		pItem->m_sizeOrg=sizeExtent;
	}
	else
	{
		// no extent from CF_OBJECTDESCRIPTOR, use extent from object
		pItem->UpdateExtent();
	}
}

BOOL CAxClientView::GetObjectInfo(COleDataObject* pDataObject,
	CSize* pSize, CSize* pOffset)
{
	ASSERT(pSize != NULL);

	// get object descriptor data
	HGLOBAL hObjDesc = pDataObject->GetGlobalData(m_cfObjectDescriptor);
	if (hObjDesc == NULL)
	{
		if (pOffset != NULL)
			*pOffset = CSize(0, 0); // fill in defaults instead
		*pSize = CSize(0, 0);
		return FALSE;
	}
	ASSERT(hObjDesc != NULL);

	// otherwise, got CF_OBJECTDESCRIPTOR ok.  Lock it down and extract size.
	LPOBJECTDESCRIPTOR pObjDesc = (LPOBJECTDESCRIPTOR)GlobalLock(hObjDesc);
	ASSERT(pObjDesc != NULL);
	pSize->cx = (int)pObjDesc->sizel.cx;
	pSize->cy = (int)pObjDesc->sizel.cy;
	if (pOffset != NULL)
	{
		pOffset->cx = (int)pObjDesc->pointl.x;
		pOffset->cy = (int)pObjDesc->pointl.y;
	}
	GlobalUnlock(hObjDesc);
	GlobalFree(hObjDesc);

	// successfully retrieved pSize & pOffset info
	return TRUE;
}

void CAxClientView::OnEditPaste() 
{
	// TODO: Add your command handler code here
	if (DoPasteItem(FALSE, NULL, NULL) == NULL)
		AfxMessageBox("粘贴对象错误!");
	
}

void CAxClientView::OnUpdateEditPaste(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	COleDataObject dataObj;
	BOOL bEnable = dataObj.AttachClipboard();
	// enable command based on availability
	pCmdUI->Enable(bEnable);
}

void CAxClientView::OnEditDel() 
{
	// TODO: Add your command handler code here
	if(m_pSelection != NULL)
	{
		CRectTracker trackerTemp;
		CRect rect;
		SetupTracker(&trackerTemp,m_pSelection);
		trackerTemp.GetTrueRect(rect);

		m_pSelection->Delete();
		m_pSelection=NULL;
		
		InvalidateRect(rect);
	}
}

void CAxClientView::OnUpdateEditDel(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(m_pSelection != NULL);	
}

void CAxClientView::OnEditCut() 
{
	// TODO: Add your command handler code here
	ASSERT(m_pSelection != NULL);
	TRY
	{
		m_pSelection->CopyToClipboard(TRUE);
		OnEditDel();
	}
	CATCH_ALL(e)
	{
		AfxMessageBox("剪切操作失败!");
	}
	END_CATCH_ALL
	
}

void CAxClientView::OnUpdateEditCut(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(m_pSelection != NULL);	
}

void CAxClientView::OnEditCopy() 
{
	// TODO: Add your command handler code here
	ASSERT(m_pSelection != NULL);
	TRY
	{
		m_pSelection->CopyToClipboard(TRUE);
	}
	CATCH_ALL(e)
	{
		AfxMessageBox("复制工作失败!");
	}
	END_CATCH_ALL
	
}

void CAxClientView::OnUpdateEditCopy(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(m_pSelection != NULL);	
}

void CAxClientView::OnEditPasteSpecial() 
{
	// TODO: Add your command handler code here
	COlePasteSpecialDialog dlg;
	dlg.AddStandardFormats();
	if (dlg.DoModal() != IDOK)
		return;

	CAxClientCntrItem* pItem = NULL;
	TRY
	{
		// Get the clipboard format of the selected
		CLIPFORMAT cf = dlg.m_ps.arrPasteEntries[dlg.m_ps.nSelectedIndex].fmtetc.cfFormat;
		pItem = new CAxClientCntrItem;
		if (!dlg.CreateItem(pItem))
		{
			TRACE0("Warning: paste special failed to create item.\n");
			AfxThrowMemoryException();
		}

		// try to get initial presentation data
		pItem->UpdateLink();

		// try to get initial extent
		pItem->UpdateExtent();

		// set the selection with bSafeSelect = TRUE
		SetSelection(pItem);
		GetDocument()->SetModifiedFlag();
		GetDocument()->UpdateAllViews(NULL, 0, pItem);
	}
	CATCH_ALL(e)
	{
		// cleanup item, if allocated
		if (pItem != NULL)
			pItem->Delete();
		AfxMessageBox(IDP_FAILED_TO_CREATE);
		return;
	}
	END_CATCH_ALL
}

void CAxClientView::OnUpdateEditPasteSpecial(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	COleDataObject dataObj;
	BOOL bEnable = dataObj.AttachClipboard();
	// enable command based on availability
	pCmdUI->Enable(bEnable);
}


void CAxClientView::OnEditPasteLink() 
{
	// TODO: Add your command handler code here
	if (DoPasteItem(TRUE, NULL, NULL) == NULL)
		AfxMessageBox("粘贴链接失败!");
	
}

class COlePropertiesEx : public COlePropertiesDialog
{
public:
	COlePropertiesEx(COleClientItem* pItem,
		UINT nScaleMin = 10, UINT nScaleMax = 500, CWnd* pParentWnd = NULL)
		: COlePropertiesDialog(pItem, nScaleMin, nScaleMax, pParentWnd)
		{ }

	virtual BOOL OnApplyScale(
		COleClientItem* pItem, int nCurrentScale, BOOL bRelativeToOrig);
};

//////////////////////////////////////////////////////////////////////////
//	在属性对话框中实现比例放缩
//	pItem:				指向对象的指针,包含对象的信息
//	nCurrentScale:		取得用户所选的放缩比例
//	bRelativeToOrig:	判断是否按原始尺寸进行放缩

BOOL COlePropertiesEx::OnApplyScale(
	COleClientItem* pItemIn, int nCurrentScale, BOOL bRelativeToOrig)
{
	if (nCurrentScale != -1)
	{
		ASSERT_VALID(pItemIn);
		CAxClientCntrItem* pItem=(CAxClientCntrItem*)pItemIn;
		pItem->GetDocument()->UpdateAllViews(NULL, 0, pItem);

		// reset to original size if necessary
		//	根据原始尺寸进行放缩
		if (bRelativeToOrig)
		{
			pItem->m_sizeOrg=CSize(0,0);
			pItem->UpdateExtent();
		}			

		// update extent to reflect scaling factor
		pItem->GetDocument()->UpdateAllViews(NULL, 0, pItem);
		
		//	根据用户所选的比例计算放缩后的尺寸
		CSize size=pItem->m_sizeCur;
		size.cx = MulDiv(size.cx, nCurrentScale, 100);
		size.cy = MulDiv(size.cy, nCurrentScale, 100);
		pItem->m_sizeCur=size;
		
		pItem->GetDocument()->UpdateAllViews(NULL, 0, pItem);
	}
	return TRUE;
}

void CAxClientView::OnOleEditProperties() 
{
	// TODO: Add your command handler code here
	if(m_pSelection==NULL)
		return;

//	COlePropertiesDialog dlg(m_pSelection);
	COlePropertiesEx dlg(m_pSelection);
	dlg.DoModal();
	
}

void CAxClientView::OnUpdateOleEditProperties(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(m_pSelection != NULL);	
}

void CAxClientView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	GetParentFrame()->ActivateFrame();

	SetSelection(HitTestItems(point));    // reselect item if appropriate
	UpdateWindow();

	if (m_pSelection != NULL)
	{
		CMenu bar;
		if (bar.LoadMenu(IDR_MENU_OBJECT))
		{
			CMenu& popup = *bar.GetSubMenu(0);
			ASSERT(popup.m_hMenu != NULL);

			ClientToScreen(&point);
			popup.TrackPopupMenu(TPM_RIGHTBUTTON,
				point.x, point.y,
				AfxGetMainWnd()); // route commands through main window
		}
	}
	
	CScrollView::OnRButtonDown(nFlags, point);
}

⌨️ 快捷键说明

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