The Wayback Machine - https://web.archive.org/web/20150318023552/http://www.codeguru.com/cpp/article.php/c18347/C-Programming-Easy-Screen-Capture-Using-MFCATL.htm

C++ Programming: Easy Screen Capture Using MFC/ATL

Introduction

CImage is a class shared by MFC and ATL. It provides support for easy manipulation of bitmaps and different graphic file formats (BMP, JPEG, GIF, and PNG). This article presents an extension class having methods for screen images capture.

CScreenImage class

CScreenImage is derived from CImage, uses shared MFC/ATL classes and plain Windows API, so can be inserted in any MFC or ATL-based application.

class CScreenImage : public CImage
{
public:
    BOOL CaptureRect(const CRect& rect) throw();
    BOOL CaptureScreen() throw();
    BOOL CaptureWindow(HWND hWnd) throw();
};

  • CScreenImage::CaptureRect - captures an image from a given screen area;
  • CScreenImage::CaptureScreen - captures the entire screen image of the first monitor;
  • CScreenImage::CaptureWindow - captures the image of a given window.
CScreenImage::CaptureRect function implementation is shown below.
It uses the classic way of bit-blitting from the screen device context to a compatible memory device context. Finally it attaches the bitmap handle to this object.
To be noticed the CAPTUREBLT flag used in BitBlt function call. This allows dealing with layered windows images.
/********************************************************************
 Function:   CScreenImage::CaptureRect
 Purpose:    captures a screen rectangle
 Parameters: rect: screen rectangle to be captured
 Return:     non-zero value if successful
*********************************************************************/
BOOL CScreenImage::CaptureRect(const CRect& rect)
{
   // detach and destroy the old bitmap if any attached
   CImage::Destroy();
   
   // create a screen and a memory device context
   HDC hDCScreen = ::CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
   HDC hDCMem = ::CreateCompatibleDC(hDCScreen);
   // create a compatible bitmap and select it in the memory DC
   HBITMAP hBitmap = 
      ::CreateCompatibleBitmap(hDCScreen, rect.Width(), rect.Height());
   HBITMAP hBmpOld = (HBITMAP)::SelectObject(hDCMem, hBitmap);

   // bit-blit from screen to memory device context
   // note: CAPTUREBLT flag is required to capture layered windows
   DWORD dwRop = SRCCOPY | CAPTUREBLT;
   BOOL bRet = ::BitBlt(hDCMem, 0, 0, rect.Width(), rect.Height(), 
                        hDCScreen, rect.left, rect.top, dwRop);
   // attach bitmap handle to this object
   Attach(hBitmap);

   // restore the memory DC and perform cleanup
   ::SelectObject(hDCMem, hBmpOld);
   ::DeleteDC(hDCMem);
   ::DeleteDC(hDCScreen);

   return bRet;
}
The other methods implementation can be found in attached source code and demo application.

Demo application

The demo application is a simple SDI application which uses CScreenImage class to capture images from the screen and display them in a view.

From the Capture menu you can choose the following items:

  • Screen, to capture the entire screen image of the first monitor;
  • Foreground window, to capture the image of the window with which the user is currently working;
  • Rectangle, to capture the image of a selected screen area, using the mouse.
It can further be improved by adding other features like capturing any other top-level or child window, capturing client areas, saving image in a file and so on.

Related links



About the Author

Ovidiu Cucu

Graduated at "Gh. Asachi" Technical University - Iasi, Romania. Programming in C++ using Microsoft technologies since 1995. Microsoft MVP awardee since 2006. Moderator and article reviewer at Codeguru.com, the number one developer site. Co-founder of Codexpert.ro, a website dedicated to Romanian C++ developers.

Downloads

Comments

  • console

    Posted by mani on 06/27/2014 09:23am

    sry for my bad en i am noob in c++ form we need capture screen from game , i test your program and i think this work but i need it on console and if u can plz help me i cant convert your program to console for microsoft visual studio c++ best regard

    Reply
  • hackett outlet

    Posted by gogojdr on 05/14/2013 08:39pm

    I'll defintely bookmark this web page, thanks for the info! toms shoes outlet oakley vault store oakley holbrook

    Reply
  • thanks to share, but i have a question ...

    Posted by zhmx on 09/02/2012 04:44am

    firstly, i am sorry about my english. this period of time, i am trapping in a question. when i use the function "BitBlt" to capture the image of video player, all the picture black it is.i know it use the technology of DirectX and the image show in overlay. how can i capture the sure picture of video player in MFC? thank you a lot!

    Reply

Top White Papers and Webcasts

  • You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this webcast that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base application that will jumpstart you into building your own more complex app …

  • Finance leaders have been talking about expanding the value-added role of their teams for a long time. The debate is no longer whether the finance and accounting function needs to become a more strategic partner to the rest of the business but rather how to get there. Technology innovation has caught up to this ambition, and what was once aspiration can be a reality – and the choice is now yours. Read this research report to learn how to make the most of information tools to enable innovation and growth.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date