MFC將視圖文件保存到PPT中
借助微軟提供的msppt.h 和 msppt.cpp文件,來開發有關ppt方面的軟件。本例是將視圖類View顯示的畫面,以圖片的方式保存到ppt中。首先是將View轉化為CBitmap對象中,然後將得到的圖像保存到文件。再將保存的位圖文件寫入到ppt中。詳見代碼;
主要函數代碼:OnFileSave() //當點擊保存菜單時調用
[cpp] view plaincopy
void CSaveToPPTView::OnFileSave()
{
// TODO: Add your command handler code here
CClientDC client(this);
CDC cdc;
CBitmap bitmap;
RECT rect;
GetClientRect(&rect);
int cx = rect.right - rect.left;
int cy = rect.bottom - rect.top;
bitmap.CreateCompatibleBitmap(&client, cx, cy);
cdc.CreateCompatibleDC(NULL);
CBitmap * oldbitmap = (CBitmap* ) cdc.SelectObject(&bitmap);
cdc.BitBlt(0, 0, cx, cy, &client, 0, 0, SRCCOPY);
cdc.SelectObject(oldbitmap);
// ::OpenClipboard(this-> m_hWnd);
// ::EmptyClipboard();
// ::SetClipboardData(CF_BITMAP, bitmap);
// ::CloseClipboard();
HBITMAP hbitmap=(HBITMAP)bitmap;
SaveBMPToFile(hbitmap,"c://temp.bmp");
//////////////////////////////////////////////////////////////////////////
_Application app;
COleException e;
if(!app.CreateDispatch("Powerpoint.Application", &e)) {
CString str;
str.Format("CreateDispatch() failed w/err 0x%08lx", e.m_sc),
AfxMessageBox(str, MB_SETFOREGROUND);
return;
}
// Make it visible.
app.SetVisible(TRUE);
// Get Presentations collection and add a new presentation.
Presentations presSet(app.GetPresentations());
_Presentation pres(presSet.Add(TRUE));
// Get Slides collection and add a new slide.
Slides slideSet(pres.GetSlides());
_Slide slide1(slideSet.Add(1, 1));
CString strPic1 ="C:\\temp.bmp";
Shapes shapes(slide1.GetShapes());
shapes.AddPicture(
strPic1, //Filename
(long)0, //LinkToFile
(long)-1, //SaveWithDocument
(float)40, //Left
(float)20, //Top
(float)650, //Width
(float)500 //Height
);
Shape shape(shapes.Item(COleVariant((long)1)));
TextFrame textFrame(shape.GetTextFrame());
TextRange textRange(textFrame.GetTextRange());
textRange.SetText("項目保存成功!");
long n=pres.GetSaved();
CString str = pres.GetPath();
pres.SetSaved(-1);
pres.Save();
}
主要函數代碼:SaveBMPToFile(HBITMAP hBitmap, LPSTR lpFileName)
[cpp] view plaincopy
BOOL CSaveToPPTView::SaveBMPToFile(HBITMAP hBitmap, LPSTR lpFileName)
{
HDC hDC; //設備描述表
int iBits; //當前顯示分辨率下每個像素所占字節數
WORD wBitCount; //位圖中每個像素所占字節數
//定義調色板大小, 位圖中像素字節大小 , 位圖文件大小 , 寫入文件字節數
DWORD dwPaletteSize=0,dwBmBitsSize,dwDIBSize, dwWritten;
BITMAP Bitmap; //位圖屬性結構
BITMAPFILEHEADER bmfHdr; //位圖文件頭結構
BITMAPINFOHEADER bi; //位圖信息頭結構
LPBITMAPINFOHEADER lpbi; //指向位圖信息頭結構
HANDLE fh, hDib, hPal;
HPALETTE hOldPal=NULL; //定義文件,分配內存句柄,調色板句柄
//計算位圖文件每個像素所占字節數
hDC = CreateDC(_T("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 if (iBits <= 24) wBitCount = 24;
else wBitCount = 32;
//計算調色板大小
if (wBitCount <= 8) dwPaletteSize = (1<<wBitCount) * sizeof(RGBQUAD);
//設置位圖信息頭結構
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.biClrUsed = 0;
bi.biClrImportant = 0;
dwBmBitsSize = ((Bitmap.bmWidth*wBitCount+31)/32)*4*Bitmap.bmHeight; //為位圖內容分配內存
/*xxxxxxxx計算位圖大小分解一下(解釋一下上面的語句)xxxxxxxxxxxxxxxxxxxx
//每個掃描行所占的字節數應該為4的整數倍,具體算法為:
int biWidth = (Bitmap.bmWidth*wBitCount) / 32;
if((Bitmap.bmWidth*wBitCount) % 32)
biWidth++; //不是整數倍的加1
biWidth *= 4;//到這裡,計算得到的為每個掃描行的字節數。
dwBmBitsSize = biWidth * Bitmap.bmHeight;//得到大小xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
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, hOldPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL, hDC);
}
//創建位圖文件
fh=CreateFile((LPCTSTR)lpFileName, 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)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
// 寫入位圖文件其余內容
WriteFile(fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize , &dwWritten, NULL);
//清除
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(fh);
return true;
}