I have what I hope to be a simple question, but one that Google is
failing me on. Everything seems to describe the opposite of the
process, but I know that the information is out there somewhere.
I have an mfc application that with multiple CViews that I have draw
code set up for and everything works wonderfully when drawing to the
screen. Now, what I want to do, is create a bitmap, tell each of the
CViews to draw to that, and then saving it out to disk as a screen
capture. I want to go this route for several reasons, the biggest
being that I want to save these CViews even if they are ocluded by
other windows, though if this proves to be too difficult, a screenshot
would suffice I suppose.
My CView has draw code that looks like the following:
void CGraphicsView::OnDraw(CDC* pDCWnd)
{
CGraphicsDoc* pModelDoc = GetModelDocument();
ASSERT_VALID(pModelDoc);
CDC *pDC = pDCWnd;
OnPrepareDC(pDC);
theApp.m_mtxDrawInProgress.Lock();
CRect rectClient;
GetClientRect(&rectClient);
CRect rectClip;
pDCWnd->GetClipBox(rectClip);
if (EqualRect(&rectClient, &rectClip))
{
if (m_bWasCovered)
{
m_bWasCovered = false;
this->Invalidate(true);
}
}
else
{
m_bWasCovered = true;
}
if (!pDC->IsPrinting())
pDC->FillSolidRect(rectClip, RGB(204, 204, 204));
if (pModelDoc->Model()->IsValid())
pModelDoc->Model()->Draw(pDC, rectClip);
Now, it seems to me (though I am FAR from fluent in device contexts or
even CViews in general (working on "inherited" code) ) that I should
be able to create a CMemDC and tell it to draw on that. I know enough
to know that the draw code shouldn't care the slightest WHAT it is
drawing on, but I haven't been able to find information on how to set
up drawing to a bitmap in memory.
Seems relatively simple, do you think any of you can help me out?
(apologies again on that last post, and I know the code cuts off, I
wasn't done. But I think that shows the important stuff)
Thanks
~Scoots
I'd like to know the REAL way to draw to a bitmap, since I don't to go
through this custom class. Sorry for the several posts, I just felt I
better address that.
~Scoots
#include <atlimage.h>
DEFINE_GUID(ImageFormatBMP, 0xb96b3cab,0x0728,0x11d3,0x9d,0x7b,
0x00,0x00,0xf8,0x1e,0xf3,0x2e);
void CMyView::OnSaveViewToBitmap()
{
CClientDC dc(this);
CRect rc;
GetClientRect(&rc);
CDC memdc;
memdc.CreateCompatibleDC(&dc);
CBitmap bmp;
bmp.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
CBitmap* pOldBmp = memdc.SelectObject(&bmp);
::PrintWindow(m_hWnd,memdc,1); // will output obscured windows too
memdc.SelectObject(pOldBmp);
//copy image to disk
CImage img;
img.Attach((HBITMAP)bmp.Detach());
CString str(_T("c:\\temp\\xxx.bmp"));
img.Save(str,ImageFormatBMP);
}
This might be a little more than what you asked for!
Typical the way I do my view drawing is double buffered, so what I normally
do is I keep a bitmap in memory that I draw to, and then when OnDraw gets
called I simply blit the bitmap in memory to the screen.
The reason I do this is because it is much faster to simply blit a bitmap
than to loop through everything and draw every item each time a WM_PAINT is
received, because that can happen more often than my actual graphics is
going to change.
So everytime something happens that requires a change to the graphics, I
simply draw to the memory bitmap and Invalidate/UpdateWindow the view so
that the display will show the changes.
With all that said, my Drawing routine usually takes a CDC and a CRect and
draws on that DC. The cool thing here is that I can pass in any DC, screen
or memory for the draw routine to draw to.
So for me, if I want to save a bitmap of what's on the screen, all I have to
do is create a memory dc and put a bitmap in it, call my draw routine with
that DC and then simply save the bitmap to a file.
The short fall of this method is that it can be memory intensive depending
on the size of your bitmaps.
AliR.
"Scoots" <linki...@msn.com> wrote in message
news:01f07cd9-e1c7-4c5a...@g27g2000yqn.googlegroups.com...
In fact, it actually works better than I thought. I was originally
going to take a screenshot of each window and reconstruct. With this,
I actually don't, though that's part of the way the application is
built. There are no menus or titlebar, rather there is a eventbar
CView that uses (for the most part) a button interface. I don't have
to reconstruct to get this bar in, AND it excludes dialogs that I have
opened up on top (which was actually a feature that I wanted)
It's been some time since I worked with device contexts. I think I
need to brush up a bit. Any recommended readings?
~Scoots