//.h----------------------------------------------------
class TFormSetup : public TForm
{
......
public: // User declarations
__fastcall TFormSetup(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormSetup *FormSetup;
//---------------------------------------------------------------------------
class TMyStream : public TFileStream
{
private://
public:
TMyStream(const System::UnicodeString AFileName, System::Word Mode);
virtual int __fastcall Read( void *Buffer, int Count);
virtual int __fastcall Write( const void *Buffer, int Count);
virtual __int64 __fastcall Seek(const __int64 Offset, TSeekOrigin Origin);
virtual int __fastcall Seek( int Offset, System::Word Origin);
};
//---------------------------------------------------------------------------
#endif
//.cpp-----------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "cSetup.h"
#include <Zlib.hpp>
#include <stdio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormSetup *FormSetup;
//---------------------------------------------------------------------------
__fastcall TFormSetup::TFormSetup(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
TMyStream::TMyStream(const System::UnicodeString AFileName,System::Word Mode):TFileStream(AFileName, Mode)
{
FormSetup->PBar1->Position = 0;//错误指向这一行,如果不要这一行,错误会指向下面访问FormSetup的那一行
}
//---------------------------------------------------------------------------
int __fastcall TMyStream::Read(void *Buffer, int Count)
{
int n = TFileStream::Read(Buffer, Count);
FormSetup->PBar1->Position = FormSetup->PBar1->Position + n ;//这行也会出错
Application->ProcessMessages();
return n;
}
//---------------------------------------------------------------------------
int __fastcall TMyStream::Write(const void *Buffer, int Count)
{
int n = TFileStream::Write(Buffer, Count);
Application->ProcessMessages();
return n;
}
//---------------------------------------------------------------------------
__int64 __fastcall TMyStream::Seek(const __int64 Offset, TSeekOrigin Origin)
{
return TFileStream::Seek(Offset, Origin);
}
//---------------------------------------------------------------------------
int __fastcall TMyStream::Seek(int Offset, System::Word Origin)
{
return TFileStream::Seek(Offset, Origin);
}
......
void __fastcall TFormSetup::BtnPathSureClick(TObject *Sender)
{
EditBackPath->Text = DirLst1->Directory;
TMyStream *sf,*df;
String strSource = EditDir->Text+"\\mydtbase.accdb";
String strComp = EditBackPath->Text+"\\bk001.zzz";
try
{
sf = new TMyStream(strSource.t_str(),fmOpenRead);
PBar1->Max = sf->Size;
PBar1->Position = 0;
try
{
df = new TMyStream(strComp.t_str(),fmCreate);
ZCompressStream(sf,df,zcDefault);
delete df;
}
catch(Exception &e)
{
Application->MessageBoxA(L"无法写入文件",L"错误",MB_OK|MB_ICONERROR);
}
delete sf;
}
catch(Exception &e)
{
ShowMessage(e.Message);
}
}
另外这个窗体是动态创建并显示的,当我不用动态创建显示的时候,不会出现错误.
3 个解决方案
#1
建议你修改一下TMyStream类,添加一个TStatusBar对象, 然后在构造函数中,将FormSetup->PBar1传入.
#2
问题是解决了,可是为什么这样子就不行呢?
#3
TMyStream类的声明在哪里?
如果在动态创建窗体之前,那自然会出异常。
因为这样是先构建TMyStream类,后构建TFormSetup类
而在TMyStream类内调用的FormSetup,这时FormSetup还不存在呢,所以就出错了。
如果在动态创建窗体之前,那自然会出异常。
因为这样是先构建TMyStream类,后构建TFormSetup类
而在TMyStream类内调用的FormSetup,这时FormSetup还不存在呢,所以就出错了。
#1
建议你修改一下TMyStream类,添加一个TStatusBar对象, 然后在构造函数中,将FormSetup->PBar1传入.
#2
问题是解决了,可是为什么这样子就不行呢?
#3
TMyStream类的声明在哪里?
如果在动态创建窗体之前,那自然会出异常。
因为这样是先构建TMyStream类,后构建TFormSetup类
而在TMyStream类内调用的FormSetup,这时FormSetup还不存在呢,所以就出错了。
如果在动态创建窗体之前,那自然会出异常。
因为这样是先构建TMyStream类,后构建TFormSetup类
而在TMyStream类内调用的FormSetup,这时FormSetup还不存在呢,所以就出错了。