• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

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.
1 Solution
Jaime OlivaresSoftware ArchitectCommented:
have a look to this tutorial:
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();
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.

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.


> 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.

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.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now