5,695,118 members and growing! (13,411 online)
Email Password   helpLost your password?
Multimedia » General Graphics » General     Intermediate

Screen Capture to the Clipboard

By Joseph M. Newcomer

How do you capture the bitmap of a window? This little download shows how, and places the bitmap on the clipboard.
VC6, C++Windows, NT4, Win2K, Visual Studio, MFC, Dev

Posted: 5 Apr 2001
Updated: 5 Apr 2001
Views: 168,004
Bookmarked: 54 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
34 votes for this Article.
Popularity: 6.89 Rating: 4.50 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 5.3%
3
0 votes, 0.0%
4
18 votes, 94.7%
5

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



Location: United States United States

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 88 (Total in Forum: 88) (Refresh)FirstPrevNext
GeneralTake a snapshot of a hidden windowmemberMember 36896164:39 31 Mar '08  
GeneralRe: Take a snapshot of a hidden windowmemberJoseph M. Newcomer5:02 31 Mar '08  
GeneralRe: Take a snapshot of a hidden windowmemberMember 36896165:58 31 Mar '08  
GeneralRe: Take a snapshot of a hidden windowmemberJoseph M. Newcomer8:53 31 Mar '08  
GeneralRe: Take a snapshot of a hidden windowmemberMember 36896169:20 31 Mar '08  
GeneralRe: Take a snapshot of a hidden windowmemberJoseph M. Newcomer13:43 31 Mar '08  
GeneralCapture MineSweeper WindowmemberSwimmerDave8:32 20 Jun '06  
GeneralRe: Capture MineSweeper Windowmembervikrant kpr1:25 25 Jan '08  
GeneralOther bitmap formatsmemberNeWi8:14 13 Oct '05  
GeneralRe: Other bitmap formatsmemberNeWi8:35 13 Oct '05  
GeneralRe: Other bitmap formatsmemberJoseph M. Newcomer8:46 18 Oct '05  
Generalwindows on top interferememberbbrehm0417:10 30 Aug '05  
GeneralRe: windows on top interferememberJoseph M. Newcomer17:07 5 Sep '05  
GeneralRe: windows on top interferememberbbrehm0415:13 6 Sep '05  
GeneralCapture in Win32 - codememberpowerpop17:20 30 Jun '05  
GeneralHow to send captured screen shot through a socket?memberbijut8:39 6 May '05  
GeneralRe: How to send captured screen shot through a socket?memberJames R. Twine9:18 6 May '05  
GeneralRe: How to send captured screen shot through a socket?memberJoseph M. Newcomer12:31 6 May '05  
GeneralRe: How to send captured screen shot through a socket?memberjags_vc23:52 8 May '05  
Generalhow to get image in memory?memberjags_vc2:00 25 Feb '05  
GeneralRe: how to get image in memory?memberJoseph M. Newcomer7:18 25 Feb '05  
GeneralRe: how to get image in memory?memberjags_vc19:13 27 Feb '05  
GeneralRe: how to get image in memory?memberJoseph M. Newcomer23:02 27 Feb '05  
GeneralRe: how to get image in memory?memberjags_vc2:13 7 Mar '05  
GeneralRe: how to get image in memory?memberJoseph M. Newcomer11:40 9 Mar '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Apr 2001
Editor: Chris Maunder
Copyright 2001 by Joseph M. Newcomer
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project