我正在使用MFC
under vs2010
,我想将the view
显示另存为a picture
,然后the picture
插入a document in MS-WORD format
. 但很少APIs
或Documents
我能找到。你有什么建议吗?
问问题
257 次
1 回答
2
大致沿着这条线:
// Pick your own size of rectangle.
CRect rect;
GetWindowRect(&rect);
CDC memDC;
memDC.CreateCompatibleDC(NULL);
BITMAPINFO info;
memset(&info, 0, sizeof(info));
// About the only parts of this you're likely to change
// are the width and height. You can change to fewer bits
// per pixel if you want -- doing so reduces file sizes,
// but increases headaches. In a BMP file, each scan line
// has to be DWORD aligned. If you use 32 bit per pixel,
// each pixel is a DWORD, so the alignment happens
// automatically. Otherwise, you have to add padding as
// as needed, which is kind of a pain.
//
info.bmiHeader.biSize = sizeof(info.bmiHeader);
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = 16;
info.bmiHeader.biCompression = BI_RGB;
info.bmiHeader.biWidth = rect.Width();
info.bmiHeader.biHeight = rect.Height();
char *bits = NULL;
HBITMAP section = CreateDIBSection(
pDC->m_hDC,
&info,
DIB_RGB_COLORS,
(void **)&bits,
NULL, 0);
memDC.SelectObject(section);
// Draw something into the DIB.
CBrush brush;
brush.CreateSolidBrush(RGB(0, 0, 255));
CRect temp(0,0,rect.Width(), rect.Height());
memDC.FillRect(temp, &brush);
memDC.SetBkMode(TRANSPARENT);
memDC.SetTextColor(RGB(255, 0, 0));
// Copy the DIB to the screen, just because we can...
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);
// Ensure the GDI is done with anything it's doing
// with the DIB before we access the bits directly.
GdiFlush();
// Done drawing. Now save it:
FILE *file = _wfopen(L"junk.bmp",L"wb");
if ( NULL == file)
memDC.TextOut(0, 0, "File didn't open");
else
memDC.TextOut(0, 0, "File opened");
// Get the size of the DIB, even though in this case
// we know it already because we just created it above.
BITMAP object;
GetObject(section, sizeof(object), &object);
DWORD bit_size = object.bmWidthBytes * object.bmHeight;
DWORD header_size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// A BMP file header. Pretty basic, really.
BITMAPFILEHEADER header;
memset(&header, 0, sizeof(header));
header.bfType = 'MB';
header.bfSize = bit_size + header_size;
header.bfOffBits = header_size;
// Write out the file header.
fwrite(&header, 1, sizeof(header), file);
// Write out the image header.
fwrite(&info, 1, sizeof(info), file);
// write out the bits.
fwrite(bits, 1, bit_size, file);
// and we're done.
fclose(file);
于 2013-02-02T02:15:22.870 回答