Print Preview without Document/View architecture
> I am working with the Microsoft Visual C++ 4.2 compiler. How can I
> develope a Print Preview, without using the Document/View architecture,
> but using the predefined preview-dialog in the mfc. Is it possible to use
> a device context as a function parameter? Is it useful to create the base
> class for a printing class library with a Cview class?
Here's how I did it. It's tricky and a bit of a kludge but I don't know
of any other approach that works without building your own previewer.
First I created my own subclass of CDocument, CFrameWnd, and CView which
will be used to do the Print Previewing (CPrintDoc, CPrintFrame, and
CPrintView in the example below). You also should have a dummy menu
resource so that the print preview mechanism thinks its doing its
thing. The menu doesn't need to have anything in it, but it should be a
valid menu resource. Then where you want to begin the Print Preview,
enter code that looks something like this:
pDocTemplate = new CSingleDocTemplate(
IDR_PRINTFRAME, // point to your dummy menu resource
Then on the CPrintView object that is created, call it's print preview
method like this:
printView->PrintPreview(); // This activates the print preview window
The tricky part is getting a pointer to the print view that is created.
I do it by examining the document in pDocTemplate.
The call to OpenDocumentFile brings up the Print Preview window just
like the document view architecture does. You might have to play with
the show command to get the window to display the first time. You then
put your print preview drawing inside OnDraw of your CPrintView subclass
of CView. I leave it to you to do this part.
For clean up, enter clean up code in OnEndPrintPreview of your
CPrintView subclass. The clean up must include closing and destroying
the frame window, CPrintFrame, plus deleting the document template you
created above. This clean up is also a bit tricky because you're inside
the view that itself is being destroyed when you destroy CPrintFrame.
You can handle this by notifying yourself to do it later. I also had to
destroy a document manager object that was created when AddDocTemplate
was called. If your application already has a document manager object
(check AfxGetApp()->m_pDocManager) don't destroy it.
This should get you started. It's not pretty, but it works for me.