How can I display the available printers to the user so he/she can choose where to print to ?
How can I display the available printers to the user so he/she can choose where to print to ?
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....
Copy RPreview.prg from c:\fwh\source\classes into your app's directory.
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()
That should do it for you.
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
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.
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
James,
Yes, I'd like to see your printer selection routines. It's a confusing area that I'm always looking to improve.
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.
cPrinter:= prnGetName() // FW function
setPrinter( cNewPrinter )
// print here
setPrinter( cPrinter ) // restore the old printer//--- 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
James Bott wrote:Yes, a "very small time" being less than a second. This is also how my setPrinter() function works.
cDriver := StrToken( GetProfString( "Devices", cModel, "" ), 1, "," )
cPort := StrToken( GetProfString( "Devices", cModel, "" ), 2, "," )
::hDC := CreateDC( cDriver, cModel, cPort )Many thanks, James & Enrico, for your printer-setting code, and your clear explanations. Very helpful.
//--- 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 lSuccessJames Bott wrote:if hDC > 0
if hDC != 0James Bott wrote:lSuccess := resetDC( hDC )
lSuccess := DeleteDC( hDC )#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