Click here to Skip to main content
12,623,511 members (31,462 online)
Click here to Skip to main content
Add your own
alternative version

Stats

334.8K 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

 
QuestionHow can i detect a global screen capture? Pin
Miguel Lopes27-Oct-03 14:38
memberMiguel Lopes27-Oct-03 14:38 
AnswerRe: How can i detect a global screen capture? Pin
Joseph M. Newcomer2-Nov-03 14:29
memberJoseph M. Newcomer2-Nov-03 14:29 
QuestionHow can I print it? Pin
Alex Evans30-Sep-03 15:32
memberAlex Evans30-Sep-03 15:32 
GeneralCapturing the whole window Pin
Arne Diegenbach25-Sep-03 12:35
sussArne Diegenbach25-Sep-03 12:35 
GeneralI rate 5 for this article! Pin
xxxyyyzzz12-Feb-03 11:17
memberxxxyyyzzz12-Feb-03 11:17 
Generalsomething that you don't need Pin
Anonymous10-Oct-02 4:44
sussAnonymous10-Oct-02 4:44 
GeneralRe: something that you don't need Pin
Joseph M. Newcomer10-Oct-02 16:23
memberJoseph M. Newcomer10-Oct-02 16:23 
Generalthere's a bug Pin
pangtao17-Jul-02 18:45
susspangtao17-Jul-02 18:45 
when I use
wnd = CWnd::GetDesktopWindow();
there's an error caused by
wnd->GetParent()->OpenClipboard();
I have to modify this clause to be following:
wnd->OpenClipboard();
Hmmm | :|
GeneralPossible Resource Leakage Pin
Lim Bio Liong7-Jan-02 23:27
memberLim Bio Liong7-Jan-02 23:27 
GeneralRe: Possible Resource Leakage Pin
Doug Hagerman1-Mar-02 16:49
memberDoug Hagerman1-Mar-02 16:49 
GeneralRe: Possible Resource Leakage Pin
Lim Bio Liong1-Mar-02 18:19
memberLim Bio Liong1-Mar-02 18:19 
GeneralRe: Possible Resource Leakage Pin
Joseph M. Newcomer1-Mar-02 21:10
memberJoseph M. Newcomer1-Mar-02 21:10 
GeneralRe: Possible Resource Leakage Pin
Joseph M. Newcomer1-Mar-02 21:03
memberJoseph M. Newcomer1-Mar-02 21:03 
GeneralRe: Possible Resource Leakage Pin
Lim Bio Liong3-Mar-02 0:39
memberLim Bio Liong3-Mar-02 0:39 
QuestionAn easier way? Pin
LrdElder6-Jan-02 10:25
memberLrdElder6-Jan-02 10:25 
QuestionHow To Capture Mouse Pointer Image Also Pin
Sujeet1-Nov-01 22:41
memberSujeet1-Nov-01 22:41 
AnswerRe: How To Capture Mouse Pointer Image Also Pin
Joseph M. Newcomer4-Nov-01 7:12
memberJoseph M. Newcomer4-Nov-01 7:12 
AnswerRe: How To Capture Mouse Pointer Image Also Pin
Joseph M. Newcomer4-Nov-01 7:16
memberJoseph M. Newcomer4-Nov-01 7:16 
GeneralRe: How To Capture Mouse Pointer Image Also Pin
Kunjan Shah26-Nov-01 22:49
memberKunjan Shah26-Nov-01 22:49 
QuestionHow to capture mediaplayer window ? Pin
Anonymous4-Sep-01 6:02
memberAnonymous4-Sep-01 6:02 
QuestionHow to receive list of Working applications with their windows' handles? Pin
Sash23-Jun-01 0:17
memberSash23-Jun-01 0:17 
AnswerRe: How to receive list of Working applications with their windows' handles? Pin
Joseph M. Newcomer23-Jun-01 7:55
memberJoseph M. Newcomer23-Jun-01 7:55 
AnswerRe: How to receive list of Working applications with their windows' handles? Pin
Amit_0910-Oct-03 1:42
memberAmit_0910-Oct-03 1:42 
QuestionResource leaking?? Pin
Anonymous7-Jun-01 16:28
memberAnonymous7-Jun-01 16:28 
Questionhow to capture a menu on the menubar? Pin
benben11-May-01 23:57
memberbenben11-May-01 23:57 

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.161128.1 | Last Updated 6 Apr 2001
Article Copyright 2001 by Joseph M. Newcomer
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid