稍微改改可以封装一个函数
// TODO: Add your control notification handler code here
/*if(!m_IsPath)
{
MessageBox("请选择文件存储位置");
return;
}*/
CDC* pDeskDC = GetDesktopWindow()->GetDC(); //获取桌面画布对象
CRect rc;
GetDesktopWindow()->GetClientRect(rc); //获取屏幕的客户区域
CDC memDC; //定义一个内存画布
memDC.CreateCompatibleDC(pDeskDC); //创建一个兼容的画布
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDeskDC,rc.Width(),rc.Height()); //创建兼容位图
memDC.SelectObject(&bmp); //选中位图对象
BITMAP bitmap;
bmp.GetBitmap(&bitmap);
memDC.BitBlt(0,0,bitmap.bmWidth,bitmap.bmHeight,pDeskDC,0,0,SRCCOPY);
//添加鼠标
CPoint point;
GetCursorPos(&point);
HICON hicon = (HICON)GetCursor();
memDC.DrawIcon(point,hicon);
DWORD size=bitmap.bmWidthBytes*bitmap.bmHeight;
LPSTR lpData=(LPSTR)GlobalAllocPtr(GPTR,size);
int panelsize = 0; //记录调色板大小
if(bitmap.bmBitsPixel<16) //判断是否为真彩色位图
panelsize = pow(2,bitmap.bmBitsPixel*sizeof(RGBQUAD));
BITMAPINFOHEADER *pBInfo = (BITMAPINFOHEADER*)LocalAlloc(LPTR,
sizeof(BITMAPINFO)+panelsize);
pBInfo->biBitCount = bitmap.bmBitsPixel;
pBInfo->biClrImportant = 0;
pBInfo->biCompression = 0;
pBInfo->biHeight = bitmap.bmHeight;
pBInfo->biPlanes = bitmap.bmPlanes;
pBInfo->biSize = sizeof(BITMAPINFO);
pBInfo->biSizeImage = bitmap.bmWidthBytes*bitmap.bmHeight;
pBInfo->biWidth = bitmap.bmWidth;
pBInfo->biXPelsPerMeter = 0;
pBInfo->biYPelsPerMeter = 0;
GetDIBits(memDC.m_hDC,bmp,0,pBInfo->biHeight,lpData,
(BITMAPINFO*)pBInfo,DIB_RGB_COLORS);
CString path,name,m_number;
//m_Path.GetWindowText(path);
CTime time = CTime::GetCurrentTime();
path.Format("D://");
name = path+"//"+time.Format("%Y%m%d%H%M%S")+".bmp";
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(name,CFile::modeCreate|CFile::modeWrite))
{
file.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
file.WriteHuge(pBInfo,sizeof(BITMAPINFOHEADER));
file.WriteHuge(lpData,size);
file.Close();
}

474

被折叠的 条评论
为什么被折叠?



