Page 1 of 2

Printing from report preview.

Posted: Wed Jun 11, 2008 8:25 pm
by HunterEC
How can I display the available printers to the user so he/she can choose where to print to ?

Posted: Thu Jun 12, 2008 2:21 pm
by Roger Seiler
You will have to slightly modify RPreview.prg in order to do this, due to a slight oversight in METHOD PrintPage(). In this method, you will have to add code for a button that calls PrinterSetup(), as shown below.

Here is the defect in the standard version...

Although the PRINT resource of PREV32.DLL provides for a button ID 110 for selecting printers, there is no corresponding code in RPreview:PrintPage(). To fix this, do as follows....

1. Copy RPreview.prg from c:\fwh\source\classes into your app's directory.

2. In your copy of this prg, go to METHOD PrintPage() and insert the following code right under the code there for button ID 102...

REDEFINE BUTTON ID 110 OF oDlg ACTION PrinterSetup()

3. Add RPreview.prg to your app's compile & link list so that your revised version will take precedence over the base version.

That should do it for you.

- Roger

Posted: Thu Jun 12, 2008 2:45 pm
by Roger Seiler
Whoops!

Correction: the button ID 110 for selecting printers is NOT in the standard PREV32.DLL!! I forgot that I had modified my copy of that long ago to add that button.

If you want to use my version of PREV32.DLL (just has a few minor additions in controls including PRINT's ID 110), you can download it from:

http://www.leadersoft.com/software/prev32.dll

- Roger

Posted: Thu Jun 12, 2008 4:32 pm
by James Bott
Hunter,

Keep in mind that the preview is made using the default print driver. If the user then selects a different driver for printing, the susequent printout may not look the same as the preview. This will confuse the user.

James

Posted: Thu Jun 12, 2008 5:41 pm
by Roger Seiler
Hunter,

James is absolutely correct.

Most people don't change printers midsteam. But in the rare case that someone has to change printers on the fly, then they can use my changes to RPreview and Prev32.dll to do that. And then they should cancel and redo the print job so the preview will reflect the newly selected printer and then actually print.

This, of course, is a cumbersome way to do it (though less cumbersome than some other possibilities). A better way would be to have the app automatically redo the print job after a new printer is selected. I just haven't found that this change-of-printer option is used enough to warrant the extra programming to really do it right - always seems to be something else of higher priority.

- Roger

Posted: Thu Jun 12, 2008 6:13 pm
by James Bott
Hunter and Roger,

Many users (and many programmers) don't understand how the print driver system works (I don't completely understand it either). There is a default print driver in the registry that is set using the Control Panel. When any app runs it users this driver as the apps' default driver. You can then select a different driver (within the app) and this driver remains the apps driver until another driver is selected (within the app). The Windows registry default driver remains as it was.

So, you can change the driver within an application without changing the Windows default.

If you have an application that you wish to always default to a different printer, then you can do that via code at the start of the app.

You can also switch from the apps default printer and print to a certain device for one report and then restore the apps default printer (via code).

I usually have a Printer Setup option on the File menu (this is a Windows standard). I also display the current app's printer in the status bar. This way the user can see the current printer and they can change it. Previews are made using this driver and printing from the preview uses this driver. They can change the driver before running the preview.

If anyone wants the save/restore printer routines let me know. This are needed only to automatically change the driver, print, then restore the driver. You would use this only to force printing to a specific driver without changing the apps default driver.

James

Posted: Thu Jun 12, 2008 7:03 pm
by Roger Seiler
James,

Yes, I'd like to see your printer selection routines. It's a confusing area that I'm always looking to improve.

- Roger

Posted: Thu Jun 12, 2008 7:14 pm
by Enrico Maria Giordano
James Bott wrote:Many users (and many programmers) don't understand how the print driver system works (I don't completely understand it either). There is a default print driver in the registry that is set using the Control Panel. When any app runs it users this driver as the apps' default driver. You can then select a different driver (within the app) and this driver remains the apps driver until another driver is selected (within the app). The Windows registry default driver remains as it was.

So, you can change the driver within an application without changing the Windows default.
FWH changes the default printer, gets the hDC of the new printer and then resets the original default printer. This leaves the default printer changed for a very small time.

EMG

Posted: Fri Jun 13, 2008 1:00 am
by James Bott
FWH changes the default printer, gets the hDC of the new printer and then resets the original default printer. This leaves the default printer changed for a very small time.
Yes, a "very small time" being less than a second. This is also how my setPrinter() function works.

So to save and restore the application's current printer do something like this:

Code: Select all

cPrinter:= prnGetName() // FW function
setPrinter( cNewPrinter )
// print here
setPrinter( cPrinter ) // restore the old printer
Regards,
James

Code: Select all

//--- Set application's current printer. Returns .T. if successful.
// Author: James Bott, jbott@compuserve.com
function setPrinter( cPrinter )
   local cOldPrinter:="", hDC:=0, aPrn, cText:="", lSuccess:=.f.

   if cPrinter <> prnGetName()

      cText := StrTran(GetProfString("Devices"),Chr(0), chr(13)+chr(10))
      aPrn  := Array(Mlcount(cText, 250))
      Aeval(aPrn, {|v,e| aPrn[e] := Trim(Memoline(cText, 250, e)) } )

      if  ascan(aPrn,cPrinter) > 0

         cOldPrinter := GetProfString( "windows", "device" , "" )
         WriteProfString( "windows", "device", cPrinter )
         SysRefresh()
         PrinterInit()
         hDC := GetPrintDefault( GetActiveWindow() )
         if hDC>0
            lSuccess:= resetDC( hDC )
         endif
         SysRefresh()
         WriteProfString( "windows", "device", cOldPrinter  )

      endif

   endif
return lSuccess

Posted: Fri Jun 13, 2008 2:32 am
by Armando
Dear friends:

There is the RPREVIEW modified by Manuel Valdenebro, perhaps this can help you

Image

Best regards

Posted: Fri Jun 13, 2008 11:17 am
by Enrico Maria Giordano
James Bott wrote:Yes, a "very small time" being less than a second. This is also how my setPrinter() function works.
And, by the way, there wouldn't need to change the default printer. This is how I do it:

Code: Select all

cDriver := StrToken( GetProfString( "Devices", cModel, "" ), 1, "," )
cPort   := StrToken( GetProfString( "Devices", cModel, "" ), 2, "," )
::hDC := CreateDC( cDriver, cModel, cPort )
EMG

Posted: Fri Jun 13, 2008 1:12 pm
by Roger Seiler
Many thanks, James & Enrico, for your printer-setting code, and your clear explanations. Very helpful.

- Roger

Posted: Fri Jun 13, 2008 3:45 pm
by James Bott
Enrico,

Your suggestion is not working for me. Perhaps you can see what I am doing wrong?

James

Code: Select all

//--- Set application's current printer. Returns .T. if successful.
// Author: James Bott, jbott@compuserve.com
function setPrinter( cPrinter )
   local hDC:=0, aPrn, cText:="", lSuccess:=.f., cDriver:="", cPort:=""

   if cPrinter <> prnGetName()

      cText := StrTran(GetProfString("Devices"),Chr(0), chr(13)+chr(10))
      aPrn  := Array(Mlcount(cText, 250))
      Aeval(aPrn, {|v,e| aPrn[e] := Trim(Memoline(cText, 250, e)) } )

      if  ascan(aPrn,cPrinter) > 0

         cDriver := StrToken( GetProfString( "Devices", cPrinter, "" ), 1, "," )
         cPort   := StrToken( GetProfString( "Devices", cPrinter, "" ), 2, "," )
         hDC := CreateDC( cDriver, cPrinter, cPort )

sysrefresh()

msgInfo( cDriver, "cDriver") // returns "winspool"
msgInfo( cPort, "cPort") // returns "Ne00:"
msgInfo( hDC, "hDC" ) // sometimes positive, sometimes negative

         if hDC > 0
            lSuccess := resetDC( hDC )
         endif
         sysRefresh()

      endif

   endif

return lSuccess

Posted: Fri Jun 13, 2008 6:15 pm
by Enrico Maria Giordano
James Bott wrote:if hDC > 0

Code: Select all

if hDC != 0
Look inside printer.prg.
James Bott wrote:lSuccess := resetDC( hDC )

Code: Select all

lSuccess := DeleteDC( hDC )
It is needed when CreateDC() is used.

EMG

Posted: Sat Jun 14, 2008 7:03 am
by James Bott
Enrico,

I have made the changes you suggested but the routine is not restoring the original printer. Ideas?

James

Code: Select all

#include "fivewin.ch"

function main()
   local oWnd
   define window oWnd
   activate window oWnd on init doit()

return nil

function doit()

   local cPrinter:= prnGetName()

   printerSetup()
   msgInfo( prnGetName() )

   setPrinter( cPrinter )
   msgInfo( prnGetName() )

return nil



//--- Set application's current printer. Returns .T. if successful.
// Author: James Bott, jbott@compuserve.com
function setPrinter( cPrinter )
   local hDC:=0, aPrn, cText:="", lSuccess:=.f., cDriver:="", cPort:=""

   if cPrinter <> prnGetName()

      cText := StrTran(GetProfString("Devices"),Chr(0), chr(13)+chr(10))
      aPrn  := Array(Mlcount(cText, 250))
      Aeval(aPrn, {|v,e| aPrn[e] := Trim(Memoline(cText, 250, e)) } )

      if  ascan(aPrn,cPrinter) > 0

         // Code by Enrico Maria Giordano
         cDriver := StrToken( GetProfString( "Devices", cPrinter, "" ), 1, "," )
         cPort   := StrToken( GetProfString( "Devices", cPrinter, "" ), 2, "," )
         hDC := CreateDC( cDriver, cPrinter, cPort )

sysrefresh()

msgInfo( cDriver, "cDriver") // returns "winspool"
msgInfo( cPort, "cPort") // returns "Ne00:"
msgInfo( hDC, "hDC" ) // sometimes positive, sometimes negative

         if hDC != 0
            lSuccess := DeleteDC( hDC )
         endif
         sysRefresh()

      endif

   endif

return lSuccess