Printing from report preview.
Posted: 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 ?
www.FiveTechSoft.com
https://fivetechsoft.com/forums/
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.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.
Yes, a "very small time" being less than a second. This is also how my setPrinter() function works.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.
Code: Select all
cPrinter:= prnGetName() // FW function
setPrinter( cNewPrinter )
// print here
setPrinter( cPrinter ) // restore the old printer
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
And, by the way, there wouldn't need to change the default printer. This is how I do it:James Bott wrote:Yes, a "very small time" being less than a second. This is also how my setPrinter() function works.
Code: Select all
cDriver := StrToken( GetProfString( "Devices", cModel, "" ), 1, "," )
cPort := StrToken( GetProfString( "Devices", cModel, "" ), 2, "," )
::hDC := CreateDC( cDriver, cModel, cPort )
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
James Bott wrote:if hDC > 0
Code: Select all
if hDC != 0
James Bott wrote:lSuccess := resetDC( hDC )
Code: Select all
lSuccess := DeleteDC( hDC )
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