步骤:
(1)根据用户在向导上的选择,动态生成窗体文件和单元文件的文本文件.至于窗体文件的格式,你只要打开delphi6以上版本的随便一个form的dfm文件参考一下其他窗体是怎么定义的,就很容易懂得如何定义窗体文件.
(2)用ObjectTextToResource把窗体文件转化成资源文件
(3)CreateCppModule生成窗体,窗体向导就实现了.
(4)向导的注册和toolsApi的使用参考别人给出的资料,delphi5/demo/expert下也有个例子.
给出QuickReport的几处关键代码实现,参考它,你的功能应该不难实现.
=====================================================================
窗体文件和单元文件格式定义如下
==================================================
===================
QuickReportUnitSource =
'unit %0:s;'#13#10 +
#13#10 +
'interface'#13#10 +
#13#10 +
'uses Windows, SysUtils, Messages, Classes, Graphics, Controls,'#13#10 +
' StdCtrls, ExtCtrls, Forms, QuickRpt, QRCtrls;'#13#10 +
#13#10 +
'type'#13#10 +
' T%1:s = class(TQuickRep)'#13#10 +
' private'#13#10 +
#13#10 +
' public'#13#10 +
#13#10 +
' end;'#13#10 +
#13#10 +
'var'#13#10 +
' %1:s: T%1:s;'#13#10 +
#13#10 +
'implementation'#13#10 +
#13#10 +
'{$R *.DFM}'#13#10 +
#13#10 +
'end.'#13#10;
QuickReportDfmSource = 'object %s: T%0:s end';
=====================================================================
生成窗体的代码
=====================================================================
procedure TNewQuickReport.Execute;
var
UnitIdent, Filename: string;
ReportName: string;
CodeStream, HdrStream, DFMStream: IStream;
DFMString, DFMVCLStream: TStream;
InCppBuilder: Boolean;
begin
if not ToolServices.GetNewModuleName(UnitIdent, FileName) then Exit;
ReportName := 'QuickReport' + Copy(UnitIdent, 5, 255);
InCppBuilder := HexDisplayPrefix <> '$';
if InCppBuilder then
begin
HdrStream := TIStreamAdapter.Create(TStringStream.Create(Format(QuickReportCppHeader,
[UnitIdent, ReportName])), soOwned);
CodeStream := TIStreamAdapter.Create(TStringStream.Create(Format(QuickReportCppSource,
[UnitIdent, ReportName])), soOwned);
end else
begin
HdrStream := nil;
CodeStream := TIStreamAdapter.Create(TStringStream.Create(Format(QuickReportUnitSource,
[UnitIdent, ReportName])), soOwned);
end;
DFMString := TStringStream.Create(Format(QuickReportDfmSource, [ReportName]));
try
DFMVCLStream := TMemoryStream.Create;
DFMStream := TIStreamAdapter.Create(DFMVCLStream, soOwned);
ObjectTextToResource(DFMString, DFMVCLStream);
DFMVCLStream.Position := 0;
ToolServices.CreateCppModule(FileName, ReportName, 'TQuickRep', '',
HdrStream, CodeStream, DFMStream, [cmAddToProject, cmShowSource, cmShowForm,
cmUnNamed, cmMarkModified]);
finally
DFMString.Free;
end;
end;