下面的代码可以供你参考:
procedure DeactiveForm(var theForm: TComponent; aContainer: TWinControl);
var
form: TForm;
begin
try
Form := nil;
if Assigned(aContainer) then
Form := Tform(GetParentForm(aContainer));
if Form <> nil then begin
Form.KeyPreview := false;
Form.OnKeyDown := nil;
end;
if Assigned(theForm) then
begin
if UpperCase(theForm.Name)='WBHELP' then exit;
if theForm is TMDIBaseForm then
begin
TMDIBaseForm(theForm).ReleaseForceClose;
TForm(theForm).Close;
end else if theForm is TForm then
begin
TForm(theForm).Close;
end;
theForm.Free;
end;
theForm := nil;
except
on e: Exception do
MessageBox(Application.Handle, Pchar('意外错误:' + e.message), '错误', MB_OK + MB_ICONWARNING);
end;
end;