MFC将视图文件保存到PPT中(一)

2014-11-24 09:52:24 · 作者: · 浏览: 2
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;