截取当前活动窗口,但我现在想截取不包含菜单栏、工具栏这些窗体的图,请问有办法吗这是我截取当前窗口的代码
void CtestView::Screen(CString fileName)
{
CDC dc;
CDC *pDC= &dc;//屏幕DC
HWND hwnd = ::GetForegroundWindow();
HDC activeDC = ::GetWindowDC(hwnd);
//pDC = this->GetDesktopWindow()->GetDC();
pDC->Attach(activeDC);
RECT rect;
::GetWindowRect(hwnd,&rect);//得到窗口的大小
int Width = rect.right - rect.left;
int Height = rect.bottom - rect.top;
// int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDC);
CBitmap memBitmap, *oldmemBitmap;//建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC, Width, Height); oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC //以下代码保存memDC中的位图到文件
BITMAP bmp;
memBitmap.GetBitmap(&bmp);//获得位图信息
CFile file;
if(!file.Open(fileName,CFile::modeCreate|CFile::modeWrite))
{ #ifdef _DENUG
AfxMessageBox("打开文件错误"); #endif
return ;
} BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount =24;// bmp.bmBitsPixel;//每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;//高度 bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小
bih.biWidth = bmp.bmWidth;//宽度 BITMAPFILEHEADER bfh = {0};//位图文件头
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件总的大小
bfh.bfType = (WORD)0x4d42;
file.Write(&bfh,sizeof(BITMAPFILEHEADER));//写入位图文件头
file.Write(&bih,sizeof(BITMAPINFOHEADER));//写入位图信息头 byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据 GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//获取位图数据
file.Write(p,bmp.bmWidthBytes * bmp.bmHeight);
file.Close();
delete [] p; memDC.SelectObject(oldmemBitmap);
}
void CtestView::Screen(CString fileName)
{
CDC dc;
CDC *pDC= &dc;//屏幕DC
HWND hwnd = ::GetForegroundWindow();
HDC activeDC = ::GetWindowDC(hwnd);
//pDC = this->GetDesktopWindow()->GetDC();
pDC->Attach(activeDC);
RECT rect;
::GetWindowRect(hwnd,&rect);//得到窗口的大小
int Width = rect.right - rect.left;
int Height = rect.bottom - rect.top;
// int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDC);
CBitmap memBitmap, *oldmemBitmap;//建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC, Width, Height); oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC //以下代码保存memDC中的位图到文件
BITMAP bmp;
memBitmap.GetBitmap(&bmp);//获得位图信息
CFile file;
if(!file.Open(fileName,CFile::modeCreate|CFile::modeWrite))
{ #ifdef _DENUG
AfxMessageBox("打开文件错误"); #endif
return ;
} BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount =24;// bmp.bmBitsPixel;//每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;//高度 bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小
bih.biWidth = bmp.bmWidth;//宽度 BITMAPFILEHEADER bfh = {0};//位图文件头
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件总的大小
bfh.bfType = (WORD)0x4d42;
file.Write(&bfh,sizeof(BITMAPFILEHEADER));//写入位图文件头
file.Write(&bih,sizeof(BITMAPINFOHEADER));//写入位图信息头 byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据 GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//获取位图数据
file.Write(p,bmp.bmWidthBytes * bmp.bmHeight);
file.Close();
delete [] p; memDC.SelectObject(oldmemBitmap);
}
CDC *pDC= &dc;//屏幕DC
HWND hwnd = ::GetForegroundWindow();
HDC activeDC = ::GetWindowDC(hwnd);
//pDC = this->GetDesktopWindow()->GetDC();
pDC->Attach(activeDC);你用当前this->GetDC()这个出来的就是View的DC,不带任何菜单和工具栏的.
请尝试
后者有效地绘制区域仅限于客户区。