MFC saves the view file to the PPT

Source: Internet
Author: User

MFC saves the view file to the PPT
Use the msppt. h and msppt. cpp files provided by Microsoft to develop software on ppt. In this example, the View is saved to the ppt as an image. First, convert the View into a CBitmap object, and then save the obtained image to the file. Then, write the saved bitmap file to the ppt. For details, see the code;
Main function code: OnFileSave () // called when you click Save menu
[Cpp] view plaincopy
Void CSaveToPPTView: OnFileSave ()
{
// TODO: Add your command handler code here
CClientDC client (this );
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 ("the project is saved successfully! ");
Long n = pres. GetSaved ();
CString str = pres. GetPath ();
Pres. SetSaved (-1 );
Pres. Save ();
}
Main function code: SaveBMPToFile (HBITMAP hBitmap, LPSTR lpFileName)
[Cpp] view plaincopy
BOOL CSaveToPPTView: SaveBMPToFile (HBITMAP hBitmap, LPSTR lpFileName)
{
HDC hDC; // device description
Int iBits; // The number of bytes occupied by each pixel at the current display resolution.
WORD wBitCount; // The number of bytes occupied by each pixel in the bitmap.
// Defines the color palette size, pixel bytes in the bitmap, bitmap file size, and number of written file bytes.
DWORD dwPaletteSize = 0, dwBmBitsSize, dwDIBSize, dwWritten;
BITMAP Bitmap; // BITMAP attribute Structure
BITMAPFILEHEADER bmfHdr; // bitmap file header Structure
BITMAPINFOHEADER bi; // bitmap information header Structure
LPBITMAPINFOHEADER lpbi; // point to the bitmap information header Structure

HANDLE fh, hDib, hPal;
HPALETTE hOldPal = NULL; // defines the file, allocates memory handles, and color palette handles.

// Calculate the number of bytes occupied by each pixel in a bitmap file
HDC = CreateDC (_ T ("DISPLAY"), 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;
// Calculate the palette size
If (wBitCount <= 8) dwPaletteSize = (1 <wBitCount) * sizeof (RGBQUAD );

// Set the bitmap information header Structure
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; // allocate memory for the Bitmap content

/* Xxxxxxxx calculate the bitmap size decomposition (explain the preceding statement) xxxxxxxxxxxxxxxxxxxx
// The number of bytes for each scan row should be an integer multiple of 4. The specific algorithm is as follows:
Int biWidth = (Bitmap. bmWidth * wBitCount)/32;
If (Bitmap. bmWidth * wBitCount) % 32)
BiWidth ++; // not an integer plus 1
BiWidth * = 4; // here, the calculated number of bytes for each scan row.
DwBmBitsSize = biWidth * Bitmap. bmHeight; // obtain the size xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */


HDib = GlobalAlloc (GHND, dwBmBitsSize + dwPaletteSize + sizeof (BITMAPINFOHEADER ));
Lpbi = (LPBITMAPINFOHEADER) GlobalLock (hDib );
* Lpbi = bi;
// Process the palette
HPal = GetStockObject (DEFAULT_PALETTE );
If (hPal)
{
HDC =: GetDC (NULL );
HOldPal = SelectPalette (hDC, (HPALETTE) hPal, FALSE );
RealizePalette (hDC );
}
// Obtain the new pixel value under the color palette
GetDIBits (hDC, hBitmap, 0, (UINT) Bitmap. bmHeight, (LPSTR) lpbi + sizeof (BITMAPINFOHEADER) + dwPaletteSize, (BITMAPINFO *) lpbi, DIB_RGB_COLORS );
// Restore the palette
If (hOldPal)
{
SelectPalette (hDC, hOldPal, TRUE );
RealizePalette (hDC );
: ReleaseDC (NULL, hDC );
}
// Create a bitmap file
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;
// Set the bitmap file header
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;
// Write the bitmap file header
WriteFile (fh, (LPSTR) & bmfHdr, sizeof (BITMAPFILEHEADER), & dwWritten, NULL );
// Write the remaining contents of the bitmap file
WriteFile (fh, (LPSTR) lpbi, sizeof (BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize, & dwWritten, NULL );
// Clear
GlobalUnlock (hDib );
GlobalFree (hDib );
CloseHandle (fh );
Return true;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.