1

Delphi 7には、設計時にプロジェクト全体で特定のタイプのすべてのコンポーネントのプロパティを特定の値に設定する方法がありますか?

すべてのTQuery'を'に置き換えるためにGExpertsを使用しましTADOQueryたが、それに応じて接続文字列を設定する必要があります。

GExpertsの「コンポーネントプロパティの設定」を使用してみましたが、接続プロパティでは機能しないようです。

.dfmファイルで検索と置換を行うことを考えましたが、接続プロパティは手動で設定されるまで.dfmファイルに保存されません。

4

1 に答える 1

1

まず、すべてのクエリを にリンクし、TADOConnectionそのコンポーネントだけに接続文字列を指定する方が賢明です。接続文字列は、開発中に数回変更される可能性が高いものの 1 つであり、クライアントごとに変更される可能性があることは言うまでもありません。すべてのシングルにそれを持つことは、それTADOQueryをハードコーディングすることと同じです。それは良い考えではありません。

次に、よく考えて、grep ツールを使用して、TADOQuery取得したコンポーネントの実際の数を確認します。他のツールを使わずにこれを手動で行うことは非常に可能かもしれません.他の人のために話すことはできません. ツールを使用する場合でも、すべての DFM をチェックして、正常に機能していることを確認する必要があります。そのためには、それらすべてを IDE で開く必要があります。

最後に、この質問に対する答えは、これを自動的に行う方法がなければ完全ではないため、簡単な方法を次に示します。

以下は、DFM ファイルの名前、コンポーネント クラスの名前、および DFM で必要な値を取得する簡単な手順です。値を探し、プロパティに指定された名前があることを確認します。プロパティが存在しない場合は、プロパティを追加します。次のように使用します。

FixComponentPropertyInDFM('Unit16.dfm', 'TPanel', 'Caption', '''Changed Caption''');

コードは、Delphi 7 でコンパイルされることを確認するために正規表現を使用していません。コードの半分は、DFM ファイル内のパターンの識別を扱っているため、正規表現を使用すると、コードのサイズが 1/4 になります。

procedure FixComponentPropertyInDFM(const FileName:string; const ClassName, PropertyName, RequiredValue: string);
var L: TStringList;
    i,j, iEnd, iLine: Integer;
    ExtraObjectCount: Integer;
    cLine, trimedLine, TestLine: string;
    lcSufixString: string;
    lcSufixLen: Integer;
    lcPropertyPrefix: string;
    lcPropertyPrefixLen: Integer;
    DelphiId: AnsiString;
    ValidId: Boolean;
    CorrectLineValue: string;

    ChangedTheDFM: BOolean;
begin
  lcSufixString := LowerCase(': ' + ClassName);
  lcSufixLen := Length(lcSufixString);

  lcPropertyPrefix := LowerCase(PropertyName + ' =');
  lcPropertyPrefixLen := Length(lcPropertyPrefix);

  CorrectLineValue := PropertyName + ' = ' + RequiredValue;

  ChangedTheDFM := False;

  L := TStringList.Create;
  try
    L.LoadFromFile(FileName);
    i := 0;
    while i < L.Count do
    begin
      cLine := L[i];
      // Is the current line of the form [object Name: ClassName] ?
      if (Length(cLine) > lcSufixLen) and (LowerCase(System.Copy(cLine, Length(cLine)-lcSufixLen+1, lcSufixLen)) = lcSufixString) then
      begin
        // This line is most likely the start of an object definition, of the type I want.
        // Check this further: does it start with "object"?
        trimedLine := Trim(cLine);
        if LowerCase(System.Copy(trimedLine, 1, 7)) = 'object ' then
        begin
          // Further checks. Doesn't hurt to be extra-safe.
          System.Delete(trimedLine, 1, 7);
          System.Delete(trimedLine, Length(trimedLine) - lcSufixLen+1, System.MaxInt);
          trimedLine := Trim(trimedLine);
          // What's left should be a valid Identifier. Also check that!
          DelphiId := AnsiString(trimedLine); // I'm writing this on Delphi2010, but I want the code to compile on Delphi 7; And I want to use the "char-in-set"
          if (Length(DelphiId) > 0) and (DelphiId[1] in ['a'..'z', 'A'..'Z', '_']) then
          begin
            ValidId := True;
            for j:=2 to Length(DelphiId) do
              if not (DelphiId[j] in ['a'..'z', 'A'..'Z', '_', '0'..'9']) then
                ValidId := False;
            if ValidId then
            begin
              // This line passed all tests. I know *for sure* an object definition of the required class starts here.
              iLine := -1; // Line where the property definition was found in the DFM
              iEnd := -1; // Line where the "end" that finishes the definition was found
              ExtraObjectCount := 0; // Counts the depth for the extra objects we might find.
              j := i+1;
              while (iEnd = -1) and (j < L.Count) do
              begin
                TestLine := LowerCase(Trim(L[j]));
                if System.Copy(TestLine, 1, 7) = 'object ' then Inc(ExtraObjectCount)
                else if System.Copy(TestLine, 1, 10) = 'inherited ' then Inc(ExtraObjectCount)
                else if TestLine = 'end' then
                  begin
                    // I'm seeing the end of an object. Don't know if it's *our* object
                    // or some other object.
                    Dec(ExtraObjectCount);
                    if ExtraObjectCount = -1 then iEnd := j; // Our object.
                  end
                else if ExtraObjectCount = 0 then
                  begin
                    // We know we're within our object, not some embeded object.
                    if System.Copy(TestLine, 1, lcPropertyPrefixLen) = lcPropertyPrefix then
                      iLine := j;
                  end;
                // Next line
                Inc(j);
              end;
              // Alter the DFM!
              if iLine = -1 then
                begin
                  // Did not find the line.
                  if iEnd = -1 then raise Exception.Create('BUG: Did not find "end"');
                  L.Insert(iEnd, CorrectLineValue);
                  ChangedTheDFM := True;
                end
              else
                begin
                  // Found the value.
                  if Trim(L[iLine]) <> CorrectLineValue then
                  begin
                    L[iLine] := CorrectLineValue;
                    ChangedTheDFM := True;
                  end;
                end;
            end;
          end;
        end;
      end;

      // Next line
      Inc(i);
    end;

    if ChangedTheDFM then
    begin
      // Make a backup of the original DFM.
      if FileExists(FileName + '.backup') then
        raise Exception.Create(FileName + '.backup already exists.');
      if not CopyFile(PChar(FileName), PChar(string(FileName + '.backup')), True) then
        raise Exception.Create('Can''t create BACKUP file.');
      L.SaveToFile(FileName);
    end;

  finally L.Free;
  end;
end;
于 2013-01-23T08:07:08.323 に答える