Click here to Skip to main content
Licence 
First Posted 5 Apr 2001
Views 236,296
Bookmarked 80 times

Screen Capture to the Clipboard

By Joseph M. Newcomer | 5 Apr 2001
How do you capture the bitmap of a window? This little download shows how, and places the bitmap on the clipboard.

1

2
2 votes, 9.5%
3

4
19 votes, 90.5%
5
5.00/5 - 36 votes
2 removed
μ 4.48, σa 1.05 [?]

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

About the Author

Joseph M. Newcomer



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralTake a snapshot of a hidden window PinmemberMember 36896164:39 31 Mar '08  
GeneralRe: Take a snapshot of a hidden window PinmemberJoseph M. Newcomer5:02 31 Mar '08  
GeneralRe: Take a snapshot of a hidden window PinmemberMember 36896165:58 31 Mar '08  
GeneralRe: Take a snapshot of a hidden window PinmemberJoseph M. Newcomer8:53 31 Mar '08  
GeneralRe: Take a snapshot of a hidden window PinmemberMember 36896169:20 31 Mar '08  
GeneralRe: Take a snapshot of a hidden window PinmemberJoseph M. Newcomer13:43 31 Mar '08  
GeneralCapture MineSweeper Window PinmemberSwimmerDave8:32 20 Jun '06  
GeneralRe: Capture MineSweeper Window Pinmembervikrant kpr1:25 25 Jan '08  
GeneralOther bitmap formats PinmemberNeWi8:14 13 Oct '05  
GeneralRe: Other bitmap formats PinmemberNeWi8:35 13 Oct '05  
GeneralRe: Other bitmap formats PinmemberJoseph M. Newcomer8:46 18 Oct '05  
Generalwindows on top interfere Pinmemberbbrehm0417:10 30 Aug '05  
GeneralRe: windows on top interfere PinmemberJoseph M. Newcomer17:07 5 Sep '05  
GeneralRe: windows on top interfere Pinmemberbbrehm0415:13 6 Sep '05  
GeneralCapture in Win32 - code Pinmemberpowerpop17:20 30 Jun '05  
QuestionHow to send captured screen shot through a socket? Pinmemberbijut8:39 6 May '05  
AnswerRe: How to send captured screen shot through a socket? PinmemberJames R. Twine9:18 6 May '05  
AnswerRe: How to send captured screen shot through a socket? PinmemberJoseph M. Newcomer12:31 6 May '05  
AnswerRe: How to send captured screen shot through a socket? Pinmemberjags_vc23:52 8 May '05  
Questionhow to get image in memory? Pinmemberjags_vc2:00 25 Feb '05  
AnswerRe: how to get image in memory? PinmemberJoseph M. Newcomer7:18 25 Feb '05  
GeneralRe: how to get image in memory? Pinmemberjags_vc19:13 27 Feb '05  
GeneralRe: how to get image in memory? PinmemberJoseph M. Newcomer23:02 27 Feb '05  
GeneralRe: how to get image in memory? Pinmemberjags_vc2:13 7 Mar '05  
GeneralRe: how to get image in memory? PinmemberJoseph M. Newcomer11:40 9 Mar '05  

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

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

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