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

Posted on 2004-11-25
Last Modified: 2013-11-20
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
Question by:kittelmann
    1 Comment
    LVL 48

    Accepted Solution

    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.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now