自己动手编写Delphi Expert

本文介绍如何在Delphi中创建自定义Expert,通过继承TIExpert类并实现其抽象方法,可以轻松扩展Delphi IDE的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    在刚刚开始编程阶段,自己一步步编写出一个登录窗体,或是一个能够对数据表记录进行增、删、改的窗体时,会感到非常的兴奋。但随着编程时间和编写项目的增加,会发现这种没有什么技术含量的窗体被不断的重复编写,于是乎开始思考有没有一种一劳永逸的办法可以帮助我们摆脱这种窘境呢?有的朋友或者会说,将这些做成一个标准的窗体存放在某个地主,在需要的时候将其Copy过来,然后略为修改。不可否认,这也是一种方法,但对于一个编程人员来说,这种方法很不专业。另一方法则相对更为专业,就是自已动手编写一个Expert。

    Delphi是一个完全开放的开发集成环境,提供了一组ToolsAPI供开发者对其进行扩展(相关单元在Delphi的安装目录下的Source/ToolsAPI/中)。要编写自己的Expert需要引用其中的ExptIntf.pas文件,它定义了一个抽象基类TIExpert,任何自定义的Expert均要继承于他。

  TIExpert = class(TInterface)
  
public
    
{ Expert UI strings }
    function GetName: 
stringvirtual; stdcall; abstract;
    function GetAuthor: 
stringvirtual; stdcall; abstract;
    function GetComment: 
stringvirtual; stdcall; abstract;
    function GetPage: 
stringvirtual; stdcall; abstract;
{$IFDEF MSWINDOWS}
    function GetGlyph: HICON; 
virtual; stdcall; abstract;
{$ENDIF}
{$IFDEF LINUX}
    function GetGlyph: Cardinal; 
virtual; stdcall; abstract;
{$ENDIF}
    function GetStyle: TExpertStyle; 
virtual; stdcall; abstract;
    function GetState: TExpertState; 
virtual; stdcall; abstract;
    function GetIDString: 
stringvirtual; stdcall; abstract;
    function GetMenuText: 
stringvirtual; stdcall; abstract;

    
{ Launch the Expert }
    procedure Execute; 
virtual; stdcall; abstract;
  end;

    下面对这个类中的方法进行简要的说明

方法名称描述
GetName子类必须改写此方法,给Expert指定一个唯一的名字
GetAuthor如果编写的是Form Export或Project Expert则子类必须改写此方法,给Expert指定作者,这个字串将出现在Object Repository中
GetComment如果编写的是Form Export或Project Expert则子类必须改写此方法,简单描述Expert的用途
GetPage如果编写的是Form Export或Project Expert则子类必须改写此方法,指定Expert出现在New Item窗体的哪个页面中
GetGlyph如果编写的是Form Export或Project Expert则子类必须改写此方法,指定Expert在New Item窗体中显示的图标,返回0值表示使用默认图标
GetStyle子类必须改写此方法,指定Expert类型,只能取以下四个值之一:esStandard, esForm, esProject, esAddIn
GetState如果编写的是StandardExport则子类必须改写此方法,指定显示在Help中的Expert菜单状态
GetIDString子类必须改写此方法,提供一个不与其他任何Expert冲突的字符串,按照惯例,该字符串格式为CompanyName.ExpertFunction
GetMenuText如果编写的是Standard Export则子类必须改写此方法,指定显示在Help中的Expert菜单标题
Execute如果编写的是Standard Export、Form Export或Project Expert则子类必须改写此方法,当点击菜单或是New Item中的Expert图标时会执行此方法。

    下面这个Demo会在Delphi IDE的Help菜单中添加一个菜单项,点击之后会弹出一个对话框显示“This is Expert Demo.”。

unit Unit2;

interface

uses SysUtils, Windows, Dialogs, ExptIntf;

type
  TMyExpert 
= class(TIExpert)
  
public
    function GetName: 
stringoverride;
    function GetAuthor: 
stringoverride;
    function GetComment: 
stringoverride;
    function GetPage: 
stringoverride;
    function GetGlyph: HICON; 
override;
    function GetStyle: TExpertStyle; 
override;
    function GetState: TExpertState; 
override;
    function GetIDString: 
stringoverride;
    function GetMenuText: 
stringoverride;

    
{ Launch the Expert }
    procedure Execute; 
override;
  end;

implementation

{ TMyExpert }

procedure TMyExpert.Execute;
begin
  inherited;
  ShowMessage(GetComment);
end;

function TMyExpert.GetAuthor: 
string;
begin
  Result :
= 'ChrisMao';
end;

function TMyExpert.GetComment: 
string;
begin
  Result :
= 'This is Expert Demo.';
end;

function TMyExpert.GetGlyph: HICON;
begin
  Result :
= 0;
end;

function TMyExpert.GetIDString: 
string;
begin
  Result :
= 'ChrisMao.ExpertDemo';
end;

function TMyExpert.GetMenuText: 
string;
begin
  Result :
= 'Click Me';
end;

function TMyExpert.GetName: 
string;
begin
  Result :
= 'ExpertDemo';
end;

function TMyExpert.GetPage: 
string;
begin
  Result :
= EmptyStr;
end;

function TMyExpert.GetState: TExpertState;
begin
  Result :
= [esEnabled];
end;

function TMyExpert.GetStyle: TExpertStyle;
begin
  Result :
= esStandard;
end;

end.

    注:这种从TIExpert继承,来编写Expert的方法,在Delphi6中已经被淘汰了(但这不表示这种方法不可用),取而代之的是使用Open Tools API方式实现。

Context: 1: Fasten Your Seat Belts 'Chapter 1: Fasten Your Seat Belts' 'Delphi installation' 'Delphi compilers and toolchains' 'Hello World app' 'Deploying to mobile devices' 'Summary' 2: Mind Your Language 'Chapter 2: Mind Your Language' 'Do you speak Object Pascal?' 'Object Pascal Phrase Book' 'Summary' 3: Packing Up Your Toolbox 'Chapter 3: Packing Up Your Toolbox' 'Parallel Programming Library' 'Working with files' 'JSON' 'XML' 'Summary' 4: Playing with FireMonkey 'Chapter 4: Playing with FireMonkey' 'Drawing in code' 'Get moving with timers' 'The power of parenting' 'Touch me' 'Game of Memory' 'Summary' 5: FireMonkey in 3D 'Chapter 5: FireMonkey in 3D' 'Cross-platform 3D rendering' 'Using Context3D' 'Custom Wireframe component''Objects 3D' 'Moving Earth' 'Building an interactive 3D scene' 'Using 3D models' 'Starfield simulation' 'Mixing 3D and 2D' 'Summary' 6: Building User Interfaces with Style 'Chapter 6: Building User Interfaces with Style' 'Working with built-in styles' 'Using custom styles' 'Embedding styles as resources' 'Customizing styles' 'Using frames' 'Working with inherited views' 'Previewing forms on devices' 'Summary' 7: Working with Mobile Operating System 'Chapter 7: Working with Mobile Operating System' 'James Bond's toy' 'What I'm running on?' 'The life of an app' 'Sensing the world' 'Taking photos' 'Using share sheets' 'Camera, light, action!' 'Working with address book' 'Notify me!' 'Navigating the we' 'Working with maps' 'Creating and consuming Android services' 'Delphi language bridges' 'Summary' 8: Extending to the Internet of Things 'Chapter 8: Extending to the Internet of Things' 'Communication protocols' 'Understanding BLE''Connecting to things with ThingConnect' 'Getting close with beacons' 'Proximity solutions with BeaconFence' 'App tethering' 'Summary' 9: Embedding Databases 'Chapter 9: Embedding Databases' 'Architecting data-driven apps' 'Modeling data' 'Choosing a database' 'Accessing databases with FireDAC' 'Building data-driven user interface' 'Using visual live bindings' 'Fast user interface prototyping' 'Summary' 10: Integrating with Web Services 'Chapter 10: Integrating with Web Services' 'Understanding web services' 'Native HTTP client' 'Consuming XML SOAP web services' 'Integrating with REST services' 'Backend as a service client' 'Integrating with the cloud' 'Summary' 11: Building Mobile Backends 'Chapter 11: Building Mobile Backends' 'Delphi and multi-tier architectures' 'Getting low-level with WebBroker' 'Do it yourself with DataSnap' 'Easy REST API publishing with RAD Server' 'Summary' 12: App Deployment 'Chapter 12: App Deployment' 'Deploying to App Stores' 'Enhancing your apps' 'Summary' 13: The Road Ahead'Chapter 13: The Road Ahead' 'What we have learned' 'Staying on top of everything' 'Your next Delphi mobile app' 'Summary'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值