你需要为你的组件注册一个自定义的组件编辑器,并重写三个方法:
function GetVerbCount: Integer;
function GetVerb(Index: Integer): string;
procedure ExecuteVerb(Index: Integer);
这是一个(极少的)示例,在 Delphi 2007 中快速组合在一起:
MyTestComponentPackage.dpk - 一个新的 VCL 包(文件->新建->包)
package MyTestComponentPackage;
{$R *.res}
{$ALIGN 8}
{$ASSERTIONS ON}
{$BOOLEVAL OFF}
{$DEBUGINFO ON}
{$EXTENDEDSYNTAX ON}
{$IMPORTEDDATA ON}
{$IOCHECKS ON}
{$LOCALSYMBOLS ON}
{$LONGSTRINGS ON}
{$OPENSTRINGS ON}
{$OPTIMIZATION ON}
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
{$REFERENCEINFO ON}
{$SAFEDIVIDE OFF}
{$STACKFRAMES OFF}
{$TYPEDADDRESS OFF}
{$VARSTRINGCHECKS ON}
{$WRITEABLECONST OFF}
{$MINENUMSIZE 1}
{$IMAGEBASE $400000}
{$IMPLICITBUILD ON}
requires
rtl,
DesignIDE;
contains
MyComponentUnit in 'MyComponentUnit.pas',
MyCompRegUnit in 'MyCompRegUnit.pas';
end.
MyComponentUnit.pas - 用于组件代码本身的新 Delphi 单元(文件->新建->单元)。 Unit 为后退/前进功能声明了一个自定义类型。该组件除了声明我们可以在设计时通过弹出菜单设置的那种类型的属性之外什么都不做。
unit MyComponentUnit;
interface
uses
Classes;
type
TMyComponentDirection = (cdBack, cdForward);
type
TMyComponent=class(TComponent)
private
FDirection: TMyComponentDirection;
published
property Direction: TMyComponentDirection read FDirection write FDirection;
end;
implementation
end.
MyCompRegUnit.pas,实现自定义组件编辑器,同时注册组件及其编辑器:
unit MyCompRegUnit;
interface
uses
DesignIntf, DesignEditors, Classes;
type
TMyComponentEditor=class(TComponentEditor)
function GetVerbCount: Integer; override;
function GetVerb(Index: Integer): string; override;
procedure ExecuteVerb(Index: Integer); override;
end;
procedure Register;
implementation
{ TMyComponentEditor }
uses
MyComponentUnit;
// Called when component of this type is right-clicked. It's where
// you actually perform the action. The component editor is passed a reference
// to the component as "Component", which you need to cast to your specific
// component type
procedure TMyComponentEditor.ExecuteVerb(Index: Integer);
begin
inherited;
case Index of
0: (Component as TMyComponent).Direction := cdBack;
1: (Component as TMyComponent).Direction := cdForward;
end;
end;
// Called the number of times you've stated you need in GetVerbCount.
// This is where you add your pop-up menu items
function TMyComponentEditor.GetVerb(Index: Integer): string;
begin
case Index of
0: Result := '&Back';
1: Result := '&Forward';
end;
end;
// Called when the IDE needs to populate the menu. Return the number
// of items you intend to add to the menu.
function TMyComponentEditor.GetVerbCount: Integer;
begin
Result := 2;
end;
procedure Register;
begin
RegisterComponents('TestStuff', [TMyComponent]);
RegisterComponentEditor(TMyComponent, TMyComponentEditor);
end;
end.
您需要保存并构建包,然后在项目管理器中右键单击它并选择“安装”。它将在TestStuff 组件面板页面上注册组件。
全部保存,然后启动一个新的 VCL 表单应用程序。在 Component Palette 中,键入 TMy 以找到新组件,然后双击它以将其添加到新表单中。在表单上右键单击它: