Page 1 of 1

wxPrintout(): Where does the wxPrinterDC come from?

Posted: Sun Jun 14, 2020 9:06 pm
by Rob190
The docomentation says:

wxDC* wxPrintout::GetDC ( ) const: "Returns the device context associated with the printout (given to the printout at start of printing or previewing)."

So how do I do that? The printer sample code doesn't have a single instance of wxPrinterDC.

Rob.

Re: wxPrintout(): Where does the wxPrinterDC come from?

Posted: Sun Jun 14, 2020 9:28 pm
by doublemax
The printer sample just uses a wxDC.

Code: Select all

bool MyPrintout::OnPrintPage(int page)
{
    wxDC *dc = GetDC();
    if (dc)
    {
        if (page == 1)
            DrawPageOne();
        else if (page == 2)
            DrawPageTwo();

        // Draw page numbers at top left corner of printable area, sized so that
        // screen size of text matches paper size.
        MapScreenSizeToPage();

        dc->DrawText(wxString::Format(wxT("PAGE %d"), page), 0, 0);

        return true;
    }
    else
        return false;
}

Re: wxPrintout(): Where does the wxPrinterDC come from?

Posted: Sun Jun 14, 2020 9:52 pm
by Rob190
That could actually be anything derived from wxDC. Somewhere in the docs or sample it does actually say it's a wxPrinterDC or wxMemoryDC depending on whether it's a print or a print preview. I'm still unsure where it comes from though. A DC must somehow be associated with the device but how does that happen?

Rob.

Re: wxPrintout(): Where does the wxPrinterDC come from?

Posted: Sun Jun 14, 2020 10:27 pm
by doublemax
I don't know the exact internals, but based on the following piece of code, the "printer specific" part comes from wxPrinter in combination with the wxPrintDialogData.

Code: Select all

void MyFrame::OnPrint(wxCommandEvent& WXUNUSED(event))
{
    wxPrintDialogData printDialogData(* g_printData);

    wxPrinter printer(&printDialogData);
    MyPrintout printout(this, wxT("My printout"));
    if (!printer.Print(this, &printout, true /*prompt*/))
If you want to know more, just trace through this piece of code and see where it takes you :)

However, the whole concept is based on the idea that you use the same code for drawing on the screen and drawing to the printer. That's why the drawing method only takes a wxDC.