Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC
Article

Screen Capture to the Clipboard

Rate me:
Please Sign up or sign in to vote.
5.00/5 (27 votes)
5 Apr 2001 412.2K   98   89
How do you capture the bitmap of a window? This little download shows how, and places the bitmap on the clipboard.

Introduction

One of the common questions that I find on the microsoft.public forums is "How do I get a bitmap of a window on the screen?" Well, here's a little subroutine I used extensively in writing Win32 Programming, because I realized that for the number of illustrations I wanted for the book, there was no hope of doing screen captures and then hand-editing just the control I wanted to show. So all my Explorers have a little button somewhere that performs a screen capture of the example window and drops it into the clipboard. I could then paste it into the document, or paste it into an editor and save it as a TIFF or JPEG as well.

ToClip.h:

void toClipboard(CWnd * wnd, BOOL FullWnd);

ToClip.cpp

#include "stdafx.h"
#include "toclip.h" 
/****************************************************************
*                                 toClipboard
* Inputs:
*       CWnd * wnd: Window whose contents are to be sent 
*                   to the clipboard
*       BOOL FullWnd: TRUE for entire window, 
*                     FALSE for client area
* Result: void
*       
* Effect: 
*       Copies the contents of the client area or the window
*       to the clipboard in CF_BITMAP format.
*****************************************************************/

void toClipboard(CWnd * wnd, BOOL FullWnd)
    {
     CDC dc;
     if(FullWnd)
        { /* full window */
         HDC hdc = ::GetWindowDC(wnd->m_hWnd);
         dc.Attach(hdc);
        } /* full window */
     else
        { /* client area only */
         HDC hdc = ::GetDC(wnd->m_hWnd);
         dc.Attach(hdc);
        } /* client area only */

     CDC memDC;
     memDC.CreateCompatibleDC(&dc);

     CBitmap bm;
     CRect r;
     if(FullWnd)
        wnd->GetWindowRect(&r);
     else
         wnd->GetClientRect(&r);

     CString s;
     wnd->GetWindowText(s);
     CSize sz(r.Width(), r.Height());
     bm.CreateCompatibleBitmap(&dc, sz.cx, sz.cy);
     CBitmap * oldbm = memDC.SelectObject(&bm);
     memDC.BitBlt(0, 0, sz.cx, sz.cy, &dc, 0, 0, SRCCOPY);

     wnd->GetParent()->OpenClipboard();
     ::EmptyClipboard();
     ::SetClipboardData(CF_BITMAP, bm.m_hObject);
     CloseClipboard();

     memDC.SelectObject(oldbm);
     bm.Detach();  // make sure bitmap not deleted with CBitmap object
    }

The views expressed in these essays are those of the author, and in no way represent, nor are they endorsed by, Microsoft.

Send mail to newcomer@flounder.com with questions or comments about this article.
Copyright © 1999 All Rights Reserved
www.flounder.com/mvp_tips.htm

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Retired
United States United States
PhD, Computer Science, Carnegie Mellon University, 1975
Certificate in Forensic Science and the Law, Duquesne University, 2008

Co-Author, [i]Win32 Programming[/i]

Comments and Discussions

 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
Joseph M. Newcomer23-Apr-01 4:36
Joseph M. Newcomer23-Apr-01 4:36 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
26-Nov-01 21:52
suss26-Nov-01 21:52 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
Joseph M. Newcomer3-Dec-01 10:02
Joseph M. Newcomer3-Dec-01 10:02 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
Joseph M. Newcomer1-Mar-02 20:13
Joseph M. Newcomer1-Mar-02 20:13 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
sanpee15-Aug-02 21:04
sanpee15-Aug-02 21:04 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
Si Willi9-Oct-02 7:04
Si Willi9-Oct-02 7:04 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
Joseph M. Newcomer10-Oct-02 15:32
Joseph M. Newcomer10-Oct-02 15:32 
QuestionHow one can render metafile image from dialog control? Pin
8-Apr-01 23:32
suss8-Apr-01 23:32 
AnswerRe: How one can render metafile image from dialog control? Pin
Joseph M. Newcomer14-Apr-01 14:38
Joseph M. Newcomer14-Apr-01 14:38 
QuestionHuh ? Pin
5-Apr-01 23:05
suss5-Apr-01 23:05 
AnswerRe: Huh ? Pin
Peter Pearson6-Apr-01 0:11
Peter Pearson6-Apr-01 0:11 
GeneralRe: Huh ? Pin
20-Apr-01 9:59
suss20-Apr-01 9:59 
AnswerRe: Huh ? Pin
26-Feb-02 19:39
suss26-Feb-02 19:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.