CWnd* pwnd=GetDlgItem(IDC_STATICDRAW);//获取静态控件的指针 CRect pRect;
// CWnd* pWnd=GetDlgItem(IDC_STATICDRAW);
CDC* pDC=pwnd->GetDC(); //使用控件指针创建绘图用的DC
// pwnd->Invalidate();
// pwnd->UpdateWindow();
pwnd->GetClientRect(&pRect);
int w, h;
w = pRect.right - pRect.left;
h = pRect.bottom - pRect.top;
CBitmap bm;
bm.CreateCompatibleBitmap(pDC, w, h);
CDC memdc;
memdc.CreateCompatibleDC(pDC);
CBitmap*pOld=memdc.SelectObject(&bm);
memdc.BitBlt( 0, 0, w, h, pDC, pRect.left, pRect.top, SRCCOPY );
BITMAP btm;
bm.GetBitmap(&btm);
DWORD size=btm.bmWidthBytes*btm.bmHeight;
LPSTR lpData=(LPSTR)::GlobalAlloc(GPTR,size);
BITMAPINFOHEADER bih;
bih.biBitCount=btm.bmBitsPixel;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=0;
bih.biHeight=btm.bmHeight;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=size;
bih.biWidth=btm.bmWidth;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
GetDIBits(memdc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
BITMAPFILEHEADER bfh;
bfh.bfReserved1=bfh.bfReserved2=0;
bfh.bfType=((WORD)('M'<< 8)|'B');
bfh.bfSize=54+size;
bfh.bfOffBits=54;
CFile bf;
if(bf.Open("a.bmp",CFile::modeCreate|CFile::modeWrite))
{
bf.Write(&bfh,sizeof(BITMAPFILEHEADER));
bf.Write(&bih,sizeof(BITMAPINFOHEADER));
bf.Write(lpData,size);
bf.Close();
MessageBox(_T("保存成功!"));
}
else
{
MessageBox(_T("保存失败!"));
}
memdc.SelectObject( pOld );
bm.DeleteObject( );
memdc.DeleteDC( );
::GlobalFree(lpData);
完毕