본문 바로가기

Delphi

[Delphi] 폼 생성시, 중복 폼을 방지하기.

procedure TForm1.btnNewClick(Sender: TObject);

var

  NewForm: TForm2;

begin

 

  if nil = FindCreateForm('TForm2') then

    //띄워져있는 폼을 검사, nil이 아닐경우 해당폼이 이미 있음

    begin

    //해당 폼이 없을 경우 밑의 소스 실행

    Application.CreateForm(TForm2, NewForm);

    NewForm.Show;

    end;

end;

  


//특정 Form의 이름을 받아 그 Form이 실행되는지를 검사

function TFormFlowAnalysis.FindCreateForm(const AClassName: string): TForm;

var

  i: Integer;

begin

  Result:= nil;

  for i:=0 to Screen.FormCount-1 do //화면에 띄워진 Form의 갯수만큼.

  begin

    if 0 <> CompareText(Screen.Forms[i].ClassName, AClassName) then Continue;

    //파라메터로 받아온 이름의 Form이 띄워져 있다면.

    Result:= Screen.Forms[i];// Form을 반환

    Break;

  end;

end;