Identificando uma impressora matricial
Enviado: 26 Abr 2010 16:38
Tem como identificar via software se a impressora é matricial.
Lócio
Lócio
Em homenagem a Paulo Cesar Toledo
https://pctoledo.org/
Código: Selecionar todos
{$APPTYPE CONSOLE}
uses Windows, Printers, WinSpool, Variants;
{
Using only API calls, determinate which type is the active printer:
Dot-Matrix or Laser (or InkJet)
}
function IsPrinterMatrix: Boolean;
var
DeviceMode: THandle;
Device, Driver, Port: array [0..79] of Char;
pDevice, pDriver, pPort: PChar;
begin
// Determinate that active printer is a Dot-Marix
Result:= False;
pDevice := @Device;
pDriver := @Driver;
pPort := @Port;
Device := #0;
Driver := #0;
Port := #0;
Printer.GetPrinter(pDevice, pDriver, pPort, DeviceMode);
// Printer can be dot-matrix when number of colors is maximum 16
// and when printer is capable to print only for TRUETYPE
// fonts as graphics (dot-matrix and PCL printers are capable for that).
if (GetDeviceCaps(Printer.Handle,NUMCOLORS)<=16) and
(DeviceCapabilities(pDevice, pPort,DC_TRUETYPE,nil,nil) = DCTT_BITMAP)
then
Result := True;
end;
begin
writeln ('Active printer is ', Printer.Printers[Printer.PrinterIndex]);
if IsPrinterMatrix then
writeln('This is a Dot-Matrix printer')
else
writeln('This is a LaserJet or InkJet printer');
end.Código: Selecionar todos
IF oPrn:nLogPixelX() <= 350
MsgInfo("Impressora Matricial - O valor de nLogPixel é "+str(oPrn:nLogPixelX()))
else
MsgInfo("Impressora jato tinta ou Laser - O valor de nLogPixel é "+str(oPrn:nLogPixelX()))
EndifCódigo: Selecionar todos
***********************
FUNCTION CHAMAIMP(cFILE)
***********************
LOCAL nPrn, li, cPrinterName, cPrinterPort, cCopias, cMsg:=""
LOCAL aPrn := GetPrinters(.t.,.f.)
If Empty(aPrn)
alert("Não existe Impressora instalada")
Return
EndIf
nPrn:=New_PRINTSETUP( @cPrinterName, @cCopias )
for li=1 to len(aPrn)
IF aPrn[li,1] = cPrinterName
cPrinterPort = aPrn[li,2]
exit
ENDIF
NEXT
IF cPrinterPort#Nil
IF LEFT(UPPER(cPrinterPort),3)="LPT"
FOR nI := 1 TO cCopias
nResult := PrintFileRaw( cPrinterName, cFILE, "Impressao Pelo Sistema" )
IF nResult = -1
cMsg = "Parametros Invalido, Favor Tentar Novamente"
ELSEIF nResult = -2
cMsg = "Falha na chamada da Impressora, Favor Verificar a Impressora"
ELSEIF nResult = -3
cMsg = "Falha ao Iniciar Impressão, Favor Verificar a Impressora"
ELSEIF nResult = -4
cMsg = "Falha ao Iniciar a Primeira Pagina, Favor Verificar a Impressora"
ELSEIF nResult = -5
cMsg = "Falha de Memoria da Impressora, Favor Verificar a Impressora"
ELSEIF nResult = -6
cMsg = "Não foi Possivel localizar o arquivo de Impressão, Favor Tentar Novamente"
ENDIF
IF len(alltrim(cMsg)) > 1
Fim_Run()
MsgInfo(cMsg,"Aviso do Sistema")
Return
ENDIF
NEXT
ELSE
IF !EMPTY(cPrinterName)
Imprime(cFILE,cPrinterName,cCopias) // impressão grafica
ENDIF
ENDIF
ENDIF
RETURN NIL
Código: Selecionar todos
******************************************
FUNCTION PRINTER_COLORBITS(cPRINTER,cPORT)
******************************************
LOCAL i, hDC, nBitsColor1, nBitsColor2, nBitsColor3
LOCAL aPrn := GetPrinters(.t.,.f.)
IF cPRINTER=Nil
cPRINTER:=GetDefaultPrinter()
ENDIF
IF cPORT=Nil
FOR i=1 to len(aPrn)
IF aPrn[i,1] = cPRINTER
cPORT = aPrn[i,2]
EXIT
ENDIF
NEXT
ENDIF
hDC := CreateDC("Devices", cPRINTER, cPORT)
nBitsColor1 := GetDeviceCaps(hDC, 12) //BITSPIXEL
nBitsColor2 := GetDeviceCaps(hDC, 14) //PLANES
nBitsColor3 := GetDeviceCaps(hDC, 24) //NUMCOLORS
msginfo("Número de cor planos " + VALTOPRG(nBitsColor1) + HB_OsNewLine() + ;
"Número de adjacente bits cor para cada pixel. " + VALTOPRG(nBitsColor2)+ HB_OsNewLine() + ;
"Número de entradas no tabela de cores do dispositivo " + VALTOPRG(nBitsColor3)+ HB_OsNewLine() + ;
"Bits Pixel.: " + VALTOPRG(nBitsColor2*nBitsColor1) + HB_OsNewLine() + ;
"")
DeleteDC(hDc)
Return
Código: Selecionar todos
oPrinter:= Win32Prn():new(printer)
oPrinter:SetPrintQuality(-1)
oPrinter:Copies:= copias
oPrinter:formtype(9)
oPrinter:create()
oPrinter:startDoc()
lar:= oprinter:pagewidth()
alt:= oprinter:pageheight()Código: Selecionar todos
FUNCTION MAIN()
cprinter := GetDefaultPrinter()
oPrinter:= Win_Prn():new(cprinter)
oPrinter:create()
oPrinter:startDoc()
lar:= oPrinter:pagewidth()
alt:= oPrinter:pageheight()
cor:= oPrinter:numcolors()
oprinter:enddoc()
oprinter:destroy()
? lar
? alt
? cor
RETURN NILCódigo: Selecionar todos
FUNCTION Main()
Local oPrinter:=Win_Prn():New(Win_PrinterGetDefault())
If
ISDOTPRINTER(oPrinter,Win_PrinterGetDefault(),Win_PrinterGetDefault())
?'Dot printer - YES'
Else
?'Dot printer - NO'
EndIf
RETURN NIL
#pragma BEGINDUMP
#include <windows.h>
#include "hbapi.h"
HB_FUNC( ISDOTPRINTER )
{
HDC hDC = (HDC) hb_parnl(1) ;
LPCSTR pPrinterName = hb_parcx(2) ;
LPCSTR pPort = hb_parcx(3) ;
// Printer can be dot-matrix when number of colors is maximum 16
// and when printer is capable to print only for TRUETYPE
// fonts as graphics (dot-matrix and PCL printers are capable for that).
if( ( GetDeviceCaps( hDC, NUMCOLORS) <= 16 )
&&( DeviceCapabilities( pPrinterName, pPort,DC_TRUETYPE, NULL, NULL )
== DCTT_BITMAP ) )
hb_retl(TRUE);
else
hb_retl(FALSE);
}
#pragma ENDDUMP
Código: Selecionar todos
#include "minigui.ch"
#include "hbcompat.ch"
#ifndef __XHARBOUR__
#xtranslate At(<a>,<b>,[<x,...>]) => hb_At(<a>,<b>,<x>)
#xtranslate ValToPrgExp([<x,...>]) => hb_valToExp(<x>)
#endif
FUNCTION MAIN()
aPrinters := GetAllPrinters()
cPrinter := WinDefaultPrinter()
aPrn := {}
For i := 1 To Len(aPrinters)
Aadd( aPrn, { Token(aPrinters[i], ",", 1), Token(aPrinters[i], ",", 2) } )
Next
FOR i=1 to Len(aPrn)
IF aPrn[i,1] = cPrinter
cPort = aPrn[i,2]
EXIT
ENDIF
NEXT
PRINTER_COLORBITS(cPrinter,cPort)
RETURN NIL
Function DebugMSG
Local i, aTemp := {}
For i := 1 to pcount()
aadd( aTemp, hb_PValue(i))
Next i
msgbox(hb_valtoexp(aTemp), "Helpful informations")
Return Nil
FUNCTION PRINTER_COLORBITS(cPrinter,cPort)
LOCAL hDC, nBitsColor1, nBitsColor2, nBitsColor3
// hDC := CREATEDC_("devices", cPrinter, cPort)
hDC := CREATEDC_("WINSPOOL", cPrinter, Nil, Nil )
nBitsColor1 := WIN_GetDeviceCaps_(hDC, 12) //BITSPIXEL
nBitsColor2 := WIN_GetDeviceCaps_(hDC, 14) //PLANES
nBitsColor3 := WIN_GetDeviceCaps_(hDC, 24) //NUMCOLORS
msginfo("Número de cor planos " + ValToPrgExp(nBitsColor1) + HB_OsNewLine() + ;
"Número de adjacente bits cor para cada pixel. " + ValToPrgExp(nBitsColor2)+ HB_OsNewLine() + ;
"Número de entradas no tabela de cores do dispositivo " + ValToPrgExp(nBitsColor3)+ HB_OsNewLine() + ;
"Bits Pixel.: " + ValToPrgExp(nBitsColor2*nBitsColor1) + HB_OsNewLine() + ;
"")
DELETEDC_(hDc)
Return
#pragma BEGINDUMP
#include <windows.h>
#include <hbapi.h>
#include "hbapiitm.h"
#ifdef __XHARBOUR__
#define HB_STORC( n, x, y ) hb_storc( n, x, y )
#else
#define HB_STORC( n, x, y ) hb_storvc( n, x, y )
#endif
#define MAX_FILE_NAME 1024
HB_FUNC ( WIN_GETDEVICECAPS_ ) // GetDevCaps ( hwnd )
{
INT ix;
HDC hdc;
HWND hwnd;
hwnd = (HWND) hb_parnl(1);
// hdc = GetDC( hwnd );
// ix = GetDeviceCaps( hdc, LOGPIXELSX );
ix = GetDeviceCaps( hwnd, LOGPIXELSX );
// ix = GetDeviceCaps( hwnd, hb_parnl(2) );
// ReleaseDC( hwnd, hdc );
hb_retni( (UINT) ix );
}
HB_FUNC( CREATEDC_ )
{
hb_retnl( ( LONG ) CreateDC( hb_parc( 1 ), hb_parc( 2 ), hb_parc( 3 ), 0 ) );
}
HB_FUNC( DELETEDC_ )
{
hb_retl( DeleteDC( ( HDC ) hb_parnl( 1 ) ) );
}
HB_FUNC( WINDEFAULTPRINTER )
{
DWORD x, y ;
UCHAR lpReturnedString[MAX_FILE_NAME] ;
x = GetProfileString("windows","device","",lpReturnedString,MAX_FILE_NAME-1);
y = 0 ;
while ( y < x && lpReturnedString[y] != ',' )
y++ ;
hb_retclen(lpReturnedString,y) ;
}
HB_FUNC( GETALLPRINTERS )
{
OSVERSIONINFO osvi;
DWORD dwSize = 0;
DWORD dwPrinters = 0;
DWORD i;
LPBYTE pBuffer ;
LPBYTE cBuffer ;
PRINTER_INFO_5* pInfo;
DWORD flags;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
flags = PRINTER_ENUM_CONNECTIONS|PRINTER_ENUM_LOCAL;
else
flags = PRINTER_ENUM_LOCAL;
EnumPrinters(flags, NULL, 5, NULL, 0, &dwSize, &dwPrinters);
pBuffer = GlobalAlloc(GPTR, dwSize);
if (pBuffer == NULL)
{
hb_reta(0);
return;
}
EnumPrinters(flags, NULL, 5, pBuffer, dwSize, &dwSize, &dwPrinters);
if (dwPrinters == 0)
{
hb_reta(0);
return;
}
pInfo = (PRINTER_INFO_5*)pBuffer;
hb_reta( dwPrinters );
for ( i = 0; i < dwPrinters; i++, pInfo++)
{
cBuffer = GlobalAlloc(GPTR, 256);
strcat(cBuffer,pInfo->pPrinterName);
strcat(cBuffer,",");
strcat(cBuffer,pInfo->pPortName);
HB_STORC( cBuffer , -1 , i+1 );
GlobalFree(cBuffer);
}
GlobalFree(pBuffer);
}
#pragma ENDDUMPCódigo: Selecionar todos
/*
Compilar com hbmk2 Nome_do_Teste -lhbwin
*/
#define MATRICIAL 1
#define DESC_LASER 2
Function Main()
Local cPrinter, i, nMaxLen:=0, nCor, nTPrin, nPrn, aPNomes:={}
Local aPrn:=Win_PrinterList(.t.,.f.)
If Empty( aPrn )
Alert("Nenhuma impressora Instalada")
Return
Endif
For i=1 To Len(aPrn)
AaDd(aPNomes , aPrn[i,1] )
nMaxLen := If( Len( aPrn[i,1] ) > nMaxLen, Len( aPrn[i,1] ), nMaxLen )
Next
Cls
While LastKey() # 27
nPrn := AChoice( 3, 1, Len( aPrn )+1, nMaxLen+1, aPNomes, .T., Nil , aPNomes )
If nPrn = 0
Return
Endi
cPrinter:=aPNomes[nPrn]
oPrinter:= Win_Prn():new(cPrinter)
oPrinter:create()
oPrinter:startdoc()
nCor:=oPrinter:numcolors()
nLar:=oPrinter:pagewidth()
nAlt:=oPrinter:pageheight()
oPrinter:TextOut( oPrinter:PrinterName + ": MaxRow() = " + Str( oPrinter:MaxRow(), 4 ) + " MaxCol() = " + Str( oPrinter:MaxCol(), 4 ) )
oPrinter:enddoc()
oPrinter:destroy()
nTPrin:=If( nCor<3 .And. nLar<1000 .And. nAlt<2000, MATRICIAL, DESC_LASER )
If nTPrin = 1
Alert( 'A impressora em: '+aPrn[nPrn,2]+' é Matricial' )
Else
Alert( 'A impressora em: '+aPrn[nPrn,2]+' é DeskJet ou Laser' )
Endi
EndDo
Return Nil