How to Select a non-default printer?

Everything else that doesn't fall into one of the other PB categories.
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

How to Select a non-default printer?

Post 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
Simon White
dCipher Computing
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to Select a non-default printer?

Post by mk-soft »

Only over "PrintRequester()"

We can't save last selected printer for next printing... :(
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Re: How to Select a non-default printer?

Post by swhite »

Hi

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

Simon.
Simon White
dCipher Computing
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to Select a non-default printer?

Post 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...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
blueb
Addict
Addict
Posts: 1044
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: How to Select a non-default printer?

Post 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
- It was too lonely at the top.

System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to Select a non-default printer?

Post 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
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Re: How to Select a non-default printer?

Post 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
Simon White
dCipher Computing
Post Reply