What you need to work around the print bug when setting UseHwnd to No:
w_printdummy - just an empty Window object with the visible flag set to false (and minimize fo safety :) ), nothing else. I inherited from PFC's w_master but any kind of window should do.
forward
global type w_printdummy from w_master
end type
end forward
global type w_printdummy from w_master
boolean visible = false
windowstate windowstate = minimized!
end type
global w_printdummy w_printdummy
on w_printdummy.create
call super::create
end on
on w_printdummy.destroy
call super::destroy
end on
u_dw_printdummy - this is used to display the proper print dialog to the user and thus get the proper print specs. Inherited from PFC's u_dw. Full source code:
forward
global type u_dw_printdummy from u_dw
end type
end forward
global type u_dw_printdummy from u_dw
end type
global u_dw_printdummy u_dw_printdummy
type variables
private:
boolean ib_usercancelled = true
end variables
forward prototypes
public function long of_printsetup (long al_pages)
end prototypes
public function long of_printsetup (long al_pages);/////////////////////////////////////////////////////////////////////////
//
// Display print dialog to set print specs needed for Datastore printing
//
/////////////////////////////////////////////////////////////////////////
long ll_rc
this.modify ( 'datawindow.print.preview=yes' )
do
// Insert rows until pagecount is the same as source DS.
// This is needed so the user can set the desired pages in the print dialog.
this.insertrow( 0 )
// I have not encountered an issue with this so far but There may be
// a need for safeguarding against a potential infinite loop here.
// To be safe, maybe add a counter variable and exit the loop
// when an implausibly high loop count is hit.
loop while long ( this.describe ("evaluate('pagecount()'," + string ( this.rowcount() ) + ")")) < al_pages
this.modify ( 'datawindow.print.preview=no' )
// We only want the print dialog to set all the specs to the DW so printing is cancelled
// immediately in printstart(). Therefore print() itself will *always* return -1 here.
// In order to still correctly identify the user pressing "Cancel" during the print dialog, this flag is set to true.
// If it wasn't reset to false in printstart(), we know that the dialog was cancelled by the user.
ib_usercancelled = true
this.print( true, true )
if not ib_usercancelled then
// continue printing the DS
ll_rc = 1
else
// dialog was cancelled by user, return -1 accordingly
ll_rc = -1
end if
// reset flag to default
ib_usercancelled = true
return ll_rc
end function
on u_dw_printdummy.create
call super::create
end on
on u_dw_printdummy.destroy
call super::destroy
end on
event printstart;call super::printstart;/////////////////////////////////////////////////////////////////////////
//
// Cancel print immediately.
// At this point the print dialog was OK'd so set ib_usercancelled to false
//
/////////////////////////////////////////////////////////////////////////
ib_usercancelled = false
this.printcancel( )
end event
Then you need to override your base Datastore userobject's pfc_print event and add an additional function of_printsetup. To make this work even with non-PFC applications, I guess the print-function itself could be overridden but you'd still need to have a custom datastore objects as a base for all your datastores. A plain datastore will not print properly.
pfc_print:
event pfc_print;/////////////////////////////////////////////////////////////////////////
//
// Overridden, workaround for when UseHwnd is No
//
/////////////////////////////////////////////////////////////////////////
long ll_rc
// I've added a flag to use the regular print event
if ib_printlegacy then
ll_rc = super::event pfc_print()
else
// call method that displays print dialog and sets the specs
ll_rc = this.of_printsetup( )
if ll_rc > 0 then
// print silently
ll_rc = this.print( false, false )
end if
end if
return ll_rc
of_printsetup:
private function long of_printsetup ();/////////////////////////////////////////////////////////////////////////
//
// Opens dummy window to show regular print dialog. Gets print specs and
// sets them via modify
//
/////////////////////////////////////////////////////////////////////////
u_dw_printdummy ldw_dw
w_printdummy lw_dummy
long ll_rc
long ll_pagecount
//Use print preview to get current pagecount
this.modify ( 'datawindow.print.preview=yes' )
ll_PageCount = long ( this.describe ( "evaluate('pagecount()', 1)" ) )
this.modify ( 'datawindow.print.preview=no' )
// Open dummy window and have it open the dummy DW
Open(lw_dummy)
ldw_dw = create u_dw_printdummy
lw_dummy.openuserobject( ldw_dw )
ldw_dw.dataobject = this.dataobject
// call function that displays the dialog
ll_rc = ldw_dw.of_printsetup(ll_PageCount)
// get print specs from DW and set them to this DS via modify
if ll_rc > 0 then
this.Modify("DataWindow.Print.Orientation= '" + ldw_dw.describe("DataWindow.Print.Orientation") + "'")
this.Modify("DataWindow.Print.Copies= '" + ldw_dw.describe("DataWindow.Print.Copies") + "'")
this.Modify("DataWindow.Print.DocumentName= '" + ldw_dw.describe("DataWindow.Print.DocumentName") + "'")
this.Modify("DataWindow.Print.Duplex= '" + ldw_dw.describe("DataWindow.Print.Duplex") + "'")
this.Modify("DataWindow.Print.FileName= '" + ldw_dw.describe("DataWindow.Print.FileName") + "'")
this.Modify("DataWindow.Print.Margin.Bottom= '" + ldw_dw.describe("DataWindow.Print.Margin.Bottom") + "'")
this.Modify("DataWindow.Print.Margin.Left= '" + ldw_dw.describe("DataWindow.Print.Margin.Left") + "'")
this.Modify("DataWindow.Print.Margin.Right= '" + ldw_dw.describe("DataWindow.Print.Margin.Right") + "'")
this.Modify("DataWindow.Print.Margin.Top= '" + ldw_dw.describe("DataWindow.Print.Margin.Top") + "'")
this.Modify("DataWindow.Print.Page.Range= '" + ldw_dw.describe("DataWindow.Print.Page.Range") + "'")
this.Modify("DataWindow.Print.Page.RangeInclude= '" + ldw_dw.describe("DataWindow.Print.Page.RangeInclude") + "'")
this.Modify("DataWindow.Print.Paper.Size= '" + ldw_dw.describe("DataWindow.Print.Paper.Size") + "'")
this.Modify("DataWindow.Print.Paper.Source= '" + ldw_dw.describe("DataWindow.Print.Paper.Source") + "'")
this.Modify("DataWindow.Print.PrinterName= '" + ldw_dw.describe("DataWindow.Print.PrinterName") + "'")
this.Modify("DataWindow.Print.Prompt= '" + ldw_dw.describe("DataWindow.Print.Prompt") + "'")
this.Modify("DataWindow.Print.Quality= '" + ldw_dw.describe("DataWindow.Print.Quality") + "'")
this.Modify("DataWindow.Print.Scale= '" + ldw_dw.describe("DataWindow.Print.Scale") + "'")
this.Modify("DataWindow.Printer= '" + ldw_dw.describe("DataWindow.Printer") + "'")
end if
//Close dummy DW and window
lw_dummy.closeuserobject( ldw_dw )
close(lw_dummy)
return ll_rc