Page 1 of 1

Changing default printer for current application only

PostPosted: Thu Mar 02, 2023 7:47 am
by hua
Guys,
I am getting different result here when I compile this code with my FWH11.08 and FWH1912.

This is the old behavior that I want to retain.
1. Run program
2. Click menu Printer Setup
3. Select a different printer than windows default. e.g. Microsoft Print To Pdf
4. Click Print [will go to recently selected printer]
5. Click Print again [will still go the printer recently selected within the program]

This is the behavior with FWH1912
1. Run program
2. Click menu Printer Setup
3. Select a different printer than windows default. e.g. Microsoft Print To Pdf
4. Click Print [will go to recently selected printer]
5. Click Print again [will now go to windows default printer]

Any help is deeply appreciated as customers have been complaining about the different behavior after their program was upgraded.

TIA

This is the self-contained test code
Code: Select all  Expand view

#include "FiveWin.ch"

static oWnd

//----------------------------------------------------------------------------//

function Main()

   DEFINE WINDOW oWnd FROM 1, 1 TO 20, 60 MENU SysMenu()

   @ 3, 3 BUTTON "&Print" OF oWnd SIZE 80, 20 ;
      ACTION PrintMe()  // try also with oWnd:HardCopy()

   ACTIVATE WINDOW oWnd

return nil

//----------------------------------------------------------------------------//

function PrintMe()

   local oPrn

   PRINT oPrn NAME "Test"
      PAGE
         oPrn:Say( 20, 20, "This is a test" )
      ENDPAGE
   ENDPRINT

return nil

//----------------------------------------------------------------------------//
STATIC FUNC SysMenu()

   local oMenu

   MENU oMenu

      MENUITEM "Printer Setup" action printerSetup()
      MENUITEM "Exit" action oWnd:end()
   ENDMENU
return oMenu

 

Re: Changing default printer for current application only

PostPosted: Thu Mar 02, 2023 3:31 pm
by James Bott
Hua,

I don't know if this will help, but I found it in my notes.

Use the following to set the application's current printer. It will remain the current printer until changed.

Code: Select all  Expand view
//--- Set application's current printer. Returns .T. if successful.
// Author: James Bott
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

Re: Changing default printer for current application only

PostPosted: Fri Mar 03, 2023 2:50 am
by hua
Thanks James.
The code helps in setting the app to the last printer selected.
For my earlier problem, I rem out PrinterEnd() at method End() of TPrinter