Click here to Skip to main content
15,881,812 members
Articles / Desktop Programming / MFC
Article

Using CRgn with print preview

Rate me:
Please Sign up or sign in to vote.
4.95/5 (13 votes)
3 Nov 2003CPOL2 min read 68.8K   20   9
CRgn objects need translating, to work correctly in print preview.

Some background

I was updating one of my classes today which prints a 2D graph, and to try and speed it up, we moved from a self clipping algorithm using y = mx + c and the graph boundaries to using a CRgn object to do the work for us. This made our code much simpler and hopefully quicker (we have yet to benchmark it).

We then moved on to some regression testing and found everything worked OK except that we got no output from the graph in print preview, although the graph would print correctly on the printer.

The problem

After checking through the code to make sure we had not done something stupid (the changes happened at the same time as upgrading the code that put data into the graph), we pin pointed the problem to the clip region we were using. Everything looked fine for the coordinates we were passing through to the creation of the CRgn object.

The problem was caused by the way the CPreviewDC class works. During preview mode you have 2 HDC objects active, the m_hAttribDC which is the HDC of the printer device context and the m_hDC which is the output DC. Now we were using a clip region that was correct for the printer device context, but not the output device context. When actually printing the attribute and output DCs are the same so we get correct output, but in preview mode, they are different. To get the output to work correctly, we needed to translate the clip region in use from the printer context to the output context.

A quick search of the MSDN turned up a problem report, PRB: Clipping Doesn't Work Correctly in Print Preview, but only because I knew what I was looking for. So that's why I am posting this fix myself here.

We were able to take the code posted there and translate the clip region units like this:

// Now if we are in print preview mode then the clipping
// rectangle needs to be adjusted before creating the
// clipping region
CRgn rgn;
if (pDC->IsKindOf(RUNTIME_CLASS(CPreviewDC)))
{
    CRect rectClip(m_axes);
    CPreviewDC *pPrevDC = static_cast<CPreviewDC*>(pDC);

    pPrevDC->PrinterDPtoScreenDP(&rectClip.TopLeft());
    pPrevDC->PrinterDPtoScreenDP(&rectClip.BottomRight());
    // Now offset the result by the viewport origin of
    // the print preview window...

    CPoint ptOrg;
    ::GetViewportOrgEx(pDC->m_hDC,&ptOrg);
    rectClip += ptOrg;
    VERIFY(rgn.CreateRectRgnIndirect(rectClip));
}
else
{
    // just use the regular clip area as we are not in preview mode
    VERIFY(rgn.CreateRectRgnIndirect(m_axes));
}
pDC->SelectClipRgn(&rgn);
VERIFY(rgn.DeleteObject());

...
pDC->SelectClipRgn(NULL);

So this sorted the problem for us. I hope this reference here helps you out.

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Sirius Analytical Instruments
United Kingdom United Kingdom
A research and development programmer working for a pharmaceutical instrument company for the past 17 years.

I am one of those lucky people who enjoys his work and spends more time than he should either doing work or reseaching new stuff. I can also be found on playing DDO on the Cannith server (Send a tell to "Maetrim" who is my current main)

I am also a keep fit fanatic, doing cross country running and am seriously into [url]http://www.ryushinkan.co.uk/[/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

Comments and Discussions

 
GeneralMy vote of 5 Pin
knn29-Mar-11 21:31
knn29-Mar-11 21:31 
GeneralThanks Pin
duplace4-Sep-06 23:06
duplace4-Sep-06 23:06 
GeneralJust what I needed.... Pin
Liam20-Jul-06 5:56
Liam20-Jul-06 5:56 
General&quot;Prev Page&quot; and &quot;Zoom Out&quot; buttons are always disabled Pin
Anonymous28-Jul-04 10:30
Anonymous28-Jul-04 10:30 
GeneralHere's how to cope with mapping modes and different viewport origins Pin
jon_a9-Jul-04 10:59
jon_a9-Jul-04 10:59 
GeneralRe: Here's how to cope with mapping modes and different viewport origins Pin
Anonymous1-Oct-05 2:05
Anonymous1-Oct-05 2:05 
GeneralRe: Here's how to cope with mapping modes and different viewport origins Pin
vjedlicka13-May-12 9:14
vjedlicka13-May-12 9:14 
Thanks for sharing Thumbs Up | :thumbsup:
GeneralNeed to include &lt;afxpriv.h&gt; Pin
jon_a9-Jul-04 10:27
jon_a9-Jul-04 10:27 
GeneralI know the problem... Pin
CodeBrain7-Jan-04 2:49
CodeBrain7-Jan-04 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.