List of printers

List of printers

Postby Natter » Wed Oct 19, 2022 11:54 am

Hi,

How to get a list of printers connected to a computer ?
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: List of printers

Postby karinha » Wed Oct 19, 2022 12:27 pm

Code: Select all  Expand view

// http://fivewin.com.br/index.php?/topic/ ... etprintdc/

#include "FiveWin.ch"
#include "Objects.ch"

#define RGB_BLACK      RGB(   0,  0,  0 )
#define RGB_RED        RGB( 255,  0,  0 )
#define RGB_GREEN      RGB(   0,255,  0 )
#define RGB_BLUE       RGB(   0,  0,255 )
#define RGB_CYAN       RGB(   0,255,255 )
#define RGB_YELLOW     RGB( 255,255,  0 )
#define RGB_MAGENTA    RGB( 255,  0,255 )
#define RGB_WHITE      RGB( 255,255,255 )
#define FW_DONTCARE    0
#define FW_THIN        100
#define FW_EXTRALIGHT  200
#define FW_LIGHT       300
#define FW_NORMAL      400
#define FW_MEDIUM      500
#define FW_SEMIBOLD    600
#define FW_BOLD        700
#define FW_EXTRABOLD   800
#define FW_HEAVY       900
#define DMPAPER_A4       9

FUNCTION Main()

   LOCAL oPrn, oPrint, oFnt, cPrinter := ""
   LOCAL oDlg, oBrw
   LOCAL aPrn := {}
   LOCAL cDef, i := 1

   // GetPrinters() and GetDefaultPrinter() do not work in Harbour
   #ifdef __HARBOUR__  

      aPrn := aGetPrinters ()
      cDef := DefaultPrinter()

   #else // Harbour

      aPrn := GetPrinters ()
      cDef := GetDefaultPrinter()

   #endif

   IF Empty ( aPrn )

      MsgAlert ( "No Printers found" )

      RETURN NIL

   ENDIF

   DEFINE DIALOG oDlg FROM 2, 2 TO 21, 50 TITLE "Available Printers"

   @ 1, 2 LISTBOX oBrw FIELDS aPrn[ i ] ;
      HEADERS "Printer Array" ;
      FIELDSIZES 200 ;
      OF oDlg SIZE 100, 100

   oBrw:bGoTop    = { || i := 1 }
   oBrw:bGoBottom = { || i := Eval( oBrw:bLogicLen ) }
   oBrw:bSkip     = { | nWant, nOld | nOld := i, i += nWant, ;
      i := Max( 1, Min( i, Eval( oBrw:bLogicLen ) ) ), ;
      i - nOld }
   oBrw:bLogicLen = { || Len( aPrn ) }
   oBrw:cAlias    = "Array"                // Just put something

   @ 7.8, 2 SAY "Default printer: " + cDef

   @ 1, 20 BUTTON "&Print (FW)" OF oDlg ACTION TestCopias ( aPrn[i] )

   @ 2, 20 BUTTON "&Print (xH)" OF oDlg ACTION Test2 ( aPrn[i] )

   @ 3, 20 BUTTON "&End       " OF oDlg ACTION( oDlg:End() )

   ACTIVATE DIALOG oDlg CENTERED

RETURN NIL

FUNCTION TestCopias( cPrn )  // Imprimir varias copias

   LOCAL nI, nCopias := 2

   // SetCopies
   FOR nI := 1 TO nCopias
      TestPrint( cPrn )
   NEXT

RETURN NIL

FUNCTION TestPrint( cPrn )

   LOCAL oFnt, oPrint
   LOCAL cText := "Este é um texto de teste para impressão"

   PRINT oPrint NAME "Formulario" TO ( cPrn )

      DEFINE FONT oFnt NAME "ARIAL" SIZE 0, - 14 OF oPrint

      // oPrint:SetCopies ( 2 ) // No funciona
      oPrint:SetLandscape()
      // oPrint:Setup ()   // check the settings

      PAGE

         oPrint:CmSay ( 1, 1, cText, oFnt )

      ENDPAGE

   ENDPRINT

   oFnt:End()

RETURN NIL

FUNCTION Test2 ( cPrn )

   LOCAL oPrinter
   LOCAL aFonts, cFont, nFont

   oPrinter           := Win32Prn():new( cPrn )      // Create printer object and configure print job
   oPrinter:landscape := .F.
   oPrinter:formType  := DMPAPER_A4
   oPrinter:copies    := 2  // <--- 2 copies !!

   IF ! oPrinter:create()                       // Create device context
      Alert( "Cannot create device context" )
      QUIT
   ENDIF

   IF ! oPrinter:startDoc( "xHarbour test page" )   // Create print job
      Alert( "Cannot create document" )
      QUIT
   ENDIF

   oPrinter:textOut( "Text in default font" )       // Text in fixed font
   oPrinter:bold( FW_EXTRABOLD )
   oPrinter:textOut( oPrinter:fontName )
   oPrinter:bold( FW_NORMAL )
   oPrinter:newLine()
   aFonts := oPrinter:getFonts()
   nFont  := AScan( aFonts, ;
      {|a| "ARIAL" $ Upper( a[1] ) } )
   cFont  := aFonts[nFont,1]
   oPrinter:setFont( cFont )                       // Text in proportional font
   oPrinter:textOut( "Text in Arial font" )
   oPrinter:bold( FW_EXTRABOLD )
   oPrinter:textOut( oPrinter:fontName )
   oPrinter:bold( FW_NORMAL )
   oPrinter:newLine()
   oPrinter:setColor( RGB_YELLOW, RGB_BLUE )    // Colored text
   oPrinter:textOut( "Yellow on Blue" )
   oPrinter:newLine()
   oPrinter:setPen( PS_DASH, 5, RGB_GREEN )    // Draw colored line across page
   oPrinter:line( oPrinter:posX, ;
   oPrinter:posY, ;
   oPrinter:rightMargin, ;
   oPrinter:posY  )
   oPrinter:endDoc()       // Send output to printer
   oPrinter:destroy()      // Release GDI device context

RETURN NIL

FUNCTION DefaultPrinter()

   LOCAL nPrn, nRet, sGetDefaultPrinter

   nRet := PrinterSetUp()
   nPrn := PrnGetName()

   IF( nRet # 0 )

      SetDefaultPrinter( PRNGETNAME() )

      sGetDefaultPrinter := GetDefaultPrinter()

      // ? PrnStatus( sGetDefaultPrinter )
      IF PrnStatus( sGetDefaultPrinter ) = 4096

         // Verifica se SPOOLER esta desligado e tenta liga-lo
         MsgRun( sGetDefaultPrinter + ": " + isprint( GetDefaultPrinter() ) + ;
            " ou Spooler Desligado.", "Status da Impressora",                 ;
            {|| WinExec( "NET START SPOOLER", 7 ) } )

      ENDIF

      //? GetPrinter()

   ELSE

      MsgAlert ( "No Printers found" )

      RETURN( .F. )

   ENDIF

RETURN( sGetDefaultPrinter )

// FIM
 


Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7824
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: List of printers

Postby Natter » Wed Oct 19, 2022 12:44 pm

Thanks !
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: List of printers

Postby Enrico Maria Giordano » Wed Oct 19, 2022 2:13 pm

Code: Select all  Expand view
FUNCTION ADEVNAMES()

    LOCAL cDevName := STRTRAN( GETPROFSTRING( "Devices" ), CHR( 0 ), CRLF )

    LOCAL aDevNames := { "" }

    LOCAL i

    FOR i = 1 TO MLCOUNT( cDevName )
        AADD( aDevNames, RTRIM( MEMOLINE( cDevName, , i ) ) )
    NEXT

    RETURN ASORT( aDevNames )
User avatar
Enrico Maria Giordano
 
Posts: 8715
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: List of printers

Postby karinha » Wed Oct 19, 2022 2:22 pm

Code: Select all  Expand view

#Include "FiveWin.ch"

FUNCTION Main()

   LOCAL cPrint

   cPrint := ADEVNAMES()

   xBrowse( cPrint )

RETURN NIL

FUNCTION ADEVNAMES()

    LOCAL cDevName := STRTRAN( GETPROFSTRING( "Devices" ), CHR( 0 ), CRLF )

    LOCAL aDevNames := { "" }

    LOCAL i

    FOR i = 1 TO MLCOUNT( cDevName )
       AADD( aDevNames, RTRIM( MEMOLINE( cDevName, , i ) ) )
    NEXT

RETURN ASORT( aDevNames )

// FIN / END
 


Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7824
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: List of printers

Postby nageswaragunupudi » Thu Oct 20, 2022 5:18 pm

Natter wrote:Hi,

How to get a list of printers connected to a computer ?


Code: Select all  Expand view
aPrinters := aGetPrinters() // FWH builtin function
XBROWSER aPrinters
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10641
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: List of printers

Postby Natter » Thu Oct 20, 2022 6:43 pm

Thank you, Rao! I completely forgot about this function :( and wrote everything through WMI. At the same time, I used the EnableBIDI option for control to cut off printers like Microsoft Print to PDF
Natter
 
Posts: 1221
Joined: Mon May 14, 2007 9:49 am

Re: List of printers

Postby Dima » Sun Oct 23, 2022 4:04 pm

WIN_PRINTERGETDEFAULT()
WIN_PRINTERLIST()

Hbwin
Dima
 
Posts: 5
Joined: Thu Nov 10, 2005 6:40 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 71 guests