使用winapi捕获整个软件窗口的位图

时间:2013-06-23 17:25:46

标签: windows winapi

我正在尝试捕获几个软件窗口并将其打印到我的软件中。目标是即使它们重叠,也能够并排查看两个不同的软件。

void MyWindowFinder::drawTo(HWND hWnd){
    MyWindow* currentWindow = anchorWindow;

    HDC hdcRendering;
    HDC hdcMemRendering;
    PAINTSTRUCT ps;

    hdcRendering = BeginPaint(hWnd, &ps);
    hdcMemRendering = CreateCompatibleDC(hdcRendering);

    int widthToRender = 0;
    for(int i = 0; i < this->anchorWindow->winID ; i++){
        HDC hdcCapture;
        HDC hdcMemCapture;
        HBITMAP hBitmap;
        HGDIOBJ oldCaptureBitmap;
        HGDIOBJ oldRenderingBitmap;

        RECT clientRect;
        GetClientRect(currentWindow->hwnd,&clientRect);
        int width = clientRect.right-clientRect.left;
        int height = clientRect.bottom-clientRect.top ;

        if (!(hdcCapture = GetDC (currentWindow->hwnd))){
            return;
        }

        hdcMemCapture = CreateCompatibleDC(hdcCapture);
        hBitmap = CreateCompatibleBitmap(hdcCapture,clientRect.right-clientRect.left,clientRect.bottom-clientRect.top);

        oldCaptureBitmap = SelectObject(hdcMemCapture, hBitmap);
        BitBlt(hdcMemCapture, 0, 0, width, height, hdcCapture, 0,0, SRCCOPY|CAPTUREBLT);

        oldRenderingBitmap = SelectObject(hdcMemRendering, hBitmap);
        BitBlt(hdcRendering, widthToRender, 0, width, height, hdcMemCapture, 0, 0, SRCCOPY);
        widthToRender+= width;
        SelectObject(hdcMemRendering, oldRenderingBitmap);
        SelectObject(hdcMemCapture, oldCaptureBitmap);


        DeleteDC(hdcMemCapture);
        DeleteDC(hdcCapture);

        DeleteObject(hBitmap);
        currentWindow = currentWindow->previousWindow;
    }
    DeleteDC(hdcMemRendering);
    DeleteDC(hdcRendering);
    EndPaint(hWnd, &ps);
}

这很好用,我可以在我的软件中左右打印两个重叠的窗口:

Side to side overlapped window 但是我面临一个棘手的问题。当我点击我的一个儿童软件的任何元素时,它们不会被我的软件绘制。

你知道为什么,以及如何解决这个问题?

0 个答案:

没有答案