Hi,
How to get a list of printers connected to a computer ?
List of printers
- karinha
- Posts: 8016
- Joined: Tue Dec 20, 2005 7:36 pm
- Location: São Paulo - Brasil
- Been thanked: 8 times
- Contact:
Re: List of printers
Code: Select all | Expand
// 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 9FUNCTION 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 CENTEREDRETURN NILFUNCTION TestCopias( cPrn ) // Imprimir varias copias LOCAL nI, nCopias := 2 // SetCopies FOR nI := 1 TO nCopias TestPrint( cPrn ) NEXTRETURN NILFUNCTION 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 NILFUNCTION 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 contextRETURN NILFUNCTION 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. ) ENDIFRETURN( sGetDefaultPrinter )// FIM
Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
- Enrico Maria Giordano
- Posts: 8780
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Has thanked: 1 time
- Been thanked: 8 times
- Contact:
Re: List of printers
Code: Select all | Expand
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 )
- karinha
- Posts: 8016
- Joined: Tue Dec 20, 2005 7:36 pm
- Location: São Paulo - Brasil
- Been thanked: 8 times
- Contact:
Re: List of printers
Code: Select all | Expand
#Include "FiveWin.ch"FUNCTION Main() LOCAL cPrint cPrint := ADEVNAMES() xBrowse( cPrint )RETURN NILFUNCTION 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 ) ) ) NEXTRETURN ASORT( aDevNames )// FIN / END
Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
- nageswaragunupudi
- Posts: 10733
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Been thanked: 11 times
- Contact:
Re: List of printers
Natter wrote:Hi,
How to get a list of printers connected to a computer ?
Code: Select all | Expand
aPrinters := aGetPrinters() // FWH builtin functionXBROWSER aPrinters
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India
Re: List of printers
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
