如何拍摄单个CWnd对象的屏幕截图? [英] How can I take a screenshot of a single CWnd object?

查看:86
本文介绍了如何拍摄单个CWnd对象的屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我可以通过以下代码捕获整个屏幕:

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);



也就是说,如何拍摄单个CWnd对象的屏幕截图?

谢谢!



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

Thank you!

推荐答案

1)如果希望CWnd完全占据屏幕上显示的区域,请使用CWnd :: GetWindowRect()http://msdn.microsoft.com/zh-CN/library/f3ef9c0s(v=VS. 80).aspx [ http://msdn.microsoft.com/zh-CN/library/90z8a14x(v = VS.80).aspx [
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.


这篇关于如何拍摄单个CWnd对象的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆