FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Printing from report preview.
Posts: 723
Joined: Tue Sep 04, 2007 08:45 AM
Printing from report preview.
Posted: Wed Jun 11, 2008 08:25 PM

How can I display the available printers to the user so he/she can choose where to print to ?

Posts: 223
Joined: Thu Dec 01, 2005 03:34 PM
Printing from report preview.
Posted: Thu Jun 12, 2008 02:21 PM

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()

  1. 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
Posts: 223
Joined: Thu Dec 01, 2005 03:34 PM
Printing from report preview.
Posted: Thu Jun 12, 2008 02:45 PM
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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Printing from report preview.
Posted: Thu Jun 12, 2008 04:32 PM

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

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 223
Joined: Thu Dec 01, 2005 03:34 PM
Printing from report preview.
Posted: Thu Jun 12, 2008 05:41 PM

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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Printing from report preview.
Posted: Thu Jun 12, 2008 06:13 PM

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

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 223
Joined: Thu Dec 01, 2005 03:34 PM
Printing from report preview.
Posted: Thu Jun 12, 2008 07:03 PM

James,

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

  • Roger
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Printing from report preview.
Posted: Thu Jun 12, 2008 07:14 PM
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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Printing from report preview.
Posted: Fri Jun 13, 2008 01:00 AM
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:

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


Regards,
James

//--- 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
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Printing from report preview.
Posted: Fri Jun 13, 2008 02:32 AM
Dear friends:

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



Best regards
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Printing from report preview.
Posted: Fri Jun 13, 2008 11:17 AM
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:

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


EMG
Posts: 223
Joined: Thu Dec 01, 2005 03:34 PM
Printing from report preview.
Posted: Fri Jun 13, 2008 01:12 PM

Many thanks, James & Enrico, for your printer-setting code, and your clear explanations. Very helpful.

  • Roger
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Printing from report preview.
Posted: Fri Jun 13, 2008 03:45 PM
Enrico,

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

James

//--- 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
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Printing from report preview.
Posted: Fri Jun 13, 2008 06:15 PM
James Bott wrote:if hDC > 0


if hDC != 0


Look inside printer.prg.

James Bott wrote:lSuccess := resetDC( hDC )


lSuccess := DeleteDC( hDC )


It is needed when CreateDC() is used.

EMG
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Printing from report preview.
Posted: Sat Jun 14, 2008 07:03 AM
Enrico,

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

James


#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
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10