准备工作
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;
}
BOOL CWordToJPG::SaveBitmapToFile(HBITMAP hBitmap, LPCTSTR FileName)
{
if(hBitmap==NULL || FileName == NULL || strlen(FileName) == 0)
{
return false;
}
HDC hDC;
int iBits;
WORD wBitCount;
DWORD dwPaletteSize=0, dwBmBitsSize=0, dwDIBSize=0, dwWritten=0;
BITMAP Bitmap;
BITMAPFILEHEADER bmfHdr;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
HANDLE fh, hDib, hPal,hOldPal=NULL;
hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
DeleteDC(hDC);
if (iBits <= 1) wBitCount = 1;
else if (iBits <= 4) wBitCount = 4;
else if (iBits <= 8) wBitCount = 8;
else wBitCount = 24;
GetObject(hBitmap, sizeof(Bitmap), (LPSTR)&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrImportant = 0;
bi.biClrUsed = 0;
dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) / 32) * 4 * Bitmap.bmHeight;
hDib = GlobalAlloc(GHND,dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
hPal = GetStockObject(DEFAULT_PALETTE);
if (hPal)
{
hDC = ::GetDC(NULL);
hOldPal = ::SelectPalette(hDC, (HPALETTE)hPal, FALSE);
RealizePalette(hDC);
}
GetDIBits(hDC, hBitmap, 0, (UINT) Bitmap.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER)
+dwPaletteSize, (BITMAPINFO *)lpbi, DIB_RGB_COLORS);
if (hOldPal)
{
::SelectPalette(hDC, (HPALETTE)hOldPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL, hDC);
}
fh = CreateFile(FileName, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh == INVALID_HANDLE_VALUE) return FALSE;
bmfHdr.bfType = 0x4D42; // "BM"
dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
WriteFile(fh, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(fh);
return TRUE;
}