如何用MFC编写一个程序,获得整个屏幕的截图并保存为bmp格式?

本人mfc学得很差,几乎零基础,求解释得详细一点~~
2025-01-04 11:26:43
推荐回答(3个)
回答1:

代码 大概就是这样 有几个关键函数仔细一下,要是要完整代码呢留言
CDC memDC;
CBitmap memBitmap,*pOldBitmap;
CWindowDC dc(GetDesktopWindow());
memDC.CreateCompatibleDC(&dc);
int nWidth,nHeight;
/* nWidth=GetSystemMetrics(SM_CXSCREEN);
nHeight=GetSystemMetrics(SM_CYSCREEN);*/
CRect rect;
GetDesktopWindow()->GetWindowRect(rect);
ClientToScreen(&rect);
nWidth=rect.Width();
nHeight=rect.Height();
memBitmap.CreateCompatibleBitmap(&dc,nWidth,nHeight);
pOldBitmap=memDC.SelectObject(&memBitmap);
// memDC.StretchBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,rect.Width(),rect.Height(),SRCCOPY);
memDC.BitBlt(0,0,nWidth,nHeight,&dc,0,0,SRCCOPY);

BITMAPFILEHEADER BMFhead;
BITMAPINFOHEADER BMIhead;
BMFhead.bfReserved1=0;
BMFhead.bfReserved2=0;
BMFhead.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
BMFhead.bfSize=BMFhead.bfOffBits+nWidth*nHeight*4;
BMFhead.bfType=0x4d42;

BMIhead.biBitCount=32;
BMIhead.biClrImportant=0;
BMIhead.biClrUsed=0;
BMIhead.biSizeImage=0;
BMIhead.biXPelsPerMeter=0;
BMIhead.biYPelsPerMeter=0;
BMIhead.biCompression=BI_RGB;
BMIhead.biHeight=nHeight;
BMIhead.biPlanes=1;
BMIhead.biSize=sizeof(BITMAPINFOHEADER);
BMIhead.biWidth=nWidth;

DWORD dwSize=nWidth*nHeight*4;
BYTE *pData=new BYTE[dwSize];
GetDIBits(memDC.m_hDC,(HBITMAP)memBitmap.m_hObject,0,nHeight,pData,(LPBITMAPINFO)&BMIhead,DIB_RGB_COLORS);

CFile file;
file.Open("test.bmp",CFile::modeCreate | CFile::modeWrite);
file.Write(&BMFhead,sizeof(BITMAPFILEHEADER));
file.Write(&BMIhead,sizeof(BITMAPINFOHEADER));
file.Write(pData,dwSize);
file.Close();
memDC.SelectObject(pOldBitmap);
MessageBox("绘图完毕");
GetParent()->ShowWindow(SW_SHOW);

回答2:

给你代码,发哪里

回答3:

http://blog.csdn.net/zwmei/article/details/7440579
此文做了比较详细的解释,可以参考