Print Dialog does not display

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
al@work
In need of some credit
In need of some credit
Posts: 3
Joined: Fri Feb 05, 2021 4:45 pm

Print Dialog does not display

Post by al@work »

Greetings.

I am getting a system error when trying to display the printer dialog. The WxWdigets sample printing program works just fine.

System build environment:
Windows 10.
WxWidgets 3.1.3.
64 bit VS2019 build.

I basically copied the code from the sample program into my program.

This code is in my App Class OnInit function.

Code: Select all

        wxInitAllImageHandlers();
        // init global objects
        g_printData = new wxPrintData;

        // You could set an initial paper size here
#if 0
        g_printData->SetPaperId(wxPAPER_LETTER); // for Americans
        g_printData->SetPaperId(wxPAPER_A4);    // for everyone else
#endif

        g_pageSetupData = new wxPageSetupDialogData;

        // copy over initial paper size from print record
        (*g_pageSetupData) = *g_printData;

        // Set some initial page margins in mm.
        g_pageSetupData->SetMarginTopLeft(wxPoint(10, 10));
        g_pageSetupData->SetMarginBottomRight(wxPoint(10, 10));
This is the code in my OnPrint handler

Code: Select all

	wxPrintDialogData printDialogData(* g_printData);

	wxPrinter printer(&printDialogData);

	MyPrintout printout(this, "My printout");

//	if (!printer.Print(this, &printout, false /*prompt*/))
	if (!printer.Print(this, &printout, true /*prompt*/))
	{
		if (wxPrinter::GetLastError() == wxPRINTER_ERROR)
		{
			wxLogError("There was a problem printing. Perhaps your current printer is not set correctly?");
		}
		else
		{
			wxLogMessage("You canceled printing");
		}
	}
	else
	{
		(*g_printData) = printer.GetPrintDialogData().GetPrintData();
	}
When the code runs, the printer dialog flashes on the screen and then a system message is displayed stating "An error occurred during this operation".
wxPrinter::getLAstError() returns 1 and the "You Canceled Printing" message is displayed.

If I call printer.Print() with the prompt parameter set to false, the document prints to the default printer without a problem,

Interestingly, the dialog displays properly on a Windows 7 machine. I tested it on 4 Windows 10 machines and they all throw the system error.

I think I am not initializing something properly either in the code or in the project settings, but I have scoured each and cannot find a difference.

Any advice is greatly appreciated.

Thanks,
Al
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Print Dialog does not display

Post by ONEEYEMAN »

Hi,
Are you running Debug build of the library and your application?
If not please build a Debug version, run it under the debugger and see if that give you any clue.

Thank you.
al@work
In need of some credit
In need of some credit
Posts: 3
Joined: Fri Feb 05, 2021 4:45 pm

Re: Print Dialog does not display

Post by al@work »

Thanks ONEEYEMAN:

I am not running the debug version of the library.
I will build and update with results. Might be after the weekend.

Thanks again.
Al
al@work
In need of some credit
In need of some credit
Posts: 3
Joined: Fri Feb 05, 2021 4:45 pm

Re: Print Dialog does not display

Post by al@work »

Update:

So I built wxWidgets debug version and have the precise place where my failure is occurring. In the file printwin.cpp, line 101. The following code is failing to get a dc.

Code: Select all

dc = wxDynamicCast(PrintDialog(parent), wxPrinterDC);
I've compared the parent object in both the sample program and mine and cannot find any differences. The printer dc objects look the same.

I've gone through my app class and canvas class and printout class insuring that they are handling the dc acquisition the same way as the sample program. I've compared project settings between the sample and mine app and do not see any differences. Obviously I am missing something. I'm sure it's something very simple.
Nunki
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Fri Sep 14, 2012 8:26 am
Location: Kontich, Belgium
Contact:

Re: Print Dialog does not display

Post by Nunki »

I use code like this

hPrnDlg = new wxPrintDialog(rApp.pMainWindow,&this->PrnData);
if (hPrnDlg->ShowModal() == wxID_OK)
{
this->hPrnDev = static_cast<wxPrinterDC *>(hPrnDlg->GetPrintDC());
}

This is what you use when the user needs to choose the printer. If you already know the printer's name you could open a handle to that device directly with the

this->hPrnDev = new wxPrinterDC(this->PrnData);

have fun,
Nunki
Post Reply