原来窗体上的 button1 有一个事件. 后来我把button1 控件删了,但其事件代码还在,并且能通过编译执行. 本来这样对我的程式没有什么影响,但后来有近100多个button控件删了,他们的事件代码还在,这样编译执行速度就慢了很多. 现在我的窗体上有很多BUTTON,我不知哪个BUTTON控件不在但它的事件还在的.
如何找出这些根本不起作用又占在代码里的多佘控件代码?非常感谢.这个"有趣"的问题已困扰了我一年多.
如何找出这些根本不起作用又占在代码里的多佘控件代码?非常感谢.这个"有趣"的问题已困扰了我一年多.
uses TypInfo;procedure UnusedMethods(mComponent: TComponent; mStrings: TStrings);
//设计 Zswang 2006-08-05 wjhu111#21cn.com 尊重作者,转贴请注明出处
type
PMethodInfo = ^TMethodInfo;
TMethodInfo = packed record
rSize: Word;
rAddr: Pointer;
rName: ShortString;
end;type
TMethodTable = packed record
rCount: Word;
rMethodInfo: array[0..0] of TMethodInfo;
end;
PMethodTable = ^TMethodTable;var
vEventList: TList; vMethodTable: PMethodTable;
vMethodInfo: PMethodInfo;
I: Integer; procedure fScanEvent(mComponent: TComponent);
var
vPropList: PPropList;
vPropCount: Integer;
vMethod: TMethod;
I: Integer;
begin
if not Assigned(mComponent) then Exit;
vPropCount := GetPropList(mComponent, vPropList);
for I := 0 to vPropCount - 1 do
begin
case vPropList[I].PropType^.Kind of
tkMethod:
begin
vMethod := GetMethodProp(mComponent, vPropList[I].Name);
if Assigned(vMethod.Code) then vEventList.Add(vMethod.Code);
end;
end;
end; for I := 0 to mComponent.ComponentCount - 1 do
fScanEvent(mComponent.Components[I]);
end;
begin
if not Assigned(mStrings) or not Assigned(mComponent) then Exit;
vEventList := TList.Create;
mStrings.BeginUpdate;
try
mStrings.Clear;
vMethodTable := PPointer(PChar(mComponent.ClassType) + vmtMethodTable)^;
if vMethodTable.rCount > 0 then
vMethodInfo := @vMethodTable^.rMethodInfo[0]; fScanEvent(mComponent); //收集所有控件事件中使用到的方法地址
for I := 0 to vMethodTable.rCount - 1 do //分析当前元件中声明但没有使用的方法
begin
if vEventList.IndexOf(vMethodInfo.rAddr) < 0 then
mStrings.Add(vMethodInfo.rName);
Inc(PChar(vMethodInfo), SizeOf(Word) + SizeOf(Pointer) + SizeOf(Byte) +
Length(vMethodInfo^.rName));
end;
vEventList.Free;
finally
mStrings.EndUpdate;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
UnusedMethods(Self, Memo1.Lines); //输出结果
end;