Page 1 of 1

How to Select a non-default printer?

Posted: Fri Sep 20, 2019 4:55 pm
by swhite
Hi

Is there a command in Purebasic that can be used to select a printer that is not the default printer?

Thanks
Simon

Re: How to Select a non-default printer?

Posted: Fri Sep 20, 2019 6:51 pm
by mk-soft
Only over "PrintRequester()"

We can't save last selected printer for next printing... :(

Re: How to Select a non-default printer?

Posted: Sat Sep 21, 2019 2:40 pm
by swhite
Hi

That is too bad because I often need to do it without user interaction when reports are automated.

Simon.

Re: How to Select a non-default printer?

Posted: Sat Sep 21, 2019 3:07 pm
by mk-soft
It's been on the wish list a long time.
There was a procedure somewhere for windows to change the default printer...

Re: How to Select a non-default printer?

Posted: Sun Sep 22, 2019 1:12 pm
by blueb
Here's something from Sparkie and Gnozal that can find my default printer
and may point you in the right direction...

Code: Select all

; ***************************************************** 
; Code    : Print Text File 
; Author  : Sparkie 
; Date    : December 2, 2006 
; OS      : Windows NT, 2000, XP, 2003, Vista 
; PB Ver  : 4.01 
; License : It's all yours to use as you so desire 
;           (props/credit are welcome) 
; ***************************************************** 

If OSVersion() =  #PB_OS_Windows_95 Or OSVersion() =  #PB_OS_Windows_98 Or OSVersion() =  #PB_OS_Windows_ME 
  MessageRequester("Error", "Requires Win/NT, 2000, XP, 2003, or Vista", #MB_OK | #MB_ICONERROR) 
  End 
EndIf 

;...Procedure to select text file to print 
Procedure PrintThis() 
  Pattern$ = "Text (*.txt)|*.txt;*.bat|PureBasic (*.pb)|*.pb|All files (*.*)|*.*"
  Pattern = 0    ; use the first of the three possible patterns as standard

  file$ = OpenFileRequester("Select File", "", Pattern$, Pattern) 
  printer$ = Chr(34) + GetGadgetText(1) + Chr(34)
  result = ShellExecute_(0, "printto", file$, printer$, "", #SW_HIDE)
  If result <> 42
    strmsg.s = "Detected an error from ShellExecute... error number:" + Str(result)
    MessageRequester("Error", strmsg, #MB_OK | #MB_ICONERROR)
  EndIf
  ProcedureReturn result 
EndProcedure 

;...Procedure to get printer names for use with "printto" 
Procedure GetPrinters() 
  result.l = 0 
  numPrinters.l = 0 
  bytesCopied.l = 0 
  ;...Enum printer names as we need string names to use "printto" 
  If EnumPrinters_(#PRINTER_ENUM_NAME, "", 5, 0, 0, @bytesCopied, @numPrinters) = 0 
    items.l = Round(bytesCopied / SizeOf(PRINTER_INFO_5), 1) 
    Dim pi5.PRINTER_INFO_5(items)    
    If EnumPrinters_(#PRINTER_ENUM_NAME, "", 5, @pi5(), bytesCopied, @bytesCopied, @numPrinters) <> 0 
      ;...Credit for getting Default Printer goes to gnozal :) 
      buff$ = Space(260) 
      If GetPrivateProfileString_("WINDOWS", "DEVICE", "", @buff$, 260, "Win.Ini") 
        defPrinter$ = StringField(buff$, 1,",") 
      EndIf 
      ;...Fill our ComboGadget with printer name(s) 
      For i = 0 To numPrinters - 1 
        AddGadgetItem(1, -1, PeekS(pi5(i)\pPrinterName)) 
        If defPrinter$ = PeekS(pi5(i)\pPrinterName) 
          SetGadgetState(1, i) 
        EndIf 
      Next i 
      DisableGadget(2, 0) 
      result = 1 
    Else 
      result = 0 
      MessageRequester("Error", "Could not find printer(s)", #MB_OK | #MB_ICONERROR) 
      DisableGadget(2, 1) 
    EndIf 
  EndIf 
  Dim pi.PRINTER_INFO_5(0) 
  ProcedureReturn result 
EndProcedure 
  
If OpenWindow(0, 0, 0, 300, 130, "Print Text File", #PB_Window_ScreenCentered | #PB_Window_SystemMenu) 
  TextGadget(0, 10, 20, 100, 20, "Select Printer") 
  ComboBoxGadget(1, 10, 40, 200, 20) 
  ButtonGadget(2, 10, 80, 100, 25, "Select file to print") 
  GetPrinters() 
  
  Repeat 
    Event = WaitWindowEvent() 
    If Event = #PB_Event_Gadget And EventGadget() = 2 
      PrintThis() 
    EndIf 
  Until Event = #PB_Event_CloseWindow 
EndIf 
End

Re: How to Select a non-default printer?

Posted: Sun Sep 22, 2019 8:28 pm
by Shardik
swhite wrote:Is there a command in Purebasic that can be used to select a printer that is not the default printer?
There is no command in PureBasic. But for Windows you may try one of the following old examples using API commands to set a new default printer. So in effect you would read and store the current default printer, change the default printer and after your printing job you would restore the previous default printer.
- shim
- Rings
- ts-soft
- hjbremer
- Max_der_Held

Re: How to Select a non-default printer?

Posted: Tue Sep 24, 2019 1:45 pm
by swhite
Now that I know there is no command to choose a printer without user interaction I will use the Windows API.

Thanks
Simon