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.
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
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:
CPreviewDC *pPrevDC = static_cast<CPreviewDC*>(pDC);
rectClip += ptOrg;
So this sorted the problem for us. I hope this reference here helps you out.