//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
String CurrentDir = GetCurrentDir();
typedef void (__stdcall *MAKE)(unsigned char* ucData, long nDataLen, char* szBmpFileName, long nClumn, long nErr, long nHLRatio,long nHeight);
typedef long (__stdcall *MIXTEXT)(char*  szSrcBmpFileName,char*  szDstBmpFileName,char*  szText,long lFontSize,long lTxtHeight,long lHmargin,long lVmargin,long lTxtCntOneLine);
typedef int  (__stdcall *SETCOLORDEPTH)(int nDepth);
typedef int  (__stdcall *GETCOLORDEPTH)();
MAKE make;
MIXTEXT  MixText;
SETCOLORDEPTH SetColorDepth;
GETCOLORDEPTH GetColorDepth;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    String Str = Memo1->Lines->Text.Trim();
    String Len = Edit1->Text.Trim();
    String err = Edit2->Text.Trim();
    String hLRatio=Edit3->Text.Trim();
    String height=Edit5->Text.Trim();
    String color=Edit4->Text.Trim();
    String szText=Edit7->Text.Trim();
    String txtCntOneLine=Edit6->Text.Trim();
    String fontSize=Edit8->Text.Trim();
    String txtHeight=Edit9->Text.Trim();
    String hmargin=Edit10->Text.Trim();
    String vmargin=Edit11->Text.Trim();

    HINSTANCE hInst;
    hInst = LoadLibrary("MakePDF417.dll");
    if(!hInst)
       ShowMessage("");
    make = (MAKE)GetProcAddress(hInst, "Make");
    MixText =(MIXTEXT)GetProcAddress(hInst, "MixText");
    SetColorDepth = (SETCOLORDEPTH)GetProcAddress(hInst, "SetColorDepth");
    GetColorDepth = (GETCOLORDEPTH)GetProcAddress(hInst, "GetColorDepth");
    long lRet = SetColorDepth(StrToInt(color));
    make(Str.c_str(), Str.Length(), "barcode.bmp", StrToInt(Len), StrToInt(err), StrToInt(hLRatio),StrToInt(height));
    lRet = GetColorDepth();
    lRet = MixText("barcode.bmp","barcode.bmp",szText.c_str(),StrToInt(fontSize),StrToInt(txtHeight),StrToInt(hmargin),StrToInt(vmargin),StrToInt(txtCntOneLine));
    FreeLibrary(hInst);
    Image1->Picture->LoadFromFile("barcode.bmp");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
     Memo1->Lines->Strings[0] = "2004062100000014ST0005";
     Memo1->Lines->Strings[1] = "机电式";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
     String Len = Edit1->Text.Trim();
     String err = Edit2->Text.Trim();
     String hLRatio=Edit3->Text.Trim();
     String height=Edit5->Text.Trim();
     String color=Edit4->Text.Trim();
     String szText=Edit7->Text.Trim();
     String txtCntOneLine=Edit6->Text.Trim();
     String fontSize=Edit8->Text.Trim();
     String txtHeight=Edit9->Text.Trim();
     String hmargin=Edit10->Text.Trim();
     String vmargin=Edit11->Text.Trim();
     OpenDialog1->InitialDir = CurrentDir + "\\bmp\\";
     OpenDialog1->Execute();

     TFileStream * OpenFile = new TFileStream(OpenDialog1->FileName, fmOpenRead);
     int MemSize;
     char * Buffer;
     MemSize = OpenFile->Size+1;
     Buffer = new char[MemSize];
     OpenFile->Read(Buffer, MemSize);
     delete Buffer;
     delete OpenFile;

     HINSTANCE hIn;
     hIn = LoadLibrary("MakePDF417.dll");
     if(!hIn)
        ShowMessage("未发现MakePDF417.dll");
     make = (MAKE)GetProcAddress(hIn, "Make");

     MixText =(MIXTEXT)GetProcAddress(hIn, "MixText");
     SetColorDepth = (SETCOLORDEPTH)GetProcAddress(hIn, "SetColorDepth");
     GetColorDepth = (GETCOLORDEPTH)GetProcAddress(hIn, "GetColorDepth");
     //make(Buffer, MemSize, "barcode.bmp", 3, 1, 4, 3);
     long lRet = SetColorDepth(StrToInt(color));
     make(Buffer, MemSize, "barcode.bmp", StrToInt(Len), StrToInt(err), StrToInt(hLRatio),StrToInt(height));
     lRet = GetColorDepth();
     lRet = MixText("barcode.bmp","barcode.bmp",szText.c_str(),StrToInt(fontSize),StrToInt(txtHeight),StrToInt(hmargin),StrToInt(vmargin),StrToInt(txtCntOneLine));
     FreeLibrary(hIn);
     Image1->Picture->LoadFromFile("barcode.bmp");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
     OpenDialog1->InitialDir = CurrentDir + "\\bmp\\";
     if(!(FileExists(CurrentDir+"\\bmp\\barcode.bmp\\")))
     {
         CreateDir(CurrentDir+"\\bmp\\");
         //Memo2->Lines->LoadFromFile(CurrentDir+"\\barcode.bmp");
         //TMemoryStream * MyStream = new TMemoryStream();
         //MyStream->LoadFromFile(CurrentDir+"\\barcode.bmp");
         //MyStream->SaveToFile(CurrentDir+"\\bmp\\barcode.bmp");
         //delete MyStream;
     }
     //if(OpenDialog1->Execute())
     //{
     //     if(Memo2->Lines->Count >0) Memo2->Clear();
     //     SaveDialog1->FileName = OpenDialog1->FileName;
     //}
     SaveDialog1->InitialDir = CurrentDir + "\\bmp\\";
     SaveDialog1->FileName = "barcode.bmp";
     SaveDialog1->Filter = "位图文件 (*.bmp)|*.bmp|所有文件 (*.*)|*.*||";
     SaveDialog1->Title = "另存为";
     if(SaveDialog1->Execute())
     {
          //Memo2->Lines->SaveToFile(SaveDialog1->FileName);
          //Memo2->Lines->SaveToFile(CurrentDir+"\\bmp\\barcode.bmp\\");
          //TMemoryStream * MyStream = new TMemoryStream();
          //MyStream->LoadFromFile(CurrentDir+"\\barcode.bmp");
          //MyStream->SaveToFile(CurrentDir+"\\bmp\\barcode.bmp");
          TFileStream * MyFile = new TFileStream(CurrentDir+"\\barcode.bmp", fmOpenRead);
          TFileStream * NewFile = new TFileStream(CurrentDir+"\\bmp\\barcode.bmp", fmCreate);
          NewFile->CopyFrom(MyFile, MyFile->Size);
          //MyStream->CopyFrom(CurrentDir+"\\barcode.bmp\\", 12);
          delete MyFile;
          delete NewFile;
     }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
     //QuickRep1->Preview();
}
//---------------------------------------------------------------------------


