From: Scoots on
Hello all,

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);
From: Scoots on
My apologies, the last went out incomplete as my browser interpretted
my formatting of the code (in which I dared to press spacebar) as
clicking the "send" button in Google Groups. Many apologies.

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
From: Scoots on
Apologies again. It appears the CMemDC (which I saw elsewhere in the
code) is a custom class that exists in the inherited code, and is
pulled from I believe CodeProject.

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
From: AliR on
I have a question, do you only want the client area of the views, or do you
want to save the border and titlebar and all that?

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" <linkingfire(a)msn.com> wrote in message
news:01f07cd9-e1c7-4c5a-a88d-0df93146b0a9(a)g27g2000yqn.googlegroups.com...
> Hello all,
>
> 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);


From: Scoots on
The code referenced by Seetharam (with a little tweaking) worked
wonderfully. I was unaware of the PrintWindow call.

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