前几天也碰了这个问题,是因为dll停靠到主form上,在 dll里有调用forms里的这个方法
function GetParentForm(Control: TControl): TCustomForm;
begin
while Control.Parent <> nil do Control := Control.Parent;
//问题出在这里,因为在dll里不认的主调form的类是否是从Tform继承,
if Control is TCustomForm then
Result := TCustomForm(Control)
else
Result := nil;
end;
作了如下修改
function GetParentForm(Control: TControl): TCustomForm;
var
cName: String;
begin
while Control.Parent <> nil do Control := Control.Parent;
if Control is TCustomForm then
Result := TCustomForm(Control)
else
begin
if upperCase(Control.ClassName) = '这里是主调form的类名' then
Result := TForm(Control)
else
Result := nil;
end;
end;
原因在这
function GetParentForm(Control: TControl): TCustomForm;
begin
while Control.Parent <> nil do Control := Control.Parent;
//这句is判断通不过,因为Control虽然是TCustomForm,但是它是exe中的,而TCustomForm类型信息是dll中的
if Control is TCustomForm then
Result := TCustomForm(Control) else
Result := nil;
end;
function ValidParentForm(Control: TControl): TCustomForm;
begin
Result := GetParentForm(Control);
if Result = nil then
raise EInvalidOperation.CreateFmt(SParentRequired, [Control.Name]);
end;