vst实例(9)创建编辑器

发布时间 2023-05-31 08:56:48作者: Luo大哥

先上编辑器单元的代码:

unit editlink;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, VirtualTrees;
type
   tcomboeditlink=class(TInterfacedObject, IVTEditLink)
   private
    Fedit:TComboBox;
    itemstrs:TStringList;
    Fnode:PVirtualNode;
    Fcolumn:Integer;
    Ftree:TVirtualStringTree;
   public
    constructor create;
    constructor createof(strings:TStrings);
    function BeginEdit: Boolean; stdcall;                  // Called when editing actually starts.
    function CancelEdit: Boolean; stdcall;                 // Called when editing has been cancelled by the tree.
    function EndEdit: Boolean; stdcall;                    // Called when editing has been finished by the tree. Returns True if successful, False if edit mode is still active.
    function PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; stdcall;
                                                           // Called after creation to allow a setup.
    procedure ProcessMessage(var Message: TMessage); stdcall;
                                                           // Used to forward messages to the edit window(s)-
    procedure SetBounds(R: TRect); stdcall;
   end;
implementation
constructor tcomboeditlink.create;
begin
  inherited create;
  itemstrs:=TStringList.Create;
end;

constructor tcomboeditlink.createof(strings: TStrings);
begin
  create;
  itemstrs.Assign(strings);
end;

function tcomboeditlink.BeginEdit:Boolean;
begin
  Result:=True;
end;

function tcomboeditlink.CancelEdit:boolean;
begin
  Result:=True;
end;

function tcomboeditlink.EndEdit:boolean;
begin
  Fedit.Hide;
  Ftree.Text[Fnode,Fcolumn]:=Fedit.Text;
  Result:=True;
end;

function tcomboeditlink.PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean;
begin
  Ftree:=TVirtualStringTree(Tree);
  Fnode:=Node;
  Fcolumn:=Column;
  fedit:=TComboBox.Create(Ftree);
  Fedit.Hide;
  Fedit.Parent:=Ftree;
  Fedit.Items.Assign(itemstrs);
  Fedit.Text:=Ftree.Text[Fnode,Fcolumn];
  FreeAndNil(itemstrs);
  Fedit.Show;
  Result:=True;
end;

procedure tcomboeditlink.SetBounds(R: TRect);
begin
  Fedit.BoundsRect:=r;
  Fedit.Width:=80;
end;

procedure tcomboeditlink.ProcessMessage(var Message: TMessage);
begin
  Fedit.WindowProc(Message);
end;
end.

编辑器通常由两个部分组成:

TInterfacedObject:编辑器的源控件,可以是tedit,可以是tcombobox,还可以是其它任何可视控件。

IVTEditLink:编辑器接口。

根据编辑器的接口,至少应该为以下部分写代码:

function BeginEdit: Boolean; stdcall;

用于开始编辑,如果返回false,则不允许编辑。

function CancelEdit: Boolean; stdcall;

用于取消编辑。通常情况下,编辑是允许取消的,但你可以在特定条件下不允许取消。如果不允许取消,只需设置result:=false即可。

function EndEdit: Boolean; stdcall;

用于编辑结束,在此阶段你应该写上你对于编辑结果的处理。

function PrepareEdit(Tree: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex): Boolean; stdcall;

用于准备开始编辑时的处理,例如本程序在此阶段才开始对combobox的items赋值,对编辑器的text进行赋值等等。

procedure ProcessMessage(var Message: TMessage); stdcall;

对消息的处理。

procedure SetBounds(R: TRect); stdcall;

对编辑器的边界的处理。

以上部分是必须重写的部分,事实上,你还可以写上对可视控件的诸如“onchange”“onkeypressed”等事件的处理,或者创建代码的重写等等。