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.
MugWumpBWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
have a look to this tutorial:
http://www.fengyuan.com/article/wmprint.html
0
Daniel CardozoSenior ITCommented:
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);
}
0
MugWumpBWAuthor Commented:
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.
0
foodlebardleCommented:

Not possible without hacking.


from http://www.fengyuan.com/article/wmprint.html:

> The implementation of this seemly simple class is quite tricky.
> Some knowledge of Win32 API implementation, compiler code generation,
> Intel machine code, and virtual memory is needed to understand it fully.

Also:

> Limitation
> The Hook function only handles exported function whose first instruction is "MOV EAX, <DWORD_constant>".
> So the implementation shown here only applies to Windows NT/2000.
> Refer to Chapter 4 for more generic of restrictive API hooking solutions.
> Only tested on Windows 2000 machine.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.