Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi everybody!

I'm able to take a capture of entire screen by this code:
HDC hScrDC = ::GetDC(NULL);
HDC hMemDC = NULL;
BYTE *lpBitmapBits = NULL;
int xstart = 0;
int ystart = 0;
int nWidth = GetSystemMetrics(SM_CXSCREEN);
int nHeight = GetSystemMetrics(SM_CYSCREEN); 
hMemDC = CreateCompatibleDC(hScrDC); 
BITMAPINFO bi; 
ZeroMemory(&bi, sizeof(BITMAPINFO));
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = nWidth;
bi.bmiHeader.biHeight = nHeight;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 24;
HBITMAP bitmap = CreateDIBSection(hMemDC, &bi, DIB_RGB_COLORS, (LPVOID*)&lpBitmapBits, NULL, 0);
HGDIOBJ oldbmp = SelectObject(hMemDC, bitmap); 
::BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, xstart, ystart, SRCCOPY);
BITMAPFILEHEADER bh;
ZeroMemory(&bh, sizeof(BITMAPFILEHEADER));
bh.bfType = 0x4d42; //bitmap 
bh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bh.bfSize = bh.bfOffBits + ((nWidth*nHeight)*3);
CFile file;
if(file.Open("sergio3.bmp", CFile::modeCreate | CFile::modeWrite))
{ 
	file.Write(&bh, sizeof(BITMAPFILEHEADER));
	file.Write(&(bi.bmiHeader), sizeof(BITMAPINFOHEADER));
	file.Write(lpBitmapBits, 3 * nWidth * nHeight);
	file.Close();
}
::SelectObject(hMemDC, oldbmp);
::DeleteObject(bitmap);
::DeleteObject(hMemDC);
::ReleaseDC(NULL, hScrDC);


That said, how can I take a screenshot of a single CWnd object?

Thank you!
Posted

1 solution

1) If you want the area the CWnd occupies exactly as it appears on the screen, use CWnd::GetWindowRect() http://msdn.microsoft.com/en-US/library/f3ef9c0s(v=VS.80).aspx[^] to get the position of the CWnd object that you are interested in and then extract just those pixels you are interested in from the code you have above.

or

2) If you want what the CWnd looks like irregardless of any other window obscuring it, use CWnd::Print() http://msdn.microsoft.com/en-US/library/90z8a14x(v=VS.80).aspx[^] and call it with a compatible DC that you created based on the size from GetClientRect.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900