以前有看過,不過只能使用LPT1 或 EPSON廠牌,無法使用USB LPT 界面及HP廠牌?
#include //INI file
#include #pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1; String ppstr;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
} //§ì¨ú¦Lªí¾÷«¬¸¹//-----------------------------------------------------------------------------
void GetDefaultPrinter(char *printer_name)
{
char *def_string = "no printer";
char buffer[MAX_PATH];
GetProfileString("windows", "device", def_string,buffer, MAX_PATH); AnsiString Abuffer(buffer);
int first_comma = Abuffer.Pos(",");
strcpy(printer_name, Abuffer.SubString(1, first_comma - 1).c_str());
} //-------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{ ////Other Pring Mode Example ////////////
/*
Printers::Printer()->Refresh();
int LineNum , MaxLineNum , LineSpace , FontHeight, posy , i; if (PrintDialog1->Execute())
{ Printer()->Canvas->Font = Memo1->Font;//³]©w¦r«¬»PMemo1¤¤ªº¦r«¬¬Û¦P
Printer()->BeginDoc();//<<±Ò°Ê¤@Ó·sªº¦Lªí¤u§@
LineNum = 0;
//¨ú¦Lªí¾÷²{¦b¦r«¬ªº°ª«×
FontHeight = Printer()->Canvas->TextHeight("Ay");
MaxLineNum = (Printer()->PageHeight/(FontHeight 10))-4;
LineSpace = FontHeight 10;
posy = LineSpace; //¦C¦L««ª½¤è¦Vªº¦ì¸m
for (i=0;iLines->Count;i )
{
//§â¤å¦r¤@¦æ¦æªº¦C¦L¥X¨Ó
Printer()->Canvas->TextOut(10,posy,Memo1->Lines->Strings[i]);
LineNum ;//pºâ¦æ¼Æ
posy =LineSpace;//««ª½¤è¦V¦ì¸m
if (LineNum > MaxLineNum)
{//¦pªG¶W¹L³Ì¤j¦æ¼Æ´N¸õ¶
Printer()->NewPage();//°e¥X¸õ¶½X
posy = LineSpace;
LineNum = 0;
}
}
Printer()->EndDoc();//<<<µ²§ô¦C¦L«ü¥O
} */
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{ String st="012345678901234567890AAAAAAAAAAAA" AnsiString(char(0x0a));//0x0a´«¦æ(carriage)
char *szPrinterName= ppstr.c_str();
char *szBuffer= st.c_str(); HANDLE hPrinter;
int iLen = strlen(szBuffer);// n¦C¦L¶Ç°e¸ê®Æªºªø«×
DWORD dwWritten; // ¸ê®Æ¶q¦h¤Ö // ¶}±Ò¨èú±o¤@Ó¦C¦L¦æµ{HANDLE
if( OpenPrinterA(szPrinterName, &hPrinter, NULL) )
{
//¬ÛÃö¦C¦L°Ñ¼Æ¸ê°T
DOC_INFO_1 Info;
int szInfo = sizeof(Info);
memset(&Info, 0, szInfo); //²M°£buffer
Info.pDocName = "The Test Print"; // µ¹Óprint±M®×¦WºÙ
Info.pOutputFile = NULL; //¤£¦s¦¨ÀɮשҥH NULL´N¥i¥H
Info.pDatatype = "RAW"; //¶Ç°e¼Ò¦¡¨Ï¥Î RAW Format // ¶}±Ò¦C¦L
if(!StartDocPrinterA(hPrinter, 1, (LPBYTE)&Info))
ShowMessage("StartDocPrinter Error~"); StartPagePrinter(hPrinter); // ¶}±Ò¦C¦L¶¶Ç¤J«ü©wªº¦C¦L¦æHANDLE
if(!WritePrinter(hPrinter, szBuffer, iLen, &dwWritten))
{
ShowMessage("¦C¦L¿ù»~");// ¼g¤J¿ù»~
} EndPagePrinter(hPrinter);//§¹¦¨µ²§ô¦C¦L¶ if(!EndDocPrinter(hPrinter))
ShowMessage("Ãö³¬¿ù»~"); ClosePrinter(hPrinter);//Ãö³¬¦C¦L¦æµ{
ListBox1->Items->Add(st);
}
else
{
ShowMessage("OpenPrinter Error~");
}
}
發表人 - taishyang 於 2004/06/29 12:31:04