Click here to Skip to main content
15,868,016 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.3K   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

 
GeneralPossible Resource Leakage Pin
Lim Bio Liong7-Jan-02 22:27
Lim Bio Liong7-Jan-02 22:27 
GeneralRe: Possible Resource Leakage Pin
1-Mar-02 15:49
suss1-Mar-02 15:49 
GeneralRe: Possible Resource Leakage Pin
Lim Bio Liong1-Mar-02 17:19
Lim Bio Liong1-Mar-02 17:19 
GeneralRe: Possible Resource Leakage Pin
Joseph M. Newcomer1-Mar-02 20:10
Joseph M. Newcomer1-Mar-02 20:10 
GeneralRe: Possible Resource Leakage Pin
Joseph M. Newcomer1-Mar-02 20:03
Joseph M. Newcomer1-Mar-02 20:03 
GeneralRe: Possible Resource Leakage Pin
Lim Bio Liong2-Mar-02 23:39
Lim Bio Liong2-Mar-02 23:39 
QuestionAn easier way? Pin
LrdElder6-Jan-02 9:25
LrdElder6-Jan-02 9:25 
QuestionHow To Capture Mouse Pointer Image Also Pin
1-Nov-01 21:41
suss1-Nov-01 21:41 
AnswerRe: How To Capture Mouse Pointer Image Also Pin
Joseph M. Newcomer4-Nov-01 6:12
Joseph M. Newcomer4-Nov-01 6:12 
AnswerRe: How To Capture Mouse Pointer Image Also Pin
Joseph M. Newcomer4-Nov-01 6:16
Joseph M. Newcomer4-Nov-01 6:16 
GeneralRe: How To Capture Mouse Pointer Image Also Pin
26-Nov-01 21:49
suss26-Nov-01 21:49 
QuestionHow to capture mediaplayer window ? Pin
4-Sep-01 5:02
suss4-Sep-01 5:02 
QuestionHow to receive list of Working applications with their windows' handles? Pin
Sash22-Jun-01 23:17
Sash22-Jun-01 23:17 
AnswerRe: How to receive list of Working applications with their windows' handles? Pin
Joseph M. Newcomer23-Jun-01 6:55
Joseph M. Newcomer23-Jun-01 6:55 
AnswerRe: How to receive list of Working applications with their windows' handles? Pin
Amit_0910-Oct-03 0:42
Amit_0910-Oct-03 0:42 
QuestionResource leaking?? Pin
7-Jun-01 15:28
suss7-Jun-01 15:28 
Questionhow to capture a menu on the menubar? Pin
benben11-May-01 22:57
benben11-May-01 22:57 
Generalmultimonitor Pin
9-May-01 10:56
suss9-May-01 10:56 
QuestionCapturing a DOS screen? Pin
Obliterator23-Apr-01 3:09
Obliterator23-Apr-01 3:09 
AnswerRe: Capturing a DOS screen? Pin
Joseph M. Newcomer23-Apr-01 4:46
Joseph M. Newcomer23-Apr-01 4:46 
GeneralRe: Capturing a DOS screen? Pin
Obliterator25-Apr-01 7:05
Obliterator25-Apr-01 7:05 
GeneralRe: Capturing a DOS screen? Pin
Joseph M. Newcomer25-Apr-01 8:51
Joseph M. Newcomer25-Apr-01 8:51 
GeneralRe: Capturing a DOS screen? Pin
Obliterator17-May-01 8:10
Obliterator17-May-01 8:10 
QuestionScreen Grabbing a window that is burried below others? Pin
jerry0davis23-Apr-01 1:58
jerry0davis23-Apr-01 1:58 
AnswerRe: Screen Grabbing a window that is burried below others? Pin
Chris Losinger23-Apr-01 4:26
professionalChris Losinger23-Apr-01 4:26 

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.