描述:
I am writing a COM container using MFC OLE classes. The purpose of the container
is to display and/or print MS Word doc as portion of a bigger display/plot. For
this I am trying to get the Enhance Metafile representation of the word doc
file using MSWord as COM Server. I have had limited success in the sense that
only first page of the document is visible in the metafile. Here is what I
have tried:
CMyContainerDoc* pDoc = new CMyContainerDoc; //derived from COleDocument
CMyCtrlItem* pCtrlItem = new CMyCtrlItem(pDoc); //derived from COleClientItem
pCtrlItem->CreateFromFile("mytestfile.doc"); //path and name of a MS word file
CSize clientSize;
BOOL bRet = pCtrlItem->GetCachedExtent(&clientSize, DVASPECT_CONTENT);
HDC hDC = ::CreateEnhMetaFile(NULL, "c:\\drawtest.emf", 0, NULL);
CDC::FromHandle(hDC)->HIMETRICtoLP(&clientSize);
CRect rect( 0, 0, clientSize.cx, clientSize.cy);
pCtrlItem->Draw(CDC::FromHandle(hDC), &rect, DVASPECT_CONTENT);
HENHMETAFILE hMetaFile = CloseEnhMetaFile( hDC );
DeleteEnhMetaFile( hMetaFile);
This results in just the first page of document to be represented in the metafile.
How could I get the whole document in the metafile?
2. Alternatively, I tried to use GetData function of IDataObject interface .
It returns success status code, but when I try to use the metafile handle, returned
through STGMEDIUM structure, it still contains only the first page. Here is the
relevant code:
FORMATETC fmtetc;
fmtetc.cfFormat = CF_METAFILEPICT;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.ptd = NULL;
fmtetc.tymed = TYMED_MFPICT;
fmtetc.lindex = -1;
STGMEDIUM stgMed;
memset(&stgMed, 0, sizeof(stgMed));
LPDATAOBJECT lpDataObject = 0;
pCtrlItem->m_lpObject->QueryInterface(IID_IDataObject, (void**)&lpDataObject);
sc = lpDataObject->GetData( &fmtetc, &stgMed );
LPMETAFILEPICT pMF = (LPMETAFILEPICT)GlobalLock(stgMed.hEnhMetaFile);
//Draw the picture.
HDC hDC2 = ::CreateEnhMetaFile(NULL, "c:\\testGetdata.emf", 0, NULL);
BOOL bRetPlay = PlayMetaFile(hDC2, pMF->hMF);
GlobalUnlock(stgMed.hEnhMetaFile);
ReleaseStgMedium(&stgMed);
HENHMETAFILE hMetaFile = CloseEnhMetaFile( hDC2 );
DeleteEnhMetaFile( hMetaFile );
Here again, the metafile testGetData.emf contains just the first page of the document.
Is there something I am missing here? Or if there is another better way of
achieving the intended results.
Thanks and Regards