unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

    var dpCrLf : AnsiString = chr(13)+chr(10);
    var szSavePath : String = 'C:\Argox';
    var szSaveFile : AnsiString = 'C:\Argox\PPLB_Example.Prn';
    var sznop1     : AnsiString = 'nop_front' + Chr(13) + chr(10);
    var sznop2     : AnsiString = 'nop_middle' + Chr(13) + chr(10);
    function  B_Bar2d_Maxi(x:integer; y:integer; cl:integer; cc:integer; pc:integer; data:AnsiString)
              :integer;stdcall;external 'WINPPLB.DLL';
    function  B_Bar2d_PDF417(x:integer; y:integer; w:integer; v:integer; s:integer; c:integer;
              px:integer; py:integer; r:integer; l:integer; t:integer; o:integer; data:AnsiString):integer;
              stdcall;external 'WINPPLB.DLL';
    function  B_Bar2d_PDF417_N(x:integer; y:integer; w:integer; h:integer; pParameter:AnsiString; data:AnsiString):integer;
              stdcall;external 'WINPPLB.DLL';
    function  B_Bar2d_DataMatrix(x:integer; y:integer; r:integer; l:integer; h:integer; v:integer; data:AnsiString):integer;
              stdcall;external 'WINPPLB.DLL';
    Procedure B_ClosePrn();stdcall;external 'WINPPLB.DLL';
    function  B_CreatePrn(selection:integer; filename:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Del_Form(formname:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Del_Pcx(pcxname:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Draw_Box(x:integer; y:integer; thickness:integer; hor_dots:integer; ver_dots:integer)
              :integer;stdcall;external 'WINPPLB.DLL';
    function  B_Draw_Line(mode:char; x:integer; y:integer; hor_dots:integer; ver_dots:integer)
              :integer;stdcall;external 'WINPPLB.DLL';
    function  B_Error_Reporting(option:char):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Get_DLL_Version(nShowMessage:integer):PAnsiChar;stdcall;external 'WINPPLB.DLL';
    function  B_Get_DLL_VersionA(nShowMessage:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Get_Graphic_ColorBMP(x:integer; y:integer; filename:AnsiString):integer;stdcall;
              external 'WINPPLB.DLL';
    function  B_Get_Graphic_ColorBMPEx(x:integer; y:integer; nWidth:integer; nHeight:integer;
              rotate:integer; id_name:AnsiString; filename:AnsiString):integer;
              stdcall;external 'WINPPLB.DLL';
    function  B_Get_Graphic_ColorBMP_HBitmap(x:integer; y:integer; nWidth:integer; nHeight:integer;
              rotate:integer; id_name:AnsiString; hbm:HBITMAP):integer;
              stdcall;external 'WINPPLB.DLL';
    function  B_Get_Pcx(x:integer; y:integer; filename:AnsiString):integer;stdcall;
              external 'WINPPLB.DLL';
    function  B_Initial_Setting(Type_Renamed:integer; Source:AnsiString):integer;stdcall;
              external 'WINPPLB.DLL';
    function  B_WriteData(IsImmediate:integer; pbuf:AnsiString; length:integer):integer;stdcall;
              external 'WINPPLB.DLL';
    function  B_ReadData(pbuf:PAnsiChar; length:integer; dwTimeoutms:integer):integer;stdcall;
              external 'WINPPLB.DLL';
    function  B_Load_Pcx(x:integer; y:integer; pcxname:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Open_ChineseFont(path:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Print_Form(labset:integer; copies:integer; form_out:AnsiString; var_Renamed:AnsiString)
              :integer;stdcall;external 'WINPPLB.DLL';
    function  B_Print_MCopy(labset:integer; copies:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Print_Out(labset:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Prn_Barcode(x:integer; y:integer; ori:integer; type_Renamed:AnsiString; narrow:integer;
              width:integer; height:integer; human:char; data:AnsiString):integer;stdcall;
              external 'WINPPLB.DLL';
    Procedure B_Prn_Configuration();stdcall;external 'WINPPLB.DLL';
    function  B_Prn_Text(x:integer; y:integer; ori:integer; font:integer; hor_factor:integer;
              ver_factor:integer; mode:char; data:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Prn_Text_Chinese(x:integer; y:integer; fonttype:integer; id_name:AnsiString; data:AnsiString)
              :integer;stdcall;external 'WINPPLB.DLL';
    function  B_Prn_Text_TrueType(x:integer; y:integer; FSize:integer; FType:AnsiString; Fspin:integer;
              FWeight:integer; FItalic:integer; FUnline:integer; FStrikeOut:integer; id_name:AnsiString;
              data:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Prn_Text_TrueType_W(x:integer; y:integer; FHeight:integer; FWidth:integer;
              FType:AnsiString; Fspin:integer; FWeight:integer; FItalic:integer; FUnline:integer;
              FStrikeOut:integer; id_name:AnsiString; data:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Select_Option(option:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Select_Option2(option:integer; p:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Select_Symbol(num_bit:integer; symbol:integer; country:integer):integer;stdcall;
              external 'WINPPLB.DLL';
    function  B_Select_Symbol2(num_bit:integer; csymbol:AnsiString; country:integer):integer;stdcall;
              external 'WINPPLB.DLL';
    function  B_Set_Backfeed(option:char):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_Backfeed_Offset(offset:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_CutPeel_Offset(offset:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_BMPSave(nSave:integer; strBMPFName:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_Darkness(darkness:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_DebugDialog(nEnable:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_Direction(direction:char):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_Form(formfile:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_Labgap(lablength:integer; gaplength:integer):integer;stdcall;
              external 'WINPPLB.DLL';
    function  B_Set_Labwidth(labwidth:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_Originpoint(hor:integer; ver:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_Prncomport(baud:integer; parity:char; data:integer; stop:integer):integer;
              stdcall;external 'WINPPLB.DLL';
    function  B_Set_Prncomport_PC(nBaudRate:integer; nByteSize:integer; nParity:integer;
              nStopBits:integer; nDsr:integer; nCts:integer; nXonXoff:integer):integer;stdcall;
              external 'WINPPLB.DLL';
    function  B_Set_Speed(speed:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_ProcessDlg(nShow:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_ErrorDlg(nShow:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_GetUSBBufferLen():integer;stdcall;external 'WINPPLB.DLL';
    function  B_EnumUSB(buf:PAnsiChar):integer;stdcall;external 'WINPPLB.DLL';
    function  B_CreateUSBPort(nPort:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_ResetPrinter():integer;stdcall;external 'WINPPLB.DLL';
    function  B_GetPrinterResponse(buf:AnsiString; nMax:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_TFeedMode(nMode:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_TFeedTest():integer;stdcall;external 'WINPPLB.DLL';
    function  B_CreatePort(nPortType:integer; nPort:integer; filename:AnsiString):integer;stdcall;
              external 'WINPPLB.DLL';
    function  B_Execute_Form(form_out:AnsiString; var_Renamed:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Bar2d_QR(x:integer; y:integer; model:integer; scl:integer; err:char; dinput:char;
              c:integer; d:integer; p:integer; data:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_GetNetPrinterBufferLen():integer;stdcall;external 'WINPPLB.DLL';
    function  B_EnumNetPrinter(buf:PAnsiChar):integer;stdcall;external 'WINPPLB.DLL';
    function  B_CreateNetPort(nPort:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Prn_Text_TrueType_Uni(x:integer; y:integer; FSize:integer; FType:AnsiString; Fspin:integer;
              FWeight:integer; FItalic:integer; FUnline:integer; FStrikeOut:integer; id_name:AnsiString;
              data:PWideChar; format:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Prn_Text_TrueType_UniB(x:integer; y:integer; FSize:integer; FType:AnsiString; Fspin:integer;
              FWeight:integer; FItalic:integer; FUnline:integer; FStrikeOut:integer; id_name:AnsiString;
              data:PWideChar; format:integer):integer;stdcall;external 'WINPPLB.DLL';
    function  B_GetUSBDeviceInfo(nPort:integer; pDeviceName:PAnsiChar; pDeviceNameLen:PInteger;
              pDevicePath:PAnsiChar; pDevicePathLen:PInteger):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Set_EncryptionKey(encryptionKey:AnsiString):integer;stdcall;external 'WINPPLB.DLL';
    function  B_Check_EncryptionKey(decodeKey:AnsiString; encryptionKey:AnsiString;
              dwTimeoutms:integer):integer;stdcall;external 'WINPPLB.DLL';

procedure TForm1.Button1Click(Sender: TObject);
var
     ret,nLen,sw : integer;
     pbuf : array[0..127] of AnsiChar;
     ver : PAnsiChar;
     strmsg : string;
     len1,len2 : integer;
     buf1,buf2 : AnsiString;
     buff1 : array[0..127] of WideChar;
     himage : HBITMAP;
begin
     // Test code start
     // open port.
     // dll version.
     ver := B_Get_DLL_Version(0);
     
     // search port.
     nLen := B_GetUSBBufferLen() + 1;
     strmsg := 'DLL ' + ver + dpCrLf;   // only sample coding, not smart.
     If nLen > 1 then
     begin
         len1 := 128;
         len2 := 128;
         SetLength(buf1,len1);
         SetLength(buf2,len2);
         B_EnumUSB(pbuf);
         B_GetUSBDeviceInfo(1, PAnsiChar(buf1), @len1, PAnsiChar(buf2), @len2);
         sw := 1;
         If 0 < sw then
                ret := B_CreatePrn(12, PAnsiChar(buf2)) // open usb.
         else
                ret := B_CreateUSBPort(1); // must call B_GetUSBBufferLen() function fisrt.
         If 0 < ret then
             strmsg := strmsg + 'Open USB fail!'
         else
         begin
             SetLength(buf1,len1);
             SetLength(buf2,len2);
             strmsg := strmsg + 'Open USB:' + dpCrLf + 'Device name: ';
             strmsg := strmsg + buf1;
             strmsg := strmsg + dpCrLf + 'Device path: ';
             strmsg := strmsg + buf2;
             //sw := 2;
             If 2 = sw then
             begin
                //Immediate Error Report.
                B_WriteData(1, '^ee'+chr(13)+chr(10), 5);   //^ee
                ret := B_ReadData(pbuf, 4, 1000);
             end;
         end;
     end
     else
     begin
         CreateDirectory(pchar(szSavePath), nil);
         ret := B_CreatePrn(0, szSaveFile);   // open file.
         strmsg := strmsg + 'Open ' + szSaveFile;
         if 0 < ret then
             strmsg := strmsg + ' file fail!'
         else
             strmsg := strmsg + ' file succeed!';
     end;
     ShowMessage(strmsg);
     If 0 < ret then
         exit;

     // sample setting.
     B_Set_DebugDialog(1);
     B_Set_Originpoint(0, 0);
     B_Select_Option(2);
     B_Set_Darkness(8);
     B_Del_Pcx('*');   // delete all picture.
     B_WriteData(0, sznop2, StrLen(PAnsiChar(sznop2)));
     B_WriteData(1, sznop1, StrLen(PAnsiChar(sznop1)));

     // draw box.
     B_Draw_Box(20, 20, 4, 760, 560);
     B_Draw_Line('O', 400, 20, 4, 540);
     
     // print text, true type text.
     B_Prn_Text(30, 40, 0, 2, 1, 1, 'N', 'PPLB Lib Example');
     B_Prn_Text_TrueType(30, 100, 30, 'Arial', 1, 400, 0, 0, 0, 'AA', 'TrueType Font');   // save in printer.
     B_Prn_Text_TrueType_W(30, 160, 20, 20, 'Times New Roman', 1, 400, 0, 0, 0, 'AB', 'TT_W: 多字元測試');

     StringToWideChar('TT_Uni: 多字元測試', buff1, 128);  // Converts double-byte characters to UNICODE(wide characters).
     buff1[13] := #$0000;   // null.
     B_Prn_Text_TrueType_Uni(30, 220, 30, 'Times New Roman', 1, 400, 0, 0, 0, 'AC', buff1, 1);   // UTF-16

     buff1[0] := #$FEFF;   // UTF-16.
     StringToWideChar('TT_UniB: 多字元測試', buff1+1, 128);  // Converts double-byte characters to UNICODE(wide characters).
     buff1[15] := #$0000;   // null.
     B_Prn_Text_TrueType_UniB(30, 280, 30, 'Times New Roman', 1, 400, 0, 0, 0, 'AD', buff1, 0);   // Byte Order Mark.

     // barcode.
     B_Prn_Barcode(420, 100, 0, '3', 2, 3, 40, 'B', '1234<+1>');   // have a counter.
     B_Bar2d_QR(420, 200, 1, 3, 'M', 'A', 0, 0, 0, 'QR CODE');

     // picture.
     B_Get_Graphic_ColorBMP(420, 280, 'bb.bmp');// Color bmp file.
     B_Get_Graphic_ColorBMPEx(420, 320, 200, 150, 2, 'bb1', 'bb.bmp');//180 angle.
     himage := LoadImage(0, 'bb.bmp', IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
     B_Get_Graphic_ColorBMP_HBitmap(630, 280, 250, 80, 1, 'bb2', himage);//90 angle.
     If 0 <> himage then
         DeleteObject(himage);

     // output.
     B_Print_Out(2);   // copy 2.
     
     // close port.
     B_ClosePrn();
end;

end.
