现在的位置: 首页 > 综合 > 正文

MFC将视图文件保存到PPT中

2013年07月02日 ⁄ 综合 ⁄ 共 4538字 ⁄ 字号 评论关闭
MFC将视图文件保存到PPT中

源代码下载:http://download.csdn.net/detail/nuptboyzhb/4499649
借助微软提供的msppt.h 和 msppt.cpp文件,来开发有关ppt方面的软件。本例是将视图类View显示的画面,以图片的方式保存到ppt中。首先是将View转化为CBitmap对象中,然后将得到的图像保存到文件。再将保存的位图文件写入到ppt中。详见代码;
主要函数代码:OnFileSave() //当点击保存菜单时调用

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)

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;
}

http://blog.csdn.net/nupt123456789/article/details/7866152

抱歉!评论已关闭.