I have briefly introduced open tools api to you. Now we need to write a simple wizard to experience the charm of open tools api.
In DELPHI6, we do not recommend that you use the tiexpert class in the exptintf unit as the base class of our own wizard, but instead use tinterfacedobject as the base class, and implement one or more interfaces in the toolsapi unit.
To compile a wizard, you must implement the iotawizard interface in the toolsapi unit and the parent interface inotifier of iotawizard (although Delphi does not call any method in the inotifier interface ). If you want to write a menu wizard, you must implement methods in the iotawizard, inotifier, and iotamenuwizard interfaces. If you want to write a Form Wizard or Project Wizard, methods In the iotawizard, inotifier, and iotarepositorywizard interfaces must be implemented. (Two aliases iotaformwizard and iotaprojectwizard are defined for the iotarepositorywizard interface in the toolsapi unit, respectively ).
In DELPHI6, the Registration Method of wizard has changed accordingly. The Wizard implemented in package does not call the registerlibraryexpert method in the exptintf unit, but instead calls the registerpackgewizard method. If the compiled wizard is implemented in DLL, you do not need to call this method, but use the iotawizardservices interface in the toolsapi unit to register and remove the compiled wizard.
Unit simplewizard;
Interface
Uses toolsapi, dialogs;
Type
Tmsimplewizard = Class (tinterfacedobject, iotanotifier, iotawizard)
Public
... {Iotanotifier}
Procedure aftersave;
Procedure beforesave;
Procedure destroyed;
Procedure modified;
... {Iotawizard}
Function getidstring: string;
Function getname: string;
Function getstate: twizardstate;
Procedure execute;
End;
Implementation
Function tmsimplewizard. aftersave;
Begin
// Do nothing
End;
Function tmsimplewizard. beforesave;
Begin
// Do nothing
End;
Procedure tmsimplewizard. destroyed;
Begin
// Do nothing
End;
Procedure tmsimplewizard. Execute;
Begin
Showmessage ('this wizard is trying to do something useful .');
End;
Function tmsimplewizard. getidstring: string;
Begin
Result: = 'chunsmao. Simple wizard ';
End;
Function tmsimplewizard. getname: string;
Begin
Result: = 'simple wizard ';
End;
Function tmsimplewizard. getstate: twizardstate;
Begin
Result: = [wsenabled];
End;
Procedure tmsimplewizard. modified;
Begin
// Do nothing
End;
To implement wizard in DLL, the first sharemem unit must be referenced in the uses clause. In addition, manually remove the wizard registered to IDE from the terminate method of DLL.
Library siamplewizarddll;
Uses sharemem, toolsapi, siamplewizard;
VaR
Index: integer =-1; // global variable. Save the index number added to the IDE by wizard.
Procedure terminate;
VaR
Services: iotawizardservices;
Begin
Services: = borlandideservices as iotawizardservices;
Services. removewizard (INDEX );
End;
Function initialize (const services: iborlandideservices; registerproc: twizardregisterproc; var terminateproc: twizardterminateproc): Boolean; stdcall;
VaR
Wizardservices: iotawizardservices;
Begin
// If you use VCL *. BPL, otherwise you need to save this interface variable
Borlandideservices: = services;
Wizardservices: = borlandideservices as iotawizardservices;
Wizardservices. addwizard (tsimplewizard. Create );
Terminateproc: = terminate;
Result: = true;
End;
Exports
Initialize as wizardentrypoint;
End.