<strong> //获取桌面窗体的CDC
CDC *pdeskdc = GetDesktopWindow()->GetDC();
CRect re;
//获取窗体的大小
GetDesktopWindow()->GetClientRect(&re);
CBitmap bmp;
bmp.CreateCompatibleBitmap(pdeskdc , re.Width() , re.Height());
//创建一个兼容的内存画板
CDC memorydc;
memorydc.CreateCompatibleDC(pdeskdc);
//选中画笔
CBitmap *pold = memorydc.SelectObject(&bmp);
//绘制图像
memorydc.BitBlt(0,0,re.Width() ,re.Height(), pdeskdc , 0 ,0 ,SRCCOPY) ;
//获取鼠标位置,然后加入鼠标图像
CPoint po;
GetCursorPos(&po);
HICON hinco = (HICON)GetCursor();
memorydc.DrawIcon(po.x-10 , po.y - 10 , hinco);
//选中原来的画笔
memorydc.SelectObject(pold);
BITMAP bit;
bmp.GetBitmap(&bit);
// DWORD size = bit.bmWidth * bit.bmHeight ;
//定义 图像大小(单位:byte)
DWORD size = bit.bmWidthBytes * bit.bmHeight ;
LPSTR lpdata = (LPSTR)GlobalAlloc(GPTR , size) ;
//后面是创建一个bmp文件的必须文件头,想要了解能够參考msdn
BITMAPINFOHEADER pbitinfo;
pbitinfo.biBitCount = 24 ;
pbitinfo.biClrImportant = 0;
pbitinfo.biCompression = BI_RGB ;
pbitinfo.biHeight = bit.bmHeight ;
pbitinfo.biPlanes = 1 ;
pbitinfo.biSize = sizeof(BITMAPINFOHEADER);
pbitinfo.biSizeImage =size;
pbitinfo.biWidth = bit.bmWidth;
pbitinfo.biXPelsPerMeter = 0;
pbitinfo.biYPelsPerMeter = 0 ;
GetDIBits(pdeskdc->m_hDC , bmp , 0 , pbitinfo.biHeight , lpdata ,
(BITMAPINFO*)&pbitinfo,DIB_RGB_COLORS);
BITMAPFILEHEADER bfh;
bfh.bfReserved1 = bfh.bfReserved2 = 0 ;
bfh.bfType = ((WORD)('M'<< 8)|'B');
bfh.bfSize = 54 + size ;
bfh.bfOffBits = 54 ;
//写入文件
CFile file;
if ( file.Open("1.bmp" , CFile::modeCreate|CFile::modeWrite) )
{
file.WriteHuge( &bfh , sizeof(BITMAPFILEHEADER) );
file.WriteHuge(&pbitinfo , sizeof(BITMAPINFOHEADER));
file.WriteHuge(lpdata , size);
file.Close();
}
GlobalFree(lpdata);</strong>