unit TSPL_Delphi;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    ComboBox1: TComboBox;
    Button1: TButton;
    Button2: TButton;
    Edit2: TEdit;
    PageControl1: TPageControl;
    QRCode: TTabSheet;
    BitMap: TTabSheet;
    Box: TTabSheet;
    Direction: TTabSheet;
    TEXT: TTabSheet;
    Label5: TLabel;
    Edit5: TEdit;
    Label6: TLabel;
    Edit6: TEdit;
    Label7: TLabel;
    Edit7: TEdit;
    Label8: TLabel;
    Edit8: TEdit;
    Label9: TLabel;
    Edit9: TEdit;
    Label10: TLabel;
    Edit10: TEdit;
    Label11: TLabel;
    btnTEXT: TButton;
    Label23: TLabel;
    Edit23: TEdit;
    Label24: TLabel;
    Edit24: TEdit;
    Label25: TLabel;
    Edit25: TEdit;
    Label26: TLabel;
    Label27: TLabel;
    Label28: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    Edit26: TEdit;
    Edit27: TEdit;
    Edit28: TEdit;
    Edit29: TEdit;
    Edit30: TEdit;
    btnQRCode: TButton;
    Memo3: TMemo;
    Label34: TLabel;
    Label35: TLabel;
    Label36: TLabel;
    Label39: TLabel;
    btnBitMap: TButton;
    Edit11: TEdit;
    Edit20: TEdit;
    Edit31: TEdit;
    Label40: TLabel;
    Label41: TLabel;
    Label42: TLabel;
    Label43: TLabel;
    Label44: TLabel;
    btnBox: TButton;
    Edit36: TEdit;
    Edit37: TEdit;
    Edit38: TEdit;
    Edit39: TEdit;
    Edit40: TEdit;
    Label47: TLabel;
    Edit43: TEdit;
    Button13: TButton;
    Button15: TButton;
    Button16: TButton;
    Memo1: TMemo;
    BarCode: TTabSheet;
    Label3: TLabel;
    Label4: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit14: TEdit;
    Edit15: TEdit;
    Edit16: TEdit;
    Edit17: TEdit;
    Edit18: TEdit;
    Edit19: TEdit;
    btnBarCode: TButton;
    Memo2: TMemo;
    Memo4: TMemo;
    Label12: TLabel;
    Edit12: TEdit;
    procedure ComboBox1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button15Click(Sender: TObject);
    procedure Button16Click(Sender: TObject);
    procedure btnTEXTClick(Sender: TObject);
    procedure btnBarCodeClick(Sender: TObject);
    procedure btnQRCodeClick(Sender: TObject);
    procedure btnBitMapClick(Sender: TObject);
    procedure btnBoxClick(Sender: TObject);
    procedure Button13Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

function UnicodeToAnsi(Str: String): AnsiString;
var
  Len: integer;
begin
  Len := Length(Str)* 2+ 1; // one for #0
  SetLength(Result, Len);
  Len := WideCharToMultiByte(CP_ACP, 0, PWideChar(Str),-1,
    PAnsiChar(Result), Len, nil, nil);
  SetLength(Result, Len);
end;


function AnsiToUnicode(Str: AnsiString): String;
var
  Len: integer;
begin
  Len := Length(Str)+ 1;
  SetLength(Result, Len);
  Len := MultiByteToWideChar(CP_ACP, 0, PAnsiChar(Str),-1,
    PWideChar(Result), Len);
  SetLength(Result, Len); // end is #0
end;
{$R *.dfm}

procedure TForm1.btnBoxClick(Sender: TObject);
var
  x: Integer;
  y: Integer;
  x_end: Integer;
  y_end: Integer;
  thick: Integer;
begin
  if (Edit36.TEXT <> '') then
  begin
    if (Edit37.TEXT <> '') then
    begin
      if (Edit38.TEXT <> '') then
      begin
        if (Edit39.TEXT <> '') then
        begin
          if (Edit40.TEXT <> '') then
          begin
                x := StrToInt(Edit36.TEXT);
                y := StrToInt(Edit37.TEXT);
                x_end := StrToInt(Edit38.TEXT);
                y_end := StrToInt(Edit39.TEXT);
                thick := StrToInt(Edit40.TEXT);
                DemClearbuff();
                DemSetup(100,80,2,6,1,2,0);
                DemBox(x, y, x_end, y_end, thick);
                DemPrint(1, 1);
                // MessageDlg(Format('Download image failed,Error Code:%s!',[data]),mtWarning,[mbOK],0);
                // MessageDlg('ok!',mtWarning,[mbOK],0);
              end
          else
          begin
            MessageDlg('边框的宽度不能为空!', mtWarning, [mbOK], 0);
          end;
        end
        else
        begin
          MessageDlg('纵坐标结束位置不能为空!', mtWarning, [mbOK], 0);
        end;
      end
      else
      begin
        MessageDlg('横坐标结束位置不能为空!', mtWarning, [mbOK], 0);
      end;
    end
    else
    begin
      MessageDlg('纵坐标起始位置不能为空!', mtWarning, [mbOK], 0);
    end;
  end
  else
  begin
    MessageDlg('横坐标起始位置不能为空!', mtWarning, [mbOK], 0);
  end;
end;

procedure TForm1.Button13Click(Sender: TObject);
var
  Direction: Integer;
begin
  if Edit43.TEXT <> '' then
  begin
    Direction := StrToInt(Edit43.TEXT);

    DemDirection(Direction);
  end
  else
  begin
    MessageDlg('打印方向不能为空!', mtWarning, [mbOK], 0);
  end;

end;

procedure TForm1.Button15Click(Sender: TObject);
begin
  DemSelftest();
end;

procedure TForm1.Button16Click(Sender: TObject);
begin
  DemFormfeed();
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  errNo: Integer;
  modelSetting: ansistring;
  portSetting: ansistring;
begin
  errNo := E_SUCCESS;
  modelSetting := Edit1.TEXT;
  portSetting := Edit2.TEXT;
  if (DemPrinterCreator(modelSetting)) then
  begin
    if (errNo <> DemPortOpen(portSetting)) then
    begin
      MessageDlg('Port Open Failed!', mtWarning, [mbOK], 0);
    end
   else
   begin
  MessageDlg('Create success!!', mtWarning, [mbOK], 0);
   end;
  end
  else
  begin
    MessageDlg('Creator Model Failed!', mtWarning, [mbOK], 0);
  end ;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  DemPortClose();
  MessageDlg('stop success!!', mtWarning, [mbOK], 0);
end;

procedure TForm1.btnTEXTClick(Sender: TObject);
var
  x: Integer;
  result: Integer;
  y: Integer;
  font: Integer;
  rotation: Integer;
  x_mul: Integer;
  y_mul: Integer;
  alignment: Integer;
  data: ansistring;
  len: Integer;
begin
  if (Trim(Edit5.TEXT) <> '') then
  begin
    if (Trim(Edit6.TEXT) <> '') then
    begin
      if (Trim(Edit7.TEXT) <> '') then
      begin
        if (Trim(Edit8.TEXT) <> '') then
        begin
          if (Trim(Edit9.TEXT) <> '') then
          begin
            if (Trim(Edit10.TEXT) <> '') then
            begin
            if (Trim(Edit12.TEXT) <> '') then
            begin
              if (Trim(Memo1.Lines.TEXT) <> '') then
                  begin
                    x := StrToInt(Edit5.TEXT);
                    y := StrToInt(Edit6.TEXT);
                    font := StrToInt(Edit7.TEXT);
                    rotation := StrToInt(Edit8.TEXT);
                    x_mul := StrToInt(Edit9.TEXT);
                    y_mul := StrToInt(Edit10.TEXT);
                    alignment := StrToInt(Edit12.TEXT);
                    data:=UnicodeToAnsi(Memo1.Lines.Text);
                    DemClearbuff();
                    DemSetup(100,80,2,6,1,2,0);
                    DemText(x, y, font, rotation, x_mul, y_mul,alignment,data);
                    DemPrint(1, 1);
                    // MessageDlg(Format('Download image failed,Error Code:%s!',[data]),mtWarning,[mbOK],0);
                    // MessageDlg('ok!',mtWarning,[mbOK],0);
                  end
                  else
                  begin
                    MessageDlg('文本内容不能为空!', mtWarning, [mbOK], 0);
                  end;
            end
            else
                  begin
                    MessageDlg('对齐方式不能为空!', mtWarning, [mbOK], 0);
                  end;
            end
            else
            begin
              MessageDlg('垂直放大不能为空!', mtWarning, [mbOK], 0);
            end;
          end
          else
          begin
            MessageDlg('水平放大不能为空!', mtWarning, [mbOK], 0);
          end;
        end
        else
        begin
          MessageDlg('旋转选择不能为空!', mtWarning, [mbOK], 0);
        end;
      end
      else
      begin
        MessageDlg('字体不能为空!', mtWarning, [mbOK], 0);
      end;
    end
    else
    begin
      MessageDlg('纵坐标起始位置不能为空!', mtWarning, [mbOK], 0);
    end;
  end
  else
  begin
    MessageDlg('横坐标起始位置不能为空!', mtWarning, [mbOK], 0);
  end;
end;

procedure TForm1.btnBarCodeClick(Sender: TObject);
var
  x: Integer;
  y: Integer;
    result: Integer ;
  code_type: Integer;
  height: Integer;
  readable: Integer;
  rotation: Integer;
  narrow: Integer;
  wide: Integer;
  data: ansistring;
begin
  if (Trim(Edit3.TEXT) <> '' )then
  begin
    if (Trim(Edit4.TEXT) <> '' )then
    begin
      if (Trim(Edit14.TEXT) <> '') then
      begin
        if (Trim(Edit15.TEXT) <> '') then
        begin
          if (Trim(Edit16.TEXT) <> '') then
          begin
            if (Trim(Edit17.TEXT) <> '' )then
            begin
              if (Trim(Edit18.TEXT) <> '') then
              begin
                if (Trim(Edit19.TEXT) <> '' )then
                begin
                   if (Trim(Memo2.TEXT) <> '') then
                      begin
                        x := StrToInt(Edit3.TEXT);
                        y := StrToInt(Edit4.TEXT);
                        code_type := StrToInt(Edit14.TEXT);
                        height := StrToInt(Edit15.TEXT);
                        readable := StrToInt(Edit16.TEXT);
                        rotation := StrToInt(Edit17.TEXT);
                        narrow := StrToInt(Edit18.TEXT);
                        wide := StrToInt(Edit19.TEXT);
                        data := UnicodeToAnsi(Memo2.Lines.TEXT);
                        DemClearbuff();
                        DemSetup(100,80,2,6,1,2,0);
                        DemBarcode(x, y, code_type, height, readable,rotation, narrow, wide, data);
                       DemPrint(1, 1);
                        // MessageDlg('ok!',mtWarning,[mbOK],0);
                      end
                      else
                      begin
                        MessageDlg('文本内容不能为空!', mtWarning, [mbOK], 0);
                      end;

                    end
                else
                begin
                  MessageDlg('宽条形宽不能为空!', mtWarning, [mbOK], 0);
                end;
              end
              else
              begin
                MessageDlg('窄条形宽不能为空!', mtWarning, [mbOK], 0);
              end;
            end
            else
            begin
              MessageDlg('旋转选择不能为空!', mtWarning, [mbOK], 0);
            end;
          end
          else
          begin
            MessageDlg('代码可读不能为空!', mtWarning, [mbOK], 0);
          end;
        end
        else
        begin
          MessageDlg('条形码高不能为空!', mtWarning, [mbOK], 0);
        end;
      end
      else
      begin
        MessageDlg('条形码类型不能为空!', mtWarning, [mbOK], 0);
      end;
    end
    else
    begin
      MessageDlg('纵坐标起始位置不能为空!', mtWarning, [mbOK], 0);
    end;
  end
  else
  begin
    MessageDlg('横坐标起始位置不能为空!', mtWarning, [mbOK], 0);
  end;

end;

procedure TForm1.btnQRCodeClick(Sender: TObject);
var
  x,result: Integer;
  y: Integer;
  ecc_level: Integer;
  width: Integer;
  mode: Integer;
  rotation: Integer;
  narrow: Integer;
  model: Integer;
  mask: Integer;
  data: ansistring;
begin
  if Edit23.TEXT <> '' then
  begin
    if Edit24.TEXT <> '' then
    begin
      if Edit25.TEXT <> '' then
      begin
        if Edit26.TEXT <> '' then
        begin
          if Edit27.TEXT <> '' then
          begin
            if Edit28.TEXT <> '' then
            begin
              if Edit29.TEXT <> '' then
              begin
                if Edit30.TEXT <> '' then
                begin
                   if Memo3.Lines.TEXT <> '' then
                      begin
                        x := StrToInt(Edit23.TEXT);
                        y := StrToInt(Edit24.TEXT);
                        ecc_level := StrToInt(Edit25.TEXT);
                        width := StrToInt(Edit26.TEXT);
                        mode := StrToInt(Edit27.TEXT);
                        rotation := StrToInt(Edit28.TEXT);
                        model := StrToInt(Edit29.TEXT);
                        mask := StrToInt(Edit30.TEXT);
                       data := UnicodeToAnsi(Memo3.Lines.Text);
      DemClearbuff();
      DemSetup(100,80,2,6,1,2,0);
      DemQrcode(x, y, ecc_level, width, mode, rotation, model, mask, data);
      DemPrint(1, 1);
                      end
                      else
                      begin
                        MessageDlg('文本内容不能为空!', mtWarning, [mbOK], 0);
                      end;
                    end
                else
                begin
                  MessageDlg('model不能为空!', mtWarning, [mbOK], 0);
                  mask := 0;
                end;
              end

              else
              begin
                MessageDlg('mask不能为空!', mtWarning, [mbOK], 0);
                model := 7;
              end;
            end

            else
            begin
              MessageDlg('旋转选择不能为空!', mtWarning, [mbOK], 0);
            end;
          end

          else
          begin
            MessageDlg('模式选择不能为空!', mtWarning, [mbOK], 0);
          end;
        end

        else
        begin
          MessageDlg('二维码宽不能为空!', mtWarning, [mbOK], 0);
        end;
      end

      else
      begin
        MessageDlg('错误校验等级不能为空!', mtWarning, [mbOK], 0);
      end;
    end

    else
    begin
      MessageDlg('纵坐标起始位置不能为空!', mtWarning, [mbOK], 0);
    end;
  end

  else
  begin
    MessageDlg('横坐标起始位置不能为空!', mtWarning, [mbOK], 0);
  end;
end;

procedure TForm1.btnBitMapClick(Sender: TObject);
var
  x: Integer;
  y: Integer;
  // result: Integer;
  mode: Integer;
  sets: Integer;
  labels: Integer;
  data: ansiString;
begin
  if Edit11.TEXT <> '' then
  begin
    if Edit20.TEXT <> '' then
    begin
      if Edit31.TEXT <> '' then
      begin
        if Memo4.Lines.TEXT <> '' then
            begin
              x := StrToInt(Edit11.TEXT);
              y := StrToInt(Edit20.TEXT);
              mode := StrToInt(Edit31.TEXT);
              //data := Memo4.TEXT;
              data:=UnicodeToAnsi(Memo4.Lines.Text);
             DemClearbuff();
             DemSetup(100,80,2,6,1,2,0);
              DemBitmap(x, y, mode, data);
              DemPrint(1, 1);
            end
            else
            begin
              MessageDlg('图片路径不能为空!', mtWarning, [mbOK], 0);
            end;
          end
      else
      begin
        MessageDlg('打印模式不能为空!', mtWarning, [mbOK], 0);
      end;
    end
    else
    begin
      MessageDlg('纵坐标起始位置不能为空!', mtWarning, [mbOK], 0);
    end;
  end
  else
  begin
    MessageDlg('横坐标起始位置不能为空!', mtWarning, [mbOK], 0);
  end;
end;


procedure TForm1.ComboBox1Change(Sender: TObject);
var
  portType: string;
begin

  portType := ComboBox1.TEXT;

  if ('USB' = portType) THEN
  begin
    Edit2.TEXT := portType;
  end
  else if ('NET' = portType) then
  begin
    Edit2.TEXT := 'Net,192.168.1.1';
  end
end;

end.
