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"
void toClipboard(CWnd * wnd, BOOL FullWnd)
{
CDC dc;
if(FullWnd)
{
HDC hdc = ::GetWindowDC(wnd->m_hWnd);
dc.Attach(hdc);
}
else
{
HDC hdc = ::GetDC(wnd->m_hWnd);
dc.Attach(hdc);
}
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();
}
The views expressed in these essays are those of the author, and in no way represent, nor are they endorsed by, Microsoft.