How to make a CWnd-derived class draw on CImage (or equivalent) instead of on screen?

I have an application which displays some windows. Specifically, it displays a CFormView-derived window. That works fine.
Now, I have an idea about making a jpeg which looks exactly like the content of that window (preferably excluting frame border).
The window contains
1) controls which are added using the resource view in VS, and the controls are derived of CEdit and CStatic.
2) controls which are added at runtime via Create methods. Those controls are derived of CButton
3) lines which are drawn using LineTo

I want the jpeg saved to disk when I do an export of all data in my application, so I don't want the view to be visible on screen while it saves the image.

How can I do this?

I had some idea to use CImage, and draw on its CDC, and then use CImage::Save to save it as a jpeg. But in that case... how can I make my CFormView-derived class draw its contents on the correct CDC? And will it work, since the window doesn't hav a CWnd-parent?
Just making a screen capture is out of the question, since the window should not be visible.
Using the CImage is just my first guess. Any other solution is just as welcome...

I think I need some code to help me along the way...

/Nicklas Kittelmann
Who is Participating?
AlexFMConnect With a Mentor Commented:
Capturing of window client area image is based on WM_PRINTCLIENT message. Create memory device context and select bitmap in it. Send WM_PRINTCLIENT message to the window passing DC handle in wParam.  Window draws itself to this device context. Now you can save this bitmap in any format using CImage class.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.