📄 65.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://bbs.tsinghua.edu.cn"><font face="黑体"><big><big>水木清华★</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center"> Delphi编程 (BM: strayli FlyingBoy) </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="454"> <p align="center">[<a href="index.htm">回到开始</a>][<a href="13.htm">上一层</a>][<a href="66.htm">下一篇</a>]
<hr><p align="left"><small>发信人: strayli (stray), 信区: Delphi <br>
标 题: How To Write Delphi Wizards(1) <br>
发信站: BBS 水木清华站 (Thu Nov 5 21:59:25 1998) WWW-POST <br>
<br>
How To Write Delphi Wizards <br>
------------------------------------------------------------------------------ <br>
-- <br>
Delphi and C++Builder are truly open development environments, in that they <br>
have interfaces to enable us to integrate our own tools and experts within <br>
their IDE. This article will focus on writing and integrating Wizards <br>
(previously called Experts) with Delphi. The resulting (32-bits) Wizards will <br>
be compatible with Delphi 2.0x, Delphi 3 and C++Builder. <br>
Delphi has four kinds of Wizards: Project Experts, Form Experts, Standard <br>
Experts and (32-bits only) AddIn Experts. The first two can be found in the <br>
Repository, Standard Experts can be found under the Help menu (like the <br>
Database Form Expert), while AddIn Experts have to provide their own <br>
menu-interface with the Delphi IDE (typicaly anywhere in the menu except for <br>
the Help Menu, which seems to be reserved for Standard Experts only). <br>
<br>
<br>
<br>
Project and Form Experts can be activated whenever you create a new Project <br>
or Form (just like Project and Form Templates). Standard and AddIn Experts <br>
are the other kind of Wizards that generally do not create a new project or <br>
form, but provide some kind of information, or only create a new file or <br>
unit. <br>
If you've ever tried an Wizard, you know what power and ease they can bring <br>
to you. The Project Expert develops an entire project for you based on your <br>
specific preferences (like for example the Application Wizard). The Form <br>
Experts develop custom forms that are added to your current project. The <br>
Database Form Expert, for example, generates a form that displays data from <br>
an external database. These example Wizards are not just external tools that <br>
can be started from Delphi, they actually communicate with Delphi and are an <br>
integrated part of the development environment. While this is not so strange <br>
for the existing Delphi Experts (after all, they were developed and added by <br>
the same team that developed Delphi in the first place, and we all know <br>
Delphi's IDE is written in Delphi), it sounds intriguing at least to know <br>
that we, too, can write a Delphi Wizard that is able to communicate with <br>
Delphi in the same way. Could we actually write an Wizard that also opens <br>
files in the IDE, that can be used to start a new project from scratch? Yes, alll this is possible, and more, as we will see <br>
shortly! <br>
<br>
1. TIExpert Interface <br>
The major reason why everybody thinks writing custom Wizards is difficult, is <br>
because they are not documented. Not in the manuals or on-line Help, that is <br>
because they are not documented. Not in the manuals or on-line Help, that is <br>
(they are documented in my book The Revolutionary Guide to Delphi 2 and in my <br>
column in The Delphi Magazine). If you take a look at the documentation and <br>
source code on your harddisk, you'll find some important files and even two <br>
example Wizards that are installed automatically by Delphi itself. The <br>
important example files can be found in the DOC, SOURCE\VCL or <br>
SOURCE\TOOLSAPI subdirectories, and the main files are EXPTINTF.PAS, <br>
TOOLINTF.PAS, VIRTINTF.PAS and SHAREMEM.PAS. The first one shows how to <br>
derive and register our own Wizard, while the second one shows how to use the <br>
tool-services of Delphi to make the integration with the IDE complete. <br>
<br>
In order to start working on a custom wizard, we have to take a look at the <br>
abstract base class definition TIExpert in EXPTINTF.PAS, which is as follows <br>
for the 32-bits versions of Delphi: <br>
<br>
<br>
Type <br>
TExpertStyle = (esStandard, esForm, esProject, esAddIn); <br>
TExpertState = set of (esEnabled, esChecked); <br>
<br>
TIExpert = class(TInterface) <br>
public <br>
{ Expert UI strings } <br>
function GetIDString: string; virtual; stdcall; abstract; <br>
function GetName: string; virtual; stdcall; abstract; <br>
function GetAuthor: string; virtual; stdcall; abstract; <br>
function GetStyle: TExpertStyle; virtual; stdcall; abstract; <br>
function GetMenuText: string; virtual; stdcall; abstract; <br>
function GetState: TExpertState; virtual; stdcall; abstract; <br>
function GetGlyph: HICON; virtual; stdcall; abstract; <br>
function GetComment: string; virtual; stdcall; abstract; <br>
function GetPage: string; virtual; stdcall; abstract; <br>
<br>
{ Launch the Expert } <br>
procedure Execute; virtual; stdcall; abstract; <br>
end; <br>
<br>
2. TGenericExpert: Hello, World! <br>
If we want to derive our own Wizard, say TGenericExpert, we have to derive it <br>
from the abstract base class TIExpert, which has seven or nine abstract <br>
member functions (GetStyle, GetName, GetComment, GetGlyph, GetState, <br>
GetIDString and GetMenuText, and for the 32-bits versions of Delphi also <br>
GetAuthor and GetPage) and one member procedure Execute. Since TIExpert is an <br>
abstract base class, we need to override every function we need for any <br>
particular Wizard. <br>
particular Wizard. <br>
<br>
<br>
unit Generic; <br>
interface <br>
uses <br>
Windows, ExptIntf; <br>
<br>
Type <br>
TGenericExpert = class(TIExpert) <br>
public <br>
{ Expert Style } <br>
function GetStyle: TExpertStyle; override; <br>
<br>
{ Expert Strings } <br>
function GetIDString: string; override; <br>
function GetName: string; override; <br>
function GetAuthor: string; override; <br>
function GetMenuText: string; override; <br>
function GetState: TExpertState; override; <br>
function GetGlyph: HICON; override; <br>
function GetComment: string; override; <br>
function GetPage: string; override; <br>
<br>
{ Expert Action } <br>
procedure Execute; override; <br>
end; <br>
<br>
procedure Register; <br>
<br>
implementation <br>
uses <br>
Dialogs; <br>
<br>
{ The implementation details of TGenericExpert will follow in the text } <br>
<br>
procedure Register; <br>
begin <br>
RegisterLibraryExpert(TGenericExpert.Create) <br>
end {Register}; <br>
end. <br>
<br>
Let's have a closer look at our generic Wizard from this listing. Since <br>
TIExpert is an abstract base class, we need to override every function we <br>
need for our TGenericExpert. First of all, we need to specify the style of <br>
of all, we need to specify the style of <br>
the Wizard with the GetStyle method that can return one of three (or four) <br>
possible values: esStandard to tell the IDE to treat the interface to this <br>
Wizard as a menu item on the Help menu, esForm to tell the IDE to treat this <br>
Wizard interface in a fashion similar to form templates, or esProject to tell <br>
the IDE to treat this interface in a fashion similar to project templates. <br>
For 32-bits Delphi Wizards only, we can also return esAddIn here, to indicate <br>
that this is a special klind of Wizard that handles all its own interfaceing <br>
to the IDE through the TIToolServices interface. For our TGenericExpert, a <br>
Standard type Wizard that only shows a MessageDlg to say hello to the world, <br>
we can use the esStandard style. <br>
<br>
function TGenericExpert.GetStyle: TExpertStyle; <br>
begin <br>
Result := esStandard <br>
end {GetStyle}; <br>
<br>
The GetIDString should be unique to all Wizards that could be installed. By <br>
convention, the format of the string is: CompanyName.ExpertFunction, like <br>
Borland.Expert or DrBob.GenericExpert. <br>
<br>
function TGenericExpert.GetIDString: String; <br>
begin <br>
begin <br>
Result := 'DrBob.TGenericExpert' <br>
end {GetIDString}; <br>
<br>
After we've set the style of the Wizard, all we need to do is fill the other <br>
options accordingly. The GetName must return a unique descriptive name <br>
identifying this Wizard, like 'Generic Wizard'. <br>
<br>
function TGenericExpert.GetName: String; <br>
begin <br>
Result := 'Generic Wizard' <br>
end {GetName}; <br>
<br>
If the style is esForm or esProject, then - for 32-bits versions of Delphi <br>
only - we need to return a valid name for the Author. In this case, the style <br>
is esStandard, so we can return an empty string instead. For an esForm or <br>
esProject style Wizard the name would be displayed in the Object Repository <br>
of the 32-bits versions of Delphi. <br>
<br>
{$IFDEF WIN32} <br>
function TGenericExpert.GetAuthor: String; <br>
begin <br>
Result := 'Bob Swart (aka Dr.Bob)' { although not needed for esStandard <br>
} <br>
end {GetAuthor}; <br>
{$ENDIF} <br>
<br>
If style is esForm or esProject then GetGlyph should return a handle to a <br>
bitmap (for Delphi 1) or icon (for Delphi 2.0x and 3) to be displayed in the <br>
form or project list boxes or dialogs. This bitmap should have a size of <br>
60x40 pixels in 16 colours. The icon should be 32x32 in 16 colours. Again, <br>
since the style is just esStandard for our TGenericExpert, we can return 0 <br>
here. We can even combine the 16- and 32-bit version of GetGlyph here (0 is a <br>
valid value to indicate that an icon or bitmap is empty). Note that if we <br>
return a 0 when a bitmap or icon is needed, Delphi will use the default <br>
image. <br>
<br>
{$IFDEF WIN32} <br>
function TGenericExpert.GetGlyph: HICON; <br>
{$ELSE} <br>
function TGenericExpert.GetGlyph: HBITMAP; <br>
{$ENDIF} <br>
begin <br>
Result := 0 { not needed for esStandard } <br>
end {GetGlyph}; <br>
end {GetGlyph}; <br>
<br>
If style is esForm or esProject then GetComment should return a 1 or 2 line <br>
sentence describing the function of this Wizard. Since the style is <br>
esStandard, we can return an empty string. <br>
<br>
function TGenericExpert.GetComment: String; <br>
begin <br>
Result := '' { not needed for esStandard } <br>
end {GetComment}; <br>
<br>
If style is esForm or esProject then - only for 32-bits versions of Delphi - <br>
using GetPage we can specify the name of the page in the Object Repository <br>
where to place our Wizard. If we don't specify a name here, then the Wizard <br>
just gets added to the Default Form or Project page. Since we're writing an <br>
esStandard Expert, we don't need to supply a page name, so we can return an <br>
empty string again. <br>
<br>
{$IFDEF WIN32} <br>
function TGenericExpert.GetPage: String; <br>
begin <br>
Result := '' { not needed for esStandard } <br>
end {GetPage}; <br>
{$ENDIF} <br>
<br>
If style is esStandard then GetMenuText should return the actual text to <br>
display for the menu item, like 'Generic Wizard'. Since this function is <br>
called each time the parent menu is pulled-down, it is even possible to <br>
provide context sensitive text. <br>
<br>
function TGenericExpert.GetMenuText: String; <br>
begin <br>
Result := '&Generic Wizard...' <br>
end {GetMenuText}; <br>
<br>
If the style is esStandard then GetState returning esChecked will cause the <br>
menu to display a checkmark. This function is called each time the Wizard is <br>
shown in a menu or listbox in order to determine how it should be displayed. <br>
We just leave it esEnabled for now. <br>
<br>
function TGenericExpert.GetState: TExpertState; <br>
begin <br>
Result := [esEnabled] <br>
end {GetState}; <br>
<br>
<br>
Finally, the Execute method is called whenever this Wizard is invoked via the <br>
menu, form gallery dialog, or project gallery dialog. Note that Execute is <br>
never called for an esAddIn style Wizard (this kind of Wizard will handle all <br>
its own interfacing to the IDE through the upcoming TIToolServices <br>
interface). The style will determine how the Wizard was invoked. In this <br>
case, we just call a MessageDlg in the Execute method to indicate that the <br>
Wizard is actually alive. <br>
<br>
procedure TGenericExpert.Execute; <br>
begin <br>
MessageDlg('Hello Nashville!', mtInformation, [mbOk], 0) <br>
end {Execute}; <br>
<br>
To install our first Wizard, all we need to do is act like it's a new <br>
component: For Delphi 1.0, pick Options | Install, for Delphi 2.0x and 3 <br>
select Component | Install, and add it to the list of installed components. <br>
Delphi 1 and 2 simply add the Wizard the the Component Library, but Delphi 3 <br>
needs to add it to a package - the DCLUSR30 package by default: <br>
<br>
<br>
<br>
-- <br>
※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 159.226.64.144] <br>
</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="13.htm">上一层</a>][<a href="66.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -