13,871
社区成员




__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Image1->Picture->LoadFromFile("D:\\ccrun\\123.bmp");
Edit1->Text = "测试数据abc好";
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String strFileName = "D:\\ccrun\\123.ccrun";
// 打开或创建目标文件
int nFileHandle;
if (FileExists(strFileName))
nFileHandle = FileOpen(strFileName, fmOpenWrite);
else
nFileHandle = FileCreate(strFileName);
// 定位到文件头
FileSeek(nFileHandle, 0x0, 0);
// 将Image中的位图存入流中
TMemoryStream *ms = new TMemoryStream;
Image1->Picture->Bitmap->SaveToStream(ms);
// 先将图像流的大小写到文件中
DWORD dw = ms->Size;
FileWrite(nFileHandle, &dw, sizeof(dw));
// 再将图像流写到文件中
FileWrite(nFileHandle, ms->Memory, ms->Size);
// 接着写入Edit1中的文本长度
dw = Edit1->Text.Length();
FileWrite(nFileHandle, &dw, sizeof(dw));
// 再将Edit中的文本写入文件
FileWrite(nFileHandle, Edit1->Text.c_str(), Edit1->Text.Length());
delete ms;
FileClose(nFileHandle);
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Image1->Picture->Assign(NULL);
Edit1->Clear();
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
String strFileName = "D:\\ccrun\\123.ccrun";
// 打开或创建目标文件
int nFileHandle;
if (FileExists(strFileName))
nFileHandle = FileOpen(strFileName, fmOpenRead);
else
{
ShowMessage("目标文件未找到.");
return;
}
// 定位到文件头
FileSeek(nFileHandle, 0x0, 0);
// 先读取图像流的大小
DWORD dw;
FileRead(nFileHandle, &dw, sizeof(dw));
// 根据图像流的大小,从文件中读取图像流
TMemoryStream *ms = new TMemoryStream;
byte *p = new byte[dw];
FileRead(nFileHandle, p, dw);
ms->Write(p, dw);
delete []p;
// 将图像流中的位图显示在Image上
ms->Position = 0;
Image1->Picture->Bitmap->LoadFromStream(ms);
// 接着读取文本的长度
FileRead(nFileHandle, &dw, sizeof(dw));
// 然后读取指定长度的内容到Edit中
char *str = new char[dw + 1];
FileRead(nFileHandle, str, dw);
str[dw] = 0x0;
Edit1->Text = str;
delete []str;
delete ms;
FileClose(nFileHandle);
}