Printing from report preview.

Printing from report preview.

Postby HunterEC » Wed Jun 11, 2008 8:25 pm

How can I display the available printers to the user so he/she can choose where to print to ?
HunterEC
 
Posts: 723
Joined: Tue Sep 04, 2007 8:45 am

Postby Roger Seiler » Thu Jun 12, 2008 2: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()

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
User avatar
Roger Seiler
 
Posts: 223
Joined: Thu Dec 01, 2005 3:34 pm
Location: Nyack, New York, USA

Postby Roger Seiler » Thu Jun 12, 2008 2: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
User avatar
Roger Seiler
 
Posts: 223
Joined: Thu Dec 01, 2005 3:34 pm
Location: Nyack, New York, USA

Postby James Bott » Thu Jun 12, 2008 4: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby Roger Seiler » Thu Jun 12, 2008 5: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
User avatar
Roger Seiler
 
Posts: 223
Joined: Thu Dec 01, 2005 3:34 pm
Location: Nyack, New York, USA

Postby James Bott » Thu Jun 12, 2008 6: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby Roger Seiler » Thu Jun 12, 2008 7: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
User avatar
Roger Seiler
 
Posts: 223
Joined: Thu Dec 01, 2005 3:34 pm
Location: Nyack, New York, USA

Postby Enrico Maria Giordano » Thu Jun 12, 2008 7: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
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby James Bott » Fri Jun 13, 2008 1: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:

Code: Select all  Expand view
cPrinter:= prnGetName() // FW function
setPrinter( cNewPrinter )
// print here
setPrinter( cPrinter ) // restore the old printer


Regards,
James

Code: Select all  Expand view
//--- 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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby Armando » Fri Jun 13, 2008 2:32 am

Dear friends:

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

Image

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
User avatar
Armando
 
Posts: 3209
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México

Postby Enrico Maria Giordano » 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:

Code: Select all  Expand view
cDriver := StrToken( GetProfString( "Devices", cModel, "" ), 1, "," )
cPort   := StrToken( GetProfString( "Devices", cModel, "" ), 2, "," )
::hDC := CreateDC( cDriver, cModel, cPort )


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Roger Seiler » Fri Jun 13, 2008 1:12 pm

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

- Roger
User avatar
Roger Seiler
 
Posts: 223
Joined: Thu Dec 01, 2005 3:34 pm
Location: Nyack, New York, USA

Postby James Bott » Fri Jun 13, 2008 3:45 pm

Enrico,

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

James

Code: Select all  Expand view
//--- 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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby Enrico Maria Giordano » Fri Jun 13, 2008 6:15 pm

James Bott wrote:if hDC > 0


Code: Select all  Expand view
if hDC != 0


Look inside printer.prg.

James Bott wrote:lSuccess := resetDC( hDC )


Code: Select all  Expand view
lSuccess := DeleteDC( hDC )


It is needed when CreateDC() is used.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby James Bott » Sat Jun 14, 2008 7:03 am

Enrico,

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

James


Code: Select all  Expand view
#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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 99 guests