Click here to Skip to main content
15,867,568 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

 
Questionhow to get image in memory? Pin
Jagdish Vasani25-Feb-05 1:00
Jagdish Vasani25-Feb-05 1:00 
AnswerRe: how to get image in memory? Pin
Joseph M. Newcomer25-Feb-05 6:18
Joseph M. Newcomer25-Feb-05 6:18 
GeneralRe: how to get image in memory? Pin
Jagdish Vasani27-Feb-05 18:13
Jagdish Vasani27-Feb-05 18:13 
GeneralRe: how to get image in memory? Pin
Joseph M. Newcomer27-Feb-05 22:02
Joseph M. Newcomer27-Feb-05 22:02 
GeneralRe: how to get image in memory? Pin
Jagdish Vasani7-Mar-05 1:13
Jagdish Vasani7-Mar-05 1:13 
GeneralRe: how to get image in memory? Pin
Joseph M. Newcomer9-Mar-05 10:40
Joseph M. Newcomer9-Mar-05 10:40 
GeneralAbout mouse pointer Pin
4-Apr-04 17:09
suss4-Apr-04 17:09 
GeneralRe: About mouse pointer Pin
Joseph M. Newcomer4-Apr-04 19:35
Joseph M. Newcomer4-Apr-04 19:35 
It is tricky. I basically do a GetCursor, then get the cursor bitmap, and then get the mouse position, compute it relative to the window I've just snapped, then do a DrawIcon. Something like that, at least. I've misplaced the code, and with 750K files on my system, I can't easily find it.
GeneralRe: About mouse pointer Pin
Rodrigo Vaz6-Apr-04 19:25
Rodrigo Vaz6-Apr-04 19:25 
GeneralRe: About mouse pointer Pin
Joseph M. Newcomer6-Apr-04 21:12
Joseph M. Newcomer6-Apr-04 21:12 
GeneralRe: About mouse pointer Pin
7-Apr-04 18:29
suss7-Apr-04 18:29 
GeneralRe: About mouse pointer Pin
Andyzyx4-Sep-04 17:24
Andyzyx4-Sep-04 17:24 
GeneralRe: About mouse pointer Pin
Joseph M. Newcomer5-Sep-04 10:57
Joseph M. Newcomer5-Sep-04 10:57 
QuestionHow capture a DirectX or OpenGL window? Pin
peterbing22-Feb-04 21:17
peterbing22-Feb-04 21:17 
AnswerRe: How capture a DirectX or OpenGL window? Pin
Joseph M. Newcomer22-Feb-04 22:13
Joseph M. Newcomer22-Feb-04 22:13 
GeneralRe: How capture a DirectX or OpenGL window? Pin
NeWi13-Oct-05 7:29
NeWi13-Oct-05 7:29 
Questionhow to save clipboard to file?? Pin
Anonymous1-Jan-04 20:41
Anonymous1-Jan-04 20:41 
AnswerRe: how to save clipboard to file?? Pin
Joseph M. Newcomer1-Jan-04 20:55
Joseph M. Newcomer1-Jan-04 20:55 
AnswerRe: how to save clipboard to file?? Pin
Anonymous14-Jan-04 8:08
Anonymous14-Jan-04 8:08 
GeneralRe: how to save clipboard to file?? Pin
Joseph M. Newcomer14-Jan-04 8:58
Joseph M. Newcomer14-Jan-04 8:58 
AnswerRe: how to save clipboard to file?? Pin
NeWi13-Oct-05 7:18
NeWi13-Oct-05 7:18 
QuestionHow to convert to non-mfc? Pin
Tommy2k9-Dec-03 11:22
Tommy2k9-Dec-03 11:22 
AnswerRe: How to convert to non-mfc? Pin
NeWi13-Oct-05 7:25
NeWi13-Oct-05 7:25 
QuestionHow can i detect a global screen capture? Pin
Miguel Lopes27-Oct-03 13:38
Miguel Lopes27-Oct-03 13:38 
AnswerRe: How can i detect a global screen capture? Pin
Joseph M. Newcomer2-Nov-03 13:29
Joseph M. Newcomer2-Nov-03 13:29 

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.