Lena wrote:1. Do I need in FormShow line:
ListView1->BeginUpdate();
ListView1->EndUpdate();
?
You should always use (Begin|End)Update() when making multiple changes to a control. It delays repainting until after you are done making changes.
Lena wrote:2. I use TBitmap * PictureFood = new TBitmap();
How to remove the memory leak because there is no delete?
There is no memory leak, because you are setting img->OwnsBitmap=true, so the TBitmap will be freed when the TListItemImage object is freed.
If you want to manage the lifetime of the TBitmap yourself, you have to set img->OwnsBitmap=false instead, and do not free the TBitmap until after the TListItemImage is freed.
Lena wrote:Maybe for the second question I need something like this global ad?:
std::vector<std::unique_ptr<Graphics::TBitmap> > PictureFood;
Only if you want to manage the TBitmap lifetimes yourself. For example:
- Code: Select all
private:
std::vector<std::unique_ptr<Graphics::TBitmap>> PictureFoods;
...
struct DelayUpdates
{
TControl *m_ctrl;
DelayUpdates(TControl *ctrl) : m_ctrl(ctrl) { m_ctrl->BeginUpdate(); }
~DelayUpdates() { m_ctrl->EndUpdate(); }
};
void __fastcall TForm1::FormShow(TObject *Sender)
{
String inifolder, imgfolder;
#if defined(__ANDROID__)
inifolder = System::Ioutils::TPath::GetSharedDownloadsPath();
imgfolder = System::Ioutils::TPath::GetSharedDownloadsPath();
#elif defined(_Windows)
inifolder = GetCurrentDir();//ExtractFilePath(ParamStr(0));
imgfolder = GetCurrentDir();//ExtractFilePath(ParamStr(0));
#endif
String path = System::Ioutils::TPath::Combine(inifolder, _D("dibocca.ini"));
if (!FileExists(path))
{
ShowMessage(L"Не найден файл меню dibocca.ini");
return;
}
std::unique_ptr<TIniFile> FileINI(new TIniFile(path));
std::unique_ptr<TStringList> AlliniSection(new TStringList());
FileINI->ReadSections(AlliniSection.get());
String text, footer, image, name, price;
DelayUpdates du(ListView1);
for (int i = 0; i < AlliniSection->Count; ++i)
{
text = FileINI->ReadString(AlliniSection->Strings[i], _D("text"), _D(""));
footer = FileINI->ReadString(AlliniSection->Strings[i], _D("footer"), _D("");
name = FileINI->ReadString(AlliniSection->Strings[i], _D("name"), _D("");
price = FileINI->ReadString(AlliniSection->Strings[i], _D("price"), _D("");
image = FileINI->ReadString(AlliniSection->Strings[i], _D("image"), _D("");
TListViewItem * item = ListView1->Items->Add();
item->Data[_D("FullName")] = TValue::From<String>(text);
item->Data[_D("FotterName")] = TValue::From<String>(footer);
item->Data[_D("TextCount")] = TValue::From<String>(_D("1"));
item->Data[_D("name")] = TValue::From<String>(name);
item->Data[_D("price")] = TValue::From<String>(price);
item->Data[_D("min")] = TValue::From<int>(0);
item->Data[_D("add")] = TValue::From<int>(1);
TListItemImage * img = static_cast<TListItemImage *>(item->Objects->FindDrawable(L"MyImage"));
if (img)
{
String path = System::Ioutils::TPath::Combine(imgfolder, image);
std::unique_ptr<Graphics::TBitmap> PictureFood(new Graphics::TBitmap(path));
if (!PictureFood->IsEmpty())
{
PictureFoods.push_back(std::move(PictureFood));
img->Bitmap = PictureFoods.back().get();
}
else
{
//image by default
img->Bitmap = ImageRAD->Bitmap;
}
img->OwnsBitmap = false;
}
}
}
}
Otherwise, get rid of the std::vector and do this instead:
- Code: Select all
std::unique_ptr<Graphics::TBitmap> PictureFood(new Graphics::TBitmap(path));
if (!PictureFood->IsEmpty())
{
img->Bitmap = PictureFood.get();
img->OwnsBitmap = true;
PictureFood.release();
}
else
{
//image by default
img->Bitmap = ImageRAD->Bitmap;
img->OwnsBitmap = false;
}