Delphi 7中是否有办法在设计时将特定类型的所有组件的属性设置为整个项目的特定值?
我已经使用 GExperts 将我的所有TQuery
's 替换为TADOQuery
's,但需要相应地设置连接字符串。
我曾尝试使用 GExperts“设置组件属性”,但这似乎不适用于连接属性。
我想在 .dfm 文件中进行搜索和替换,但连接属性在手动设置之前不会存储在 .dfm 文件中。
首先,将所有查询链接到 aTADOConnection
并仅在该组件上指定连接字符串会更聪明。连接字符串是一件事,在开发过程中可能会更改几次,更不用说它可能会从客户端更改为客户端。将它放在每一个上TADOQuery
都相当于对其进行硬编码。这可不是个好主意。
其次,好好考虑一下,使用 grep 工具检查TADOQuery
您拥有的组件的实际数量。很可能手动完成此操作,无需任何工具:不能代表他人发言,但我敢肯定,我宁愿手动更改 100 个查询,然后再使用工具。即使您使用工具,您仍然需要检查所有 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;