使源可以从Delphi IDE中写入

前端之家收集整理的这篇文章主要介绍了使源可以从Delphi IDE中写入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以在Delphi IDE中编写只读源代码(例如,.pas& .dfm)?右键单击选项可在IDE中生成文件Readonly / Writable,但不会更改文件系统上的属性.是否有IDE扩展或类似可以实现此目的?

在不必集成源控制系统的情况下执行此操作的方法将是优选的.我使用的是Delphi XE和Delphi 6.

谢谢!

上证

解决方法

我就是这样做的.

创建一个新的包,它将在设计时安装到IDE中.如果你有一个现有的包方便,那么你可以继续使用它.确保包装需要指定包装.您可以在项目管理器中执行此操作,或者仅通过查看项目源并将requireide添加到requires子句.

现在将以下单元添加到您的包中.

unit MakeEditable;

interface

procedure Register;

implementation

uses
  Windows,SysUtils,Menus,ToolsAPI;

type
  TMakeEditable = class(TObject)
  private
    FEditorServices: IOTAEditorServices;
    FFileMenu: TMenuItem;
    FMakeEditable: TMenuItem;
    function MenuItemWithCaptionLike(const Menu: TMenuItem; const DesiredCaption: string): TMenuItem;
    procedure MakeEditableClick(Sender: TObject);
  public
    constructor Create;
    destructor Destroy; override;
  end;

constructor TMakeEditable.Create;
var
  Index: Integer;
  PrevIoUsMenuItem: TMenuItem;
begin
  inherited;
  FEditorServices := (BorlandIDEServices as IOTAEditorServices);
  FFileMenu := MenuItemWithCaptionLike((BorlandIDEServices as INTAServices40).MainMenu.Items,'File');
  if Assigned(FFileMenu) then begin
    PrevIoUsMenuItem := MenuItemWithCaptionLike(FFileMenu,'Reopen');
    if Assigned(PrevIoUsMenuItem) then begin
      Index := PrevIoUsMenuItem.MenuIndex;
      if Index>=0 then begin
        FMakeEditable := TMenuItem.Create(FFileMenu);
        FMakeEditable.Caption := 'Ma&ke Editable';
        FMakeEditable.OnClick := MakeEditableClick;
        FFileMenu.Insert(Index,FMakeEditable);
      end;
    end;
  end;
end;

destructor TMakeEditable.Destroy;
begin
  FMakeEditable.Free;
  inherited;
end;

function TMakeEditable.MenuItemWithCaptionLike(const Menu: TMenuItem; const DesiredCaption: string): TMenuItem;
var
  i: Integer;
  Target,Found: string;
begin
  Target := StringReplace(LowerCase(Trim(DesiredCaption)),'&','',[rfReplaceAll,rfIgnoreCase]);
  for i := 0 to Menu.Count-1 do begin
    Result := Menu.Items[i];
    Found := StringReplace(LowerCase(Trim(Result.Caption)),rfIgnoreCase]);
    if Pos(Target,Found)>0 then begin
      exit;
    end;
  end;
  Result := nil;
end;

procedure TMakeEditable.MakeEditableClick(Sender: TObject);

  procedure MakeFileEditable(const FileName: string);
  var
    Attributes: DWORD;
  begin
    Attributes := GetFileAttributes(PChar(FileName));
    SetFileAttributes(PChar(FileName),Attributes and not FILE_ATTRIBUTE_READONLY);
  end;

var
  FileName: string;
  FileExt: string;
  LinkedFileName: string;
  EditBuffer: IOTAEditBuffer;

begin
  EditBuffer := FEditorServices.TopBuffer;
  FileName := EditBuffer.FileName;
  if FileExists(FileName) then begin
    MakeFileEditable(FileName);
    EditBuffer.IsReadOnly := False;

    FileExt := ExtractFileExt(FileName);
    if SameText(FileExt,'.dfm') then begin
      LinkedFileName := ChangeFileExt(FileName,'.pas');
    end else if SameText(FileExt,'.pas') then begin
      LinkedFileName := ChangeFileExt(FileName,'.dfm');
    end else begin
      LinkedFileName := '';
    end;
    if (LinkedFileName<>'') and FileExists(LinkedFileName) then begin
      MakeFileEditable(LinkedFileName);
    end;
  end;
end;

var
  MakeEditableInstance: TMakeEditable;

procedure Register;
begin
  MakeEditableInstance := TMakeEditable.Create;
end;

initialization

finalization
  MakeEditableInstance.Free;

end.

当您编译并安装此软件包时,您现在将在“文件菜单上有一个新菜单项,它既清除输入缓冲区中的只读标志,又使文件可写.

原文链接:https://www.f2er.com/delphi/101973.html

猜你在找的Delphi相关文章