Link to home
Start Free TrialLog in
Avatar of MugWumpBW
MugWumpBW

asked on

Capture the window bitmap from another running program.

I have a Visual C++ 6.0 program which needs to capture the client area bitmap from the window of another running application.  I have the window handle of the target window am able to get the bitmap if the target window is completely visible, but if the window is partially overlayed, then the bitmap capture gets the overlaying windows as well.  I have read here and other sites that sending a WM_PRINT message to the target window and providing a device context will do this, but so far I have been unable to get this to work.  The code I am using is the following:

        CRect rect;
        CDC   memDC;

        CWnd* pWindow = CWnd::FromHandle( m_hWindow );

        if ( ( pWindow != NULL ) && ( IsWindow( pWindow->m_hWnd ) == TRUE ) )
        {
            CDC* pClientDC   = new CClientDC( pWindow );
            CBitmap* pBitmap = new CBitmap();
           
            pWindow->GetClientRect( &rect );
            pBitmap->CreateCompatibleBitmap( pClientDC, rect.Width(), rect.Height() );

            memDC.CreateCompatibleDC( pClientDC );
            CBitmap* pOldBitmap = memDC.SelectObject( pBitmap );

            // Adding this line and removing the next line did not work
            //SendMessage( m_hWindow, WM_PRINT, ( WPARAM )memDC.GetSafeHdc(), PRF_CLIENT );

            // This code captures the overlayed windows
            memDC.BitBlt( 0, 0, rect.Width(), rect.Height(), pClientDC, 0, 0, SRCCOPY );

            memDC.SelectObject( pOldBitmap );

            ReleaseDC( m_hWindow, pClientDC->GetSafeHdc() );
            delete pClientDC;
        }

What do I need to modify in this code so that the target window bitmap is captured even if the window is partially overlayed.
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

have a look to this tutorial:
http://www.fengyuan.com/article/wmprint.html
If you don't mind the target window to come in and out of focus for an instant, you can do:

if ( ( pWindow != NULL ) && ( IsWindow( pWindow->m_hWnd ) == TRUE ) )
{
    HWND currentForegroundWindow = GetForegroundWindow();
    SetForegroundWindow(pWindow->m_hWnd);
    ........
    ........
    SetForegroundWindow(currentForegroundWindow);
}
Avatar of MugWumpBW
MugWumpBW

ASKER

The only problem with that approach is that I capture the bitmaps every 500 ms so that would effectively cause the target window to remain on top at all times - which is what I am trying to avoid.
ASKER CERTIFIED SOLUTION
Avatar of foodlebardle
foodlebardle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial