如何 截取被遮挡窗口图片

jwqih1 2010-11-08 03:32:00
Delphi 截取被遮挡窗口图片
Delphi截图,Delphi 截取被遮挡窗口图片,Delphi遮挡窗口截图
使用dELPHI用TCanvas 的CopyRect或者bitblt 对窗口进行截图的时候窗口对象容易被遮挡,
截出来的图像混杂着遮挡窗体,PrintWindow这个个函数可以帮我们
解决掉这个问题,不管是隐藏的窗体还是北遮挡的窗体都可以截取的到。
dELPHI中没有声明这个函数,需要我们自己动手声明

function PrintWindow(SourceWindow: hwnd; Destination: hdc; nFlags: cardinal): bool; stdcall; external 'user32.dll' name 'PrintWindow';


procedure TForm1.Button2Click(Sender: TObject);
var
bmp: TBitmap;
wnd: cardinal;
rec: TRect;
begin
wnd := FindWindow(nil, '计算器'); // 查找窗口句柄,这里用计算器演示
GetWindowRect(wnd, rec); // 获取到计算器窗口的举行
bmp := TBitmap.Create;
try
bmp.Width := rec.Right - rec.Left;
bmp.Height := rec.Bottom - rec.Top;
bmp.PixelFormat := pf24bit;
PrintWindow(wnd, bmp.Canvas.Handle, 0);
bmp.SaveToFile('cao.bmp');
finally
bmp.Free;
end;
end;
...全文
901 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
printwindow也不一定行啊,在mdi父窗口覆盖的情形下,就不行了。
rover___ 2010-12-26
  • 打赏
  • 举报
回复
FindWindowW
good778899 2010-12-23
  • 打赏
  • 举报
回复
我在BCB6.0中使用PrintWindow(),并引用了WinUser.h,但找不到PrintWindow(),请问怎么才能用得了这个函数?
grf9527 2010-11-10
  • 打赏
  • 举报
回复
楼上的这个老兄,很帅气,代码是对的,呵呵
百事烟 2010-11-10
  • 打赏
  • 举报
回复
void Cvc2005Dlg::OnBnClickedButton1()
{
// 目标窗体不能最小化
HWND src_hWnd = ::FindWindowW(NULL, L"计算器");
RECT rc;
::GetWindowRect(src_hWnd, &rc);

// 24位图的BITMAPINFO
BITMAPINFO *pBITMAPINFO = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER));
memset(pBITMAPINFO, 0, sizeof(BITMAPINFOHEADER));
BITMAPINFOHEADER *pInfo_Header = (BITMAPINFOHEADER *)pBITMAPINFO;
pInfo_Header->biSize = sizeof(BITMAPINFOHEADER);
pInfo_Header->biWidth = rc.right - rc.left;
pInfo_Header->biHeight = rc.bottom - rc.top;
pInfo_Header->biPlanes = 1;
pInfo_Header->biBitCount = 24;
pInfo_Header->biCompression = BI_RGB;

// 创建兼容DC 打印目标窗体
HDC src_hdc = ::GetWindowDC(src_hWnd);
HBITMAP hBitmap = ::CreateCompatibleBitmap(src_hdc,
pInfo_Header->biWidth, pInfo_Header->biHeight);
HDC hdcCompatible = CreateCompatibleDC(src_hdc);
::SelectObject(hdcCompatible, hBitmap);
::PrintWindow(src_hWnd, hdcCompatible, 0);
CDC *pdcCompatible = CDC::FromHandle(hdcCompatible);
::ReleaseDC(src_hWnd, src_hdc);


// 把兼容DC贴到DC上
CDC *pCurDC = this->GetDC();
pCurDC->BitBlt(0,0,pInfo_Header->biWidth, pInfo_Header->biHeight,pdcCompatible,0,0,SRCCOPY);

// 获得数据buf
DWORD bufSize = (pInfo_Header->biWidth * 3 + 3) / 4 * 4 * pInfo_Header->biHeight;
BYTE * pBuffer = new BYTE[bufSize];
if(GetDIBits(hdcCompatible, hBitmap, 0, pInfo_Header->biHeight, pBuffer,
pBITMAPINFO, DIB_RGB_COLORS) == 0)
{
AfxMessageBox(L"GetDIBits");
}

// 建立文件
CFile file;
file.Open(L"D:\\新图.bmp", CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);

// 写文件头
BITMAPFILEHEADER File_Header;
File_Header.bfType = 0x4d42;
File_Header.bfOffBits = sizeof(BITMAPFILEHEADER) + pInfo_Header->biSize;
File_Header.bfSize = File_Header.bfOffBits + bufSize;
File_Header.bfReserved1 = 0;
File_Header.bfReserved2 = 0;
file.Write(&File_Header, sizeof(BITMAPFILEHEADER));

// 写数据头
file.Write(pInfo_Header, sizeof(BITMAPINFOHEADER));

// 写数据
file.Write(pBuffer, bufSize);

// 关闭文件 释放内存
file.Close();
delete []pBuffer;
free(pBITMAPINFO);
::DeleteObject(hdcCompatible);
::DeleteObject(hBitmap);
this->ReleaseDC(pCurDC);
}
jwqih1 2010-11-08
  • 打赏
  • 举报
回复
怎么用C++实现啊~!!!!!!!

19,469

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧