1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| //---------------------------------------------------------------------------
void TFrmLst***::Print***()
{
TStringStream *ss = new TStringStream();
ss->LoadFromFile(ExtractFilePath(Application->ExeName)+"Modele***.rtf");
String chaine = ss->DataString;
delete ss;
for (int i = 0; i < ClientDataSet->Fields->Count ; i++)
{
TField *champ = ClientDataSet->Fields->Fields[i];
String tag = "<" + champ->FieldName + ">";
String TagValue;
if(champ->FieldName == "Memo***")
{
TagValue = Memo***AsString(champ);
String TagPlainTextValue = RTFToPlainText(TagValue);
String tagPlainText = "<" + champ->FieldName + ".PlainText" + ">";
chaine = StringReplace(chaine,tagPlainText,TagPlainTextValue,TReplaceFlags()<<rfIgnoreCase<<rfReplaceAll);
}
else
TagValue = champ->AsString;
chaine = StringReplace(chaine,tag,TagValue,TReplaceFlags()<<rfIgnoreCase<<rfReplaceAll);
}
ss = new TStringStream();
ss->WriteString(chaine);
char szTmpFile[ MAX_PATH ];
::GetTempPath( MAX_PATH, szTmpFile );
TFileName fichierRTF = IncludeTrailingPathDelimiter(szTmpFile) + "***_" + ClientDataSet->FieldByName("ID")->AsString + ".rtf";
DeleteFile(fichierRTF);
ss->SaveToFile(fichierRTF);
delete ss;
ShellExecuteW(NULL,L"print",fichierRTF.c_str(),NULL,NULL,SW_HIDE);
Sleep(2000);
int tentative = 0;
do
{
Sleep(100);
DeleteFile(fichierRTF);
tentative++;
}
while (FileExists(fichierRTF) || (tentative <= 20));
}
//---------------------------------------------------------------------------
String TFrmLst***::RTFToPlainText(String TagValue)
{
String szChaine;
TStringStream *ss = new TStringStream(TagValue);
TRichEdit *myRichEdit = new TRichEdit(this);
myRichEdit->Parent = this;
myRichEdit->SetBounds(-1,-1,0,0);
myRichEdit->Lines->LoadFromStream(ss);
szChaine = myRichEdit->Text;
delete ss;
delete myRichEdit;
return szChaine;
}
//---------------------------------------------------------------------------
String TFrmLst***::Memo***AsString(TField *Champ)
{
TStringStream *stream = new TStringStream();
TVarBytesField *VBF = ((TVarBytesField*)Champ);
BYTE *Txt = new BYTE[VBF->DataSize];
if(VBF->GetData(Txt))
{
// 2 octets pour la taille du binaire (WORD est sur 2 octet)
stream->Write(&Txt[2],(WORD&)Txt[0]);
stream->Position = 0;
}
String result = stream->DataString;
delete[] Txt;
delete stream;
return result;
} |
Partager