wxPrintout(): Where does the wxPrinterDC come from?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Rob190
In need of some credit
In need of some credit
Posts: 3
Joined: Sun Jun 14, 2020 8:20 pm

wxPrintout(): Where does the wxPrinterDC come from?

Post by Rob190 » Sun Jun 14, 2020 9:06 pm

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.

User avatar
doublemax
Moderator
Moderator
Posts: 15265
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post by doublemax » Sun Jun 14, 2020 9:28 pm

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;
}
Use the source, Luke!

Rob190
In need of some credit
In need of some credit
Posts: 3
Joined: Sun Jun 14, 2020 8:20 pm

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

Post by Rob190 » Sun Jun 14, 2020 9:52 pm

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.

User avatar
doublemax
Moderator
Moderator
Posts: 15265
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post by doublemax » Sun Jun 14, 2020 10:27 pm

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.
Use the source, Luke!

Post Reply