Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Capture the window bitmap from another running program.

Posted on 2004-10-26
Medium Priority
Last Modified: 2013-12-03
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.
Question by:MugWumpBW
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12427486
have a look to this tutorial:

Expert Comment

by:Daniel Cardozo
ID: 12504197
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();

Author Comment

ID: 12505609
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.

Accepted Solution

foodlebardle earned 1500 total points
ID: 12556341

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.


Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question