本身ModalResult不等于mrNone就会被关闭,你只要自己拦载ModalResult值,然后跟据你的设
定再设定ModalResult为mrNone或其它值,使其符合你的要求.在被SHOWMODAL的那个表单中处
理.实际上楼上的ugvanxk已经说到点上了.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCloseQuery(Sender: TObject
var CanClose: Boolean);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
bOK:Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormCloseQuery(Sender: TObject
var CanClose: Boolean);
begin
{ if bOK then CanClose:=True
else canClose:=False;}//两种方法,改变CanClose,或ModalResult
if bOK then
ModalResult:=mrOK
else ModalResult:=mrNone;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
Close;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
bOK:=True;
end;
end.
FORM1调用.
procedure TForm1.Button1Click(Sender: TObject);
begin
try
form2:=TForm2.Create(Application);
form2.ShowModal;
finally
form2.free;
end;
end;
再看CustomForm中的源代码ShowModal部分
try
Show;
try
SendMessage(Handle, CM_ACTIVATE, 0, 0);
ModalResult := 0;
repeat
Application.HandleMessage;
if Application.FTerminate then ModalResult := mrCancel else
if ModalResult <> 0 then CloseModal;//看这儿,是一个循环,ModalResult不等于0就关闭
until ModalResult <> 0;
Result := ModalResult;
SendMessage(Handle, CM_DEACTIVATE, 0, 0);
if GetActiveWindow <> Handle then ActiveWindow := 0;
finally
Hide;
end;