参考:
作者?: ycguser
标题?: ActiveX开发历程--控件数据的序列化
关键字:
分类?: 开发技巧
密级?: 公开
(评分: , 回复: 0, 阅读: 149) »»
刚开发开发此控件时,首先是想到此控件的数据要序列化到WORD文档中。
在建立工程时,我采用了ActiveXForm. 我在ActivexForm中加入一个USERNAME(String型)的属性后,将控件放到WORD中,发现USERNAME无法序列化到WORD文档中。这时我发现比较难办。如果这个基本的功能实现不了,那个这个控件根本无法开发下去。于是我查看了基类:TActiveXControl ,发现它已经实现了好几个序列化的接口。经过一天的调试跟踪,也没有搞明白它序列化方面的原理。现实的结果是在WORD中存不住数据。根据C++经验,只有自己去实现序列化的接口,但经过对系统支持的几个序列化接口试验以后,才发现只有实现IPersistStorage接口在WORD中才能序列化数据。
下面是是IPersistStorage实现的代码:
TCanvasForm = class(TActiveForm, ICanvasForm,IPersistStorage)
private
{ Private declarations }
FUserName:String; //用户名称
FDataTime:string; //取得笔迹数据的时间
FWriteTime:Integer; //返回的时间
FIndata
leVariant; //笔迹数据变量 array of byte 的变量
FisScale:boolean; //记录图片是否比image大
FLastTime:Integer;
bdirty:Boolean; //是否保存标识
public
///////IPersistStorage
function IsDirty: HResult; stdcall;
function InitNew(const stg: IStorage): HResult; stdcall;
function Load(const stg: IStorage): HResult; stdcall;
function Save(const stgSave: IStorage; fSameAsLoad: BOOL): HResult;
stdcall;
function SaveCompleted(const stgNew: IStorage): HResult; stdcall;
function HandsOffStorage: HResult; stdcall;
function GetClassID(out classID: TCLSID): HResult; stdcall;
end;
////////////////////IPersistStoge
function TCanvasForm.IsDirty: HResult; stdcall;
begin
//if bdirty then
// Result := S_False
//else
result:=S_OK;
end;
function TCanvasForm.InitNew(const stg: IStorage): HResult; stdcall;
begin
Result := S_OK;
end;
function TCanvasForm.Load(const stg: IStorage): HResult; stdcall;
var
stmData:IStream;
OS:TOleStream;
read:TReader;
elementcount:integer;
bty:array of byte;
begin
if SUCCEEDED(stg.OpenStream('PropData',nil, STGM_READWRITE or STGM_SHARE_EXCLUSIVE,0,stmData)) then
begin
OS:=TOleStream.Create(stmData);
try
read:=Treader.Create(oS,4096);
try
FUserName:=read.ReadWideString;
FDataTime:=read.ReadWideString ;
self.Height :=trunc(read.ReadSingle) ;
self.Width :=trunc(read.ReadSingle) ;
self.FLastTime:=trunc(read.ReadSingle );
elementcount:=trunc(read.ReadSingle );
finally
read.Free;
end;
finally
OS.Free;
end;
end;
Result := S_OK;
end;
function TCanvasForm.Save(const stgSave: IStorage; fSameAsLoad: BOOL): HResult;
stdcall;
var
stmData:IStream;
OS:TOleStream;
write:TWriter;
arrayElement:integer;
p
ointer;
begin
// if not SUCCEEDED(stgSave.OpenStream('ss',nil, STGM_READWRITE or STGM_SHARE_EXCLUSIVE,0,stmData)) then
OleCheck(stgSave.CreateStream('PropData',STGM_CREATE or STGM_READWRITE or STGM_SHARE_EXCLUSIVE,0,0,stmData));
OS:=TOleStream.Create(stmData);
try
write:=TWriter.Create(OS,4096);
try
write.WriteWideString(FUserName);
write.WriteWideString(FDataTime);
write.WriteSingle(Height);
Write.WriteSingle(Width);
write.WriteSingle(FLastTime);
///////////// 开始写变体数据
if not VarIsEmpty(self.FIndata) then
begin
arrayElement:=VarArrayHighBound(Findata,1)+1;
write.WriteSingle(arrayElement);
p := VarArrayLock(Findata);
try
write.Write(p^, arrayElement);
finally
VarArrayUnlock(Findata);
end;
end
else
begin
arrayElement:=-1;
write.WriteSingle(arrayElement);
end;
finally
write.Free;
end;
finally
OS.Free;
end;
bdirty:=true;
Result := S_OK;
end;
function TCanvasForm.SaveCompleted(const stgNew: IStorage): HResult; stdcall;
begin
Result := S_OK;
end;
function TCanvasForm.HandsOffStorage: HResult; stdcall;
begin
Result := S_OK;
end;
function TCanvasForm.GetClassID(out classID: TCLSID): HResult; stdcall;
begin
classID:=Class_CanvasForm;
Result := S_OK;
end;