VC将WORD保存为BMP

准备工作

ClassWizeard->AddClass->From TypeLibrary 选择OFFICE安装目录下的msword.olb  导入_Application,_Document,Documents,Paragraphs,Range

第一步将WORD内容复制到剪贴板

BOOL CWordToJPG::CopyWordToClipboard(LPCTSTR lpszFileName)
{
	COleVariant vTrue(( short )TRUE), vFalse(( short )FALSE), vOpt(( long )DISP_E_PARAMNOTFOUND, VT_ERROR); 

	_Application wordApp = ((CMakeQLibExApp *)AfxGetApp())->GetWordApplication();
	//BOOL bRet = wordApp.CreateDispatch(_T("Word.Application"));

	Documents docs = wordApp.GetDocuments();
	
	COleVariant FileName(lpszFileName);	
	_Document tempDoc = docs.Open(FileName,vFalse,vTrue,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt); 
	
	Paragraphs tempPgs = tempDoc.GetParagraphs();
	Range aRange = tempDoc.Range(vOpt,vOpt);
	aRange.Copy();


	aRange.ReleaseDispatch();


	tempPgs.ReleaseDispatch();

	tempDoc.Close(vOpt,vOpt,vOpt);
	tempDoc.ReleaseDispatch();

	docs.ReleaseDispatch();


	return TRUE;
}

 

HBITMAP CWordToJPG::SaveClipboardToBitmap()
{
	
	if (!::OpenClipboard(NULL))
	{
		return NULL;
	}
	
	UINT nRet = EnumClipboardFormats(CF_ENHMETAFILE);
	if (nRet)
	{
		

		HANDLE   hClip=::GetClipboardData(CF_ENHMETAFILE);			
		HENHMETAFILE hEnhMetaFile=(HENHMETAFILE)hClip; 		 
		

		
		CString strEmf = "c:\\word_meta_temp.emf";
		::DeleteFile(strEmf);

		HENHMETAFILE   hMetaFile=CopyEnhMetaFile(hEnhMetaFile, strEmf);//
		


		SIZE size = GetEMFInfo(strEmf);

		
		GLOBALHANDLE hGMem = GetClipboardData(CF_METAFILEPICT);
		LPMETAFILEPICT lpMFP = (LPMETAFILEPICT)GlobalLock(hGMem);
		
		
		ENHMETAHEADER emh;
		ZeroMemory( &emh, sizeof(ENHMETAHEADER) );   
		emh.nSize = sizeof(ENHMETAHEADER);   
		
		if( GetEnhMetaFileHeader(hMetaFile, sizeof( ENHMETAHEADER ), &emh ) == 0 )   
		{   
			DeleteEnhMetaFile( hMetaFile );		 
			return NULL ;   
		}   
		DeleteEnhMetaFile( hMetaFile );	


		
		HDC		hDC = ::CreateDC("DISPLAY", NULL, NULL, NULL);
		HDC		hMemDC = ::CreateCompatibleDC(hDC);

		CDC *pDC=CDC::FromHandle(hMemDC);
		int lWidth = size.cx;
		int lHeight = size.cy;

		CRect metafileRect(0,0,lWidth,lHeight);
		
		HBITMAP hMemBitmap = CreateCompatibleBitmap(hDC,lWidth,lHeight);
		HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hMemDC,hMemBitmap);
		

		
	
		pDC->SetMapMode(lpMFP->mm);
		pDC->FillSolidRect(metafileRect,RGB(255,255,255));
		pDC->SetViewportExt(metafileRect.Width(), metafileRect.Height());
		pDC->SetViewportOrg(metafileRect.left, metafileRect.top);
		pDC->PlayMetaFile(lpMFP->hMF);
	
		GlobalUnlock(hGMem);


		EmptyClipboard();
		CloseClipboard();


		BOOL bAutoCut = TRUE;	
		CRect rcClip;


		pDC->SetMapMode(MM_TEXT);

		int xPos,yPos;


		CRect rcClip(0,0,lWidth,lHeight);			
		HDC		hDCTemp		 = CreateCompatibleDC(hMemDC);
		HBITMAP	hBitmapRslt	 = CreateCompatibleBitmap(hDC,rcClip.Width(),rcClip.Height());
		SelectObject(hDCTemp,hBitmapRslt);
		BitBlt(hDCTemp,0,0,rcClip.Width(),rcClip.Height(),hMemDC,rcClip.left,rcClip.top,SRCCOPY);

		


		::SelectObject(hMemDC,hOldBitmap);
		
		::DeleteDC(hMemDC);
		
		::CloseClipboard();
		
		::DeleteObject(hMemBitmap);
		
		::DeleteDC(hDC);
		::DeleteDC(hDCTemp);


	

		
		 return hBitmapRslt;
	}
	
	






	return NULL;
}
</pre><pre class="cpp" name="code">


 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页