如何捕获特定窗口而不是总桌面?

时间:2010-04-16 10:24:51

标签: com visual-c++ atl

我使用CPP,COM和DirectShow捕获了整个桌面。但是我怎样才能捕获特定窗口?

2 个答案:

答案 0 :(得分:0)

我使用了一些助手 - 但这些应该很容易转化为你需要的东西:

// Copy the contents of the client area or the window into an HBITMAP
HBITMAP CaptureWindow(HWND hwnd, bool bIncludeFrame, const RECT * pClip)
{
    // get the DC of the source
    HDC hdcSource = bIncludeFrame ? ::GetWindowDC(hwnd) : ::GetDC(hwnd);

    // get a memory DC
    HDC hdcMemory = ::CreateCompatibleDC(hdcSource);

    // get the clipping rect
    RECT rcClip;
    if (pClip)
        rcClip = *pClip;
    else if (bIncludeFrame)
        ::GetWindowRect(hwnd, &rcClip);
    else
        ::GetClientRect(hwnd, &rcClip);

    // determine the size of bitmap we're going to be making
    SIZE sz = { rcClip.right - rcClip.left, rcClip.bottom - rcClip.top };

    // create a bitmap on which to copy the image
    HBITMAP hbmCapture = ::CreateCompatibleBitmap(hdcSource, sz.cx, sz.cy);

    // temporarily select our bitmap into the memory DC, grab the image, then deselect it again
    if (AutoSelectGDIObject & select_capture = AutoSelectGDIObject(hdcMemory, hbmCapture))
        ::BitBlt(hdcMemory, 0, 0, sz.cx, sz.cy, hdcSource, rcClip.left, rcClip.top, SRCCOPY);

    // release our resources
    ::DeleteDC(hdcMemory);
    ::ReleaseDC(hwnd, hdcSource);

    // return the captured bitmap
    return hbmCapture;
}

答案 1 :(得分:0)

  1. 您捕获桌面的方式,BitBlt仅将感兴趣的窗口区域放入您的屏幕外位图(考虑捕获完整桌面并将图像裁剪到窗口位置)
  2. 或者,使用WM_PAINTWM_PRINTCLIENT消息请求窗口将自己绘制到您的DC