mark_c wrote:I can merge the two files "ImageList.dat" and "ListView.dat" to one?
Not with WriteComponentResFile(), no. You would need to implement your own custom file format to store what you need. For example:
Code: Select all
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "jpeg.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall WriteComponentResStream(TStream *Stream, TComponent *Instance)
{
TStream *MStream = new TMemoryStream;
try
{
MStream->WriteComponentRes(Instance->ClassName(), Instance);
__int64 Size = MStream->Size;
Stream->WriteBuffer(&Size, sizeof(Size));
Stream->CopyFrom(MStream, 0);
}
__finally
{
delete MStream;
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for(int i = 0; i < FileListBox1->Items->Count; ++i)
{
String FileName = IncludeTrailingPathDelimiter(FileListBox1->Directory) + FileListBox1->Items->Strings[i];
int ImageIndex;
Graphics::TBitmap *bmp = new Graphics::TBitmap;
try
{
TJPEGImage *jpeg = new TJPEGImage;
try
{
jpeg->LoadFromFile(FileName);
bmp->Assign(jpeg);
}
__finally
{
delete jpeg;
}
ImageIndex = ImageList1->Add(bmp, NULL);
}
__finally
{
delete bmp;
}
TListItem *pItem = ListView1->Items->Add();
pItem->Caption = FileName;
pItem->ImageIndex = ImageIndex;
}
Label1->Caption = ImageList1->Count;
TFileStream *FStream = new TFileStream("ImageListAndListView.dat", fmCreate);
try
{
WriteComponentResStream(FStream, ImageList1);
WriteComponentResStream(FStream, ListView1);
}
__finally
{
delete FStream;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ListView1->Items->Clear();
ImageList1->Clear();
Label1->Caption = 0;
}
//---------------------------------------------------------------------------
TComponent* __fastcall ReadComponentResStream(TStream *Stream, TComponent *Instance)
{
TComponent *Result;
TStream *MStream = new TMemoryStream;
try
{
__int64 Size = 0;
Stream->ReadBuffer(&Size, sizeof(Size));
if (Size != 0) MStream->CopyFrom(Stream, Size);
Result = MStream->ReadComponentRes(Instance);
}
__finally
{
delete MStream;
}
return Result;
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
ListView1->Items->Clear();
ImageList1->Clear();
Label1->Caption = 0;
TFileStream *FStream = new TFileStream("ImageListAndListView.dat", fmOpenRead | fmShareDenyWrite);
try
{
ReadComponentResStream(FStream, ImageList1);
ReadComponentResStream(FStream, ListView1);
}
__finally
{
delete FStream;
}
Label1->Caption = ImageList1->Count;
}
//---------------------------------------------------------------------------
That being said, I think DFM resources are self-delimiting, so it MIGHT be possible to do away with the TMemoryStream handling, but I'm not sure as I haven't tried it yet:
Code: Select all
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "jpeg.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for(int i = 0; i < FileListBox1->Items->Count; ++i)
{
String FileName = IncludeTrailingPathDelimiter(FileListBox1->Directory) + FileListBox1->Items->Strings[i];
int ImageIndex;
Graphics::TBitmap *bmp = new Graphics::TBitmap;
try
{
TJPEGImage *jpeg = new TJPEGImage;
try
{
jpeg->LoadFromFile(FileName);
bmp->Assign(jpeg);
}
__finally
{
delete jpeg;
}
ImageIndex = ImageList1->Add(bmp, NULL);
}
__finally
{
delete bmp;
}
TListItem *pItem = ListView1->Items->Add();
pItem->Caption = FileName;
pItem->ImageIndex = ImageIndex;
}
Label1->Caption = ImageList1->Count;
TFileStream *FStream = new TFileStream("ImageListAndListView.dat", fmCreate);
try
{
FStream->WriteComponentRes(ImageList1->ClassName(), ImageList1);
FStream->WriteComponentRes(ListView1->ClassName(), ListView1);
}
__finally
{
delete FStream;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ListView1->Items->Clear();
ImageList1->Clear();
Label1->Caption = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
ListView1->Items->Clear();
ImageList1->Clear();
Label1->Caption = 0;
TFileStream *FStream = new TFileStream("ImageListAndListView.dat", fmOpenRead | fmShareDenyWrite);
try
{
FStream->ReadComponentRes(ImageList1);
FStream->ReadComponentRes(ListView1);
}
__finally
{
delete FStream;
}
Label1->Caption = ImageList1->Count;
}
//---------------------------------------------------------------------------
Using the intermediate TMemoryStream objects allows the file to be explicit about which portions of its data belong to which DFM.