Click here to Skip to main content
12,349,148 members (68,843 online)
Click here to Skip to main content
Add your own
alternative version

Stats

326K views
91 bookmarked
Posted

Screen Capture to the Clipboard

, 5 Apr 2001
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Joseph M. Newcomer
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]

You may also be interested in...

Comments and Discussions

 
GeneralPossible Resource Leakage Pin
Lim Bio Liong7-Jan-02 22:27
memberLim Bio Liong7-Jan-02 22:27 
GeneralRe: Possible Resource Leakage Pin
Doug Hagerman1-Mar-02 15:49
memberDoug Hagerman1-Mar-02 15:49 
GeneralRe: Possible Resource Leakage Pin
Lim Bio Liong1-Mar-02 17:19
memberLim Bio Liong1-Mar-02 17:19 
GeneralRe: Possible Resource Leakage Pin
Joseph M. Newcomer1-Mar-02 20:10
memberJoseph M. Newcomer1-Mar-02 20:10 
GeneralRe: Possible Resource Leakage Pin
Joseph M. Newcomer1-Mar-02 20:03
memberJoseph M. Newcomer1-Mar-02 20:03 
GeneralRe: Possible Resource Leakage Pin
Lim Bio Liong2-Mar-02 23:39
memberLim Bio Liong2-Mar-02 23:39 
QuestionAn easier way? Pin
LrdElder6-Jan-02 9:25
memberLrdElder6-Jan-02 9:25 
QuestionHow To Capture Mouse Pointer Image Also Pin
Sujeet1-Nov-01 21:41
memberSujeet1-Nov-01 21:41 
AnswerRe: How To Capture Mouse Pointer Image Also Pin
Joseph M. Newcomer4-Nov-01 6:12
memberJoseph M. Newcomer4-Nov-01 6:12 
AnswerRe: How To Capture Mouse Pointer Image Also Pin
Joseph M. Newcomer4-Nov-01 6:16
memberJoseph M. Newcomer4-Nov-01 6:16 
GeneralRe: How To Capture Mouse Pointer Image Also Pin
Kunjan Shah26-Nov-01 21:49
memberKunjan Shah26-Nov-01 21:49 
QuestionHow to capture mediaplayer window ? Pin
Anonymous4-Sep-01 5:02
memberAnonymous4-Sep-01 5:02 
QuestionHow to receive list of Working applications with their windows' handles? Pin
Sash22-Jun-01 23:17
memberSash22-Jun-01 23:17 
AnswerRe: How to receive list of Working applications with their windows' handles? Pin
Joseph M. Newcomer23-Jun-01 6:55
memberJoseph 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
memberAmit_0910-Oct-03 0:42 
QuestionResource leaking?? Pin
Anonymous7-Jun-01 15:28
memberAnonymous7-Jun-01 15:28 
Questionhow to capture a menu on the menubar? Pin
benben11-May-01 22:57
memberbenben11-May-01 22:57 
Generalmultimonitor Pin
Anonymous9-May-01 10:56
memberAnonymous9-May-01 10:56 
QuestionCapturing a DOS screen? Pin
Obliterator23-Apr-01 3:09
memberObliterator23-Apr-01 3:09 
AnswerRe: Capturing a DOS screen? Pin
Joseph M. Newcomer23-Apr-01 4:46
memberJoseph M. Newcomer23-Apr-01 4:46 
GeneralRe: Capturing a DOS screen? Pin
Obliterator25-Apr-01 7:05
memberObliterator25-Apr-01 7:05 
GeneralRe: Capturing a DOS screen? Pin
Joseph M. Newcomer25-Apr-01 8:51
memberJoseph M. Newcomer25-Apr-01 8:51 
GeneralRe: Capturing a DOS screen? Pin
Obliterator17-May-01 8:10
memberObliterator17-May-01 8:10 
QuestionScreen Grabbing a window that is burried below others? Pin
Jeremy Davis23-Apr-01 1:58
memberJeremy Davis23-Apr-01 1:58 
AnswerRe: Screen Grabbing a window that is burried below others? Pin
Chris Losinger23-Apr-01 4:26
memberChris Losinger23-Apr-01 4:26 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
Joseph M. Newcomer23-Apr-01 4:36
memberJoseph M. Newcomer23-Apr-01 4:36 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
Kunjan Shah26-Nov-01 21:52
memberKunjan Shah26-Nov-01 21:52 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
Joseph M. Newcomer3-Dec-01 10:02
memberJoseph M. Newcomer3-Dec-01 10:02 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
Joseph M. Newcomer1-Mar-02 20:13
memberJoseph M. Newcomer1-Mar-02 20:13 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
sanpee15-Aug-02 21:04
membersanpee15-Aug-02 21:04 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
Si Willi9-Oct-02 7:04
sussSi Willi9-Oct-02 7:04 
GeneralRe: Screen Grabbing a window that is burried below others? Pin
Joseph M. Newcomer10-Oct-02 15:32
memberJoseph M. Newcomer10-Oct-02 15:32 
QuestionHow one can render metafile image from dialog control? Pin
Michael Ortenberg8-Apr-01 23:32
memberMichael Ortenberg8-Apr-01 23:32 
AnswerRe: How one can render metafile image from dialog control? Pin
Joseph M. Newcomer14-Apr-01 14:38
memberJoseph M. Newcomer14-Apr-01 14:38 
QuestionHuh ? Pin
Anonymous5-Apr-01 23:05
memberAnonymous5-Apr-01 23:05 
AnswerRe: Huh ? Pin
Peter Pearson6-Apr-01 0:11
memberPeter Pearson6-Apr-01 0:11 
GeneralRe: Huh ? Pin
Gregory20-Apr-01 9:59
memberGregory20-Apr-01 9:59 
AnswerRe: Huh ? Pin
Anonymous26-Feb-02 19:39
memberAnonymous26-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 6 Apr 2001
Article Copyright 2001 by Joseph M. Newcomer
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid