Changing default printer for current application only

Changing default printer for current application only

Postby hua » Thu Mar 02, 2023 7:47 am

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

 
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: Changing default printer for current application only

Postby James Bott » Thu Mar 02, 2023 3:31 pm

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
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Changing default printer for current application only

Postby hua » Fri Mar 03, 2023 2:50 am

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
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 107 guests