在Delphi中调用Live Template时,如何创建GUID?

在Delphi中调用Live Template时,如何创建GUID?,第1张

概述我在Delphi中使用了Live Templates,但是试图提出一个将GUIDS添加到模板的解决方案。有谁知道如何做到这一点? 在模板下面我现在用GUID作为一个单词我需要手动替换。 <?xml version="1.0" encoding="utf-8" ?><codetemplate xmlns="http://schemas.borland.com/Delphi/2005/codet 我在Delphi中使用了live Templates,但是试图提出一个将GUIDS添加到模板的解决方案。有谁知道如何做到这一点?

在模板下面我现在用GUID作为一个单词我需要手动替换。

<?xml version="1.0" enCoding="utf-8" ?><codetemplate   xmlns="http://schemas.borland.com/Delphi/2005/codetemplates"            version="1.0.0"><template name="iacc" surround="false" invoke="manual">    <point name="name">        <text>            IntfAccessors        </text>        <hint>            Accessors name        </hint>    </point>    <description>        accessor declaration    </description>    <author>        PMH    </author>    <code language="Delphi" context="methoddecl" delimiter="|">    <![cdaTA[I|name|Accessors = interface(IInterface)GUID <-- here I want a GUIDend;I|name| = interface(I|name|Accessors)GUID <-- here I want a GUIDend;    ]]>        </code>    </template></codetemplate>
解决方法 您可以通过编写自定义脚本引擎来扩展IDE来执行此 *** 作。 ( Here是由Nick Hodges撰写的一篇文章,其中插入了当前日期的类似示例。)

我假设您的示例模板中的两个不同的界面需要两个不同的IID,所以我写了脚本引擎来从“脚本”加载点名称(它只是名称=值对的列表,其中名称是点名称和值必须为NewGuID,否则忽略),因此您可以创建具有多个点的模板,每个点都接收单独的新IID。

示例模板intf.xml:

<?xml version="1.0" enCoding="utf-8" ?><codetemplate   xmlns="http://schemas.borland.com/Delphi/2005/codetemplates" version="1.0.0"><template name="iacc" surround="false" invoke="manual">    <point name="name">        <text>            Accessor        </text>        <hint>            Accessors name        </hint>    </point>    <point name="guID1"/>    <point name="guID2"/>    <description>        accessor declaration    </description>    <author>        PMH    </author>    <script language="NewGuIDScript" onvalIDate="true">        guID1=NewGuID        guID2=NewGuID    </script>    <code language="Delphi" context="any" delimiter="|">    <![cdaTA[I|name|Accessors = interface(IInterface)|*||guID1|end;I|name| = interface(I|name|Accessors)|*||guID2|end;]]>    </code>    </template></codetemplate>

NewGuIDScriptEngine.pas:

unit NewGuIDScriptEngine;interfaceuses  Classes,SysUtils,ToolsAPI,CodeTemplateAPI,DesignEditors;type  TNewGuIDScriptEngine = class(TNotifIErObject,IOTACodeTemplateScriptEngine)  public    procedure Execute(const ATemplate: IOTACodeTemplate; const APoint: IOTACodeTemplatePoint; const ASyncPoints: IOTASyncEditPoints; const AScript: IOTACodeTemplateScript; var Cancel: Boolean);    function GetIDString: WIDeString;    function GetLanguage: WIDeString;  end;procedure Register;implementationuses  ActiveX,ComObj;procedure Register;begin  (BorlandIDEServices as IOTACodeTemplateServices).RegisterScriptEngine(TNewGuIDScriptEngine.Create);end;procedure TNewGuIDScriptEngine.Execute(const ATemplate: IOTACodeTemplate; const APoint: IOTACodeTemplatePoint;  const ASyncPoints: IOTASyncEditPoints; const AScript: IOTACodeTemplateScript; var Cancel: Boolean);var  I: Integer;  GuID: TGUID;  P: IOTACodeTemplatePoint;  Points: TStringList;begin  Cancel := False;  if not Assigned(ATemplate) then    Exit;  Points := TStringList.Create;  try    Points.Text := AScript.Script;    for I := 0 to Points.Count - 1 do      Points.Strings[I] := Trim(Points[I]);    for I := 0 to Points.Count - 1 do      if Points.ValueFromIndex[I] = 'NewGuID' then      begin        P := ATemplate.FindPoint(Points.names[I]);        if Assigned(P) then        begin          oleCheck(CoCreateGuID(GuID));          P.Editable := False;          P.Value := '[''' + GUIDToString(GuID) + ''']';        end;      end;  finally    Points.Free;  end;end;function TNewGuIDScriptEngine.GetIDString: WIDeString;begin  Result := 'OndrejKelle.NewGuIDScriptEngine';end;function TNewGuIDScriptEngine.GetLanguage: WIDeString;begin  Result := 'NewGuIDScript';end;end.

将上述单元放入设计时间包中,将对designIDe.dcp的引用添加到其require子句中,并将该包安装在IDE中。

另一个有用的,类似的模板可能如下所示:

<?xml version="1.0" enCoding="utf-8" ?><codetemplate   xmlns="http://schemas.borland.com/Delphi/2005/codetemplates" version="1.0.0"><template name="iacc" surround="false" invoke="manual">    <point name="name">        <text>        </text>        <hint>            Accessors name        </hint>    </point>    <point name="guID1"/>    <point name="guID2"/>    <description>        accessor declaration    </description>    <author>        PMH    </author>    <script language="NewGuIDScript" onvalIDate="true">        guID1=NewGuID        guID2=NewGuID    </script>    <code language="Delphi" context="any" delimiter="|">    <![cdaTA[const  SIID_I|name|Accessors = |guID1|;  IID_I|name|Accessors: TGUID = SIID_I|name|Accessors;  SIID_I|name| = |guID2|;  IID_I|name|: TGUID = SIID_I|name|;type  I|name|Accessors = interface    [SIID_I|name|Accessors]  end;  I|name| = interface(I|name|Accessors)    [SIID_I|name|]  end;]]>    </code>    </template></codetemplate>

这将声明字符串以及TGUID常量,并在接口声明中重用它们。在这种情况下,插入的GUID值不应该包含在方括号中。您有几个选项来调整脚本引擎来执行此 *** 作:

>修改代码只是不使用方括号>引入一个单独的新功能NewGuIDNoBrackets并在模板中使用它>引入一些简单的语法,如NewGuID(false),您的引擎可以解析并使用参数值来确定是否应使用方括号。

总结

以上是内存溢出为你收集整理的在Delphi中调用Live Template时,如何创建GUID?全部内容,希望文章能够帮你解决在Delphi中调用Live Template时,如何创建GUID?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1280700.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存