読者です 読者をやめる 読者になる 読者になる

Haruyuki Mohri

毛利春幸のブログです。

Using The FireMonkey Save State(OnSaveState) , TFileStream and TBinaryReader, TBinaryWriter

The FireMonkey TForm OnSaveState events is implemented.

http://docwiki.embarcadero.com/RADStudio/Berlin/en/FireMonkey_Save_State
OnSaveState is a TForm of events. OnSaveState is very useful in mobile.
It is, because generated at the time of the background switching.

Useful this event even Windows.
f:id:mojeld:20160905110441j:plain

Use the OnSaveState, save the form content at the time of occurrence.

I used the TBinaryWriter and TFileStream.
Arranging two of TEdit to The Form.
f:id:mojeld:20160905112724j:plain

#include <memory>

void __fastcall TForm1::FormSaveState(TObject *Sender)
{
    std::unique_ptr<TFileStream> fs{std::make_unique<TFileStream>(FSaveFileName, fmOpenReadWrite + fmCreate)};
    std::unique_ptr<TBinaryWriter> bw{std::make_unique<TBinaryWriter>(fs.get(), TEncoding::Unicode, False)};
    try
    {
        for (int i = 0; i < this->ComponentCount; i++)
            bw->Write((static_cast<TEdit* >(this->Components[i]) )->Text);
    }
    catch(Exception& e)
    {
        ShowMessage(e.Message);
    }
}

This saved in the "FSaveFileName".
Location here....

#include <System.IOUtils.hpp>

#if defined(_PLAT_MSWINDOWS)
    FSaveFileName = ChangeFileExt(ParamStr(0), ".bin");
#else
    using tpath_ = System::Ioutils::TPath;
    FSaveFileName = tpath_::GetDocumentsPath() + tpath_::DirectorySeparatorChar + L"save.bin";
#endif

Create the "SaveFile" read method.

void __fastcall TForm1::LoadBinaly()
{
    std::unique_ptr<TFileStream> fs{std::make_unique<TFileStream>(FSaveFileName, fmOpenRead)};
    std::unique_ptr<TBinaryReader> br{std::make_unique<TBinaryReader>(fs.get(), TEncoding::Unicode, false)};
    try
    {
        for (int i = 0; i < this->ComponentCount; i++)
            (static_cast<TEdit* >(this->Components[i]) )->Text = br->ReadString();
    }
    catch(Exception* e)
    {
        ShowMessage(e->Message);
    }
}

I uses the C++11. "unique_ptr" and "using" etc..

Using FireMonkey SaveState in C++Builder - YouTube