如何在Delphi XE7下创建默认项目?

时间:2015-10-19 12:56:02

标签: delphi delphi-xe7

与此问题相关:Should "Library path" point to the source files of packages?

Fabricio Araujo建议没有必要设置搜索路径'通过创建默认项目选项'为每个新项目。如何在Delphi XE7中完成?

2 个答案:

答案 0 :(得分:3)

提示你的q,更多的是娱乐而不是其他任何东西,我决定尝试 编写IDE插件,提供一种存储某些首选项目的方法 在某处设置并允许您将它们应用于当前项目。

使用,准备和保存样本.Ini文件,其中包含下面显示的格式的首选设置(重要的是让项目选项名称正确,请参阅下面有关如何找到它们的内容),然后编译下面的单元进入一个新的包,并将其安装在IDE中。当你随后打开一个项目时,它会弹出它。

.Ini中的设置被加载到ValueList编辑器中并按下 其中一个值中的[Return]键将应用于项目。

有趣的是,IDE用于Project seetings的名称在XE7中是相同的 因为他们在D7。 Iow,XE7 IDE在内部使用这些而不是名称 它出现在.DProj XML文件中。您可以点击GetOptions按钮获取完整列表。

与使用IDE OTA服务一样,代码必须包含 相当数量的“行李”。

在D7和XE7中测试。

示例Ini文件:

[settings]
OutputDir=Somewhere
UnitOutputDir=Somewhere else
UnitDir=$(DELPHI)
ObjDir=$(DELPHI)
SrcDir=$(DELPHI)
ResDir=$(DELPHI)

代码:

unit ProjectOptionsXE7u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ToolsAPI, Grids, ValEdit, IniFiles;

type
  TProjectOptionsForm = class(TForm)
    Panel1: TPanel;
    Memo1: TMemo;
    ValueListEditor1: TValueListEditor;
    btnGetOptions: TButton;
    procedure btnGetOptionsClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ValueListEditor1KeyPress(Sender: TObject; var Key: Char);
  private
    function GetCurrentProject: IOTAProject;
    procedure GetOptionsFromIni;
    procedure UpdateOptionValue;
  public
    Services: IOTAServices;
    ProjectGroup : IOTAProjectGroup;
    Project: IOTAProject;
    Options : IOTAProjectOptions;
    ModServices: IOTAModuleServices;
    Module: IOTAModule;
    NotifierIndex: Integer;
    Ini : TMemIniFile;
    IsSetUp : Boolean;
    SetUpCount : Integer;
    procedure GetProjectOptions;
    procedure SetUp;
  end;

var
  ProjectOptionsForm: TProjectOptionsForm;

procedure Register;

implementation

{$R *.dfm}

type
  TIdeNotifier = class(TNotifierObject, IOTANotifier, IOTAIDENotifier)
  protected
    procedure AfterCompile(Succeeded: Boolean);
    procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean);
    procedure FileNotification(NotifyCode: TOTAFileNotification;
      const FileName: string; var Cancel: Boolean);
  end;

procedure Register;
begin
  ProjectOptionsForm:= TProjectOptionsForm.Create(Nil);
  ProjectOptionsForm.Services := BorlandIDEServices as IOTAServices;
  ProjectOptionsForm.NotifierIndex := ProjectOptionsForm.Services.AddNotifier(TIdeNotifier.Create);
end;

procedure CloseDown;
begin
  ProjectOptionsForm.Services.RemoveNotifier(ProjectOptionsForm.NotifierIndex);
  ProjectOptionsForm.Close;
  ProjectOptionsForm.Free;
end;

procedure TIdeNotifier.AfterCompile(Succeeded: Boolean);
begin
end;

procedure TIdeNotifier.BeforeCompile(const Project: IOTAProject; var Cancel: Boolean);
begin
end;

procedure TIdeNotifier.FileNotification(NotifyCode: TOTAFileNotification;
  const FileName: string; var Cancel: Boolean);
begin
  if NotifyCode in [ofnProjectDesktopLoad, ofnActiveProjectChanged]  then begin
    ProjectOptionsForm.Show;
//    ProjectOptionsForm.Memo1.Lines.Add('Got notification');
    ProjectOptionsForm.SetUp;
//    ProjectOptionsForm.Memo1.Lines.Add('after GetProjectOptions');
  end;
end;

procedure TProjectOptionsForm.btnGetOptionsClick(Sender: TObject);
var
  KeyName,
  Value,
  S : String;
  V : Variant;
  i : Integer;
begin
  GetProjectOptions;
  ValueListEditor1.Strings.Clear;
  for i := Low(Options.GetOptionNames) to High(Options.GetOptionNames) do begin
    try
      KeyName := Options.GetOptionNames[i].Name;
      if CompareText(KeyName, 'HeapSize') = 0 then
        NoOp;
      V := Options.Values[KeyName];
      if not VarIsEmpty(V) then
        Value := VarToStr(V)
      else
        Value := '';
      ValueListEditor1.InsertRow(KeyName, Value, True);
    except
      //  Reading some CPP-related settings cause exceptions
      S := '***Error   ' + KeyName; // + ': ' + IntToStr(Options.Values[KeyName].Kind);
      Memo1.Lines.Add(S);
    end;
  end;
end;

procedure TProjectOptionsForm.FormDestroy(Sender: TObject);
begin
  Ini.Free;
end;

procedure TProjectOptionsForm.GetOptionsFromIni;
var
  i : Integer;
  KeyName : String;
  TL : TStringList;
begin
  ValueListEditor1.Strings.Clear;
  TL := TStringList.Create;
  try
    Ini.ReadSection('Settings', TL);
    Assert(TL.Count > 0);
    for i := 0 to TL.Count - 1 do begin
      KeyName := TL[i];
      ValueListEditor1.InsertRow(KeyName, Ini.ReadString('Settings', KeyName, ''), True);
    end;
  finally
    TL.Free;
  end;
end;

procedure TProjectOptionsForm.FormCreate(Sender: TObject);
var
  IniFileName : String;
begin
  IniFileName := 'd:\aaad7\ota\ProjectOptions.Ini'; //  <beware of hard-code path
  Ini := TMemIniFile.Create(IniFileName);
  GetOptionsFromIni;
end;

function TProjectOptionsForm.GetCurrentProject: IOTAProject;
var
  i: Integer;
begin
  Result := nil;
  ModServices := BorlandIDEServices as IOTAModuleServices;
  for i := 0 to ModServices.ModuleCount - 1 do
  begin
    Module := ModServices.Modules[i];
    if Supports(Module, IOTAProjectGroup, ProjectGroup) then begin
      Result := ProjectGroup.ActiveProject;
      Options := Result.ProjectOptions;
      Exit;
    end
    else if Supports(Module, IOTAProject, Project) then
    begin // In the case of unbound packages, return the 1st
      if Result = nil then begin
        Result := Project;
        Options := Result.ProjectOptions;
      end;
    end;
  end;
end;

procedure TProjectOptionsForm.GetProjectOptions;
begin
  Assert(Project <> Nil, 'Project');
  Options := Project.ProjectOptions;
end;

procedure TProjectOptionsForm.SetUp;
begin
  Project := GetCurrentProject;
  GetProjectOptions;
  Inc(SetUpCount);
  Caption := 'Setup done ' + IntToStr(SetUpCount);
  IsSetUp := True;
end;

procedure TProjectOptionsForm.UpdateOptionValue;
var
  Rect : TGridRect;
  S : String;
  KeyName,
  Value : String;
  Row,
  Col : Integer;
begin
  if Options = Nil then
    Exit;
  Rect := ValueListEditor1.Selection;
//  S := 'left: %d top: %d right: %d, bottom: %d';
//  S := Format(S, [Rect.Left, Rect.Top, Rect.Right, Rect.Bottom]);
//  Memo1.Lines.Add(S);
  Row := Rect.Bottom;
  Col := Rect.Left - 1;
  KeyName := ValueListEditor1.Cells[Col, Row];
  Value := ValueListEditor1.Values[KeyName];

  Options.SetOptionValue(KeyName, Value);
  Options.ModifiedState := True;

  Module.Save(False, False);
end;

procedure TProjectOptionsForm.ValueListEditor1KeyPress(Sender: TObject; var
    Key: Char);
begin
  if Key = #13 then
    UpdateOptionValue;
end;

initialization

finalization
  CloseDown;
end.

答案 1 :(得分:2)

  

如何在Delphi XE7中完成此操作?

它不能。此功能已被删除我不知道确切的时间,但它已经存在了很长时间。

你能做的是:

  1. 创建一个新项目。
  2. 请随意更改设置。
  3. 将此修改后的项目模板保存到某个中心位置。
  4. 每当您创建一个新项目时,请通过复制此项目模板来执行此操作。
  5. 您可以将修改后的项目模板保存到Object Repository中,从而将此流程集成到IDE中。使用Project | Add to Repository将项目添加到存储库。