MFC: Drawing to a device context, splitting STL containers into pages

Posted on 2008-11-06
Last Modified: 2013-12-14

I'm writing an MFC application that produces printed reports. I do this through MFC's device context mechanism - I provide an implementation of a CScrollView's OnDraw(). All of the data that I draw comes from various STL containers that contain CStrings (a description that is mapped to an int which is a database PK, for example). I have an int offset value which increases as I draw things, so that I progressively draw down the page. The drawing logic works fine.

The problem comes when I want to split a larger report into multiple pages. I have to write code to split the containers into pages, before I do my drawing. This is typically very spaghetti-ish. This is what I've done in the past, but the particularly complex code of my most recent report makes me reconsider this approach.

Is there a better way, perhaps with dummy device contexts? Does MFC itself provide a mechanism for this?

Question by:sternocera
    LVL 30

    Accepted Solution

    Hi sternocera,

    IMO you can use CDC::StartPage/EndPage to controls this, i.e.:

    CMyView::OnDraw( CDC* pDC )
     // loop to print lines
     int nOffset = 0;
     while ( ... )
       // print line and increase offset

       if ( pDC->IsPrinting() )
        // if new page is required
        if ( nOffset > [maximum offset per page] )
          nOffset = 0;

    Hope that helps,


    Author Comment


    That's very interesting. I'll look into it, and get back to you,


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
    Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now